1+ const page = document . querySelector ( ".post" ) ;
2+ const content = document . querySelector ( ".book-page-html" ) ;
3+
4+ const scale = ( page , content ) => { content . style . transform = `scale(${ Math . min ( 1 , page . clientWidth / content . scrollWidth ) } )` ; }
5+ scale ( page , content ) ;
6+ // eliminates resize flashes
7+ content . style . visibility = "visible" ;
8+ window . addEventListener ( "resize" , ( ) => scale ( page , content ) ) ;
9+
10+ // prefetch HTML and image
11+ content . querySelectorAll ( "a" ) . forEach ( anchor => {
12+ if ( ! anchor . href ) return ;
13+ const listener = ( ) => {
14+ const link_html = document . createElement ( 'link' ) ;
15+ link_html . href = anchor . href ;
16+ link_html . rel = "prefetch" ;
17+ link_html . type = "document" ;
18+ document . head . appendChild ( link_html ) ;
19+
20+ const parts = anchor . href . split ( '/' ) ;
21+ const book = parts [ parts . length - 3 ] . replace ( "-html" , "" ) . replace ( "-" , "_" ) ;
22+ const image = parts [ parts . length - 2 ] . replace ( "-" , "_" ) ;
23+
24+ const link_img = document . createElement ( 'link' ) ;
25+ link_img . href = `/assets/images/book/tinkers/3.10.2/${ book } /${ image } .png` ;
26+ link_img . rel = "prefetch" ;
27+ link_img . type = "image" ;
28+ document . head . appendChild ( link_img ) ;
29+
30+ anchor . removeEventListener ( "mouseover" , listener ) ;
31+ } ;
32+ anchor . addEventListener ( 'mouseover' , listener ) ;
33+ } ) ;
0 commit comments