PageSourceSearch

https://www.ilpiccolo.net/ilpiccolodialessandria/news/wp-content/t…/js/image_sizes.js?ver=5.10.37

js ilpiccolo.net collected 2026-10-02 10:00:06 UTC 22,148 bytes, 852 lines download raw bytes

1
2_execute_1 = false;
3_execute_2 = false;
4
5var is_Mobile = false;
6var is_Tablet = false;
7var is_Desktop = false;
8
9var preload_LCP_image_has_run = false;
10
11print_debug(window.pageYOffset);
12
13//Check if Webp is supported
14
15var isWebPSupported = false;
16
17if(isWebPSupported == false){
18    mybrowserversion = getBrowserVersion();
19    mybrowser = mybrowserversion.match(/[a-z]+/i);
20    myversion = mybrowserversion.match(/[\d]+/);
21    if(mybrowser == "Chrome"){
22        if(myversion[0] <=31){
23            isWebPSupported = false;
24        }
25        else{
26            isWebPSupported = true;
27        }
28    }
29    else if (mybrowser == "Safari"){
30        if(myversion[0] <=15){
31            isWebPSupported = false;
32        }
33        else{
34            isWebPSupported = true;
35        }
36    }
37    else if(mybrowser == "Firefox" ){
38        if(myversion[0] <=64){
39            isWebPSupported = false;
40        }
41        else{
42            isWebPSupported = true;
43        }
44    }
45    else if(mybrowser == "Edge" ){
46        if(myversion[0] <=17){
47            isWebPSupported = false;
48        }
49        else{
50            isWebPSupported = true;
51        }
52    }
53    else{
54        isWebPSupported = support_format_webp();
55    }
56}
57
58function support_format_webp()
59{
60 var elem = document.createElement('canvas');
61
62 if (!!(elem.getContext && elem.getContext('2d')))
63 {
64  // was able or not to get WebP representation
65  return true;
66  return elem.toDataURL('image/webp').indexOf('data:image/webp') == 0;
67     
68 }
69 else
70 {
71  // very old browser like IE 8, canvas not supported
72  return false;
73 }
74}
75
76function getBrowserVersion(){
77    var ua= navigator.userAgent;
78    var tem; 
79    var M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
80    if(/trident/i.test(M[1])){
81        tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
82        return 'IE '+(tem[1] || '');
83    }
84    if(M[1]=== 'Chrome'){
85        tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
86        if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
87    }
88    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
89    if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
90    return M.join(' ');
91}
92
93
94
95// launch preload image LCP
96var my_i=0;
97if(typeof interval != "number" ){    
98    const interval = setInterval(function() {
99        // method to be executed;
100        console.log(my_i);
101        //console.log(my_i >= 50);
102        my_i=my_i+1;
103        if( typeof preload_LCP_image == "function" && document.getElementById("image_LCP") != null){
104            preload_LCP_image();
105        }
106        if (my_i >= 20 || preload_LCP_image_has_run == true){
107            clearInterval(interval);
108        }
109        
110    }, 100);
111} 
112
113if(typeof interval_jquery != "number" ){ 
114    const interval_jquery = setInterval(function() {
115        if(typeof jQuery == "function" && preload_LCP_image_has_run == true){
116            clearInterval(interval_jquery);
117            //PHONE
118            if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
119                print_debug("is_phone_START");
120                is_Mobile = true;
121                is_Tablet = true;
122
123                if ( _execute_1 == false ){
124                    _execute_1 = js_primary();
125                    //_execute_2 = js_secondary();
126                }
127
128
129            }  
130            //DESKTOP
131            else {
132                print_debug("is_desktop_START");
133                is_Desktop = true;
134
135                if ( _execute_1 == false ){
136                    _execute_1 = js_primary();
137                    
138                    if ( window.screen.width > 2300 ) {
139						_execute_2 = js_secondary();	
140						setInterval(function(){
141							js_continuos();
142						},500);					
143					}
144                    
145                   
146                }
147
148
149            }
150        }
151        
152    }, 100);
153}  
154
155
156
157
158
159       
160
161
162 if(typeof jQuery == "function"){
163window.onscroll = function() { 
164//jQuery("body").scroll(function(){
165//window.ontouchstart = function() { 
166    print_debug("onscroll_START2");
167    print_debug(window.pageYOffset);
168    if ( _execute_1 == false ){
169       _execute_1 = js_primary();
170    }
171    
172    js_continuos();
173
174    if ( _execute_2 == false ) {
175        if ( _execute_1 == false ){
176            _execute_1 = js_primary();
177        }
178        _execute_2 = js_secondary();
179
180    }
181    }
182//});
183 }
184
185if(typeof jQuery == "function"){
186jQuery(".button_prev ,.button_next").on('click', function() { 
187
188    print_debug(".button_prev ,.button_next_START");
189    if ( _execute_1 == false ){
190       _execute_1 = js_primary();
191    }
192    
193    js_continuos();
194    
195    if ( _execute_2 == false ) {
196        if ( _execute_1 == false ){
197            _execute_1 = js_primary();
198        }
199        _execute_2 = js_secondary();
200    
201    }
202    
203});
204}
205
206///////////////////////////////////////////////////////////////////////////////////////
207
208function preload_LCP_image (){
209    if(preload_LCP_image_has_run == false){
210        print_debug("preload_LCP_image_START");
211        preload_LCP_image_has_run = true;
212        img_post = document.getElementById("image_LCP");
213        if(img_post.getAttribute("srcset") == null && img_post.getAttribute("ta-srcset") != null){
214
215                    img_post.setAttribute("srcset", replaceToWebP( img_post.getAttribute("ta-srcset")));
216                    img_post.setAttribute("sizes", "(max-width:500px) 301px, 700px");
217                    //img_post.setAttribute("sizes", "(max-width:500px) 700px, 700px");
218                }
219    }       
220}
221
222
223function add_onscroll_event(){
224    print_debug("add_onscroll_event_START");
225    
226    if(typeof jQuery == "function"){
227//window.onscroll = function() { 
228jQuery("body").scroll(function(){
229//window.ontouchstart = function() { 
230    print_debug("onscroll_START");
231    print_debug(window.pageYOffset);
232    if ( _execute_1 == false ){
233       _execute_1 = js_primary();
234    }
235    
236    js_continuos();
237
238    if ( _execute_2 == false ) {
239        if ( _execute_1 == false ){
240            _execute_1 = js_primary();
241        }
242        _execute_2 = js_secondary();
243
244    }
245
246});
247 }
248    
249}
250
251
252function js_primary(){
253    print_debug("js_primary_START");
254
255     _execute_1 = true;
256     
257    if(typeof jQuery == "function"){
258        load_ta_featured(function(){
259
260            load_ta_images();
261
262        });  
263    
264
265        
266     
267        return _execute_1;
268    }
269}
270
271function js_secondary(){
272    print_debug("js_secondary_START");
273        
274    _execute_2 = true;        
275    
276    switch_ta_scripts();
277    load_high_res_images();
278    
279    
280    
281    setTimeout(function(){
282		
283		 print_debug("js_secondary_START_TIMEOUT");
284		
285		load_ta_iframes();
286    
287    	load_ta_async_content();
288    
289    
290	}, 3000);
291	
292
293    
294        
295    return _execute_2;
296}
297
298function js_continuos(){
299
300    load_ta_images();
301    load_high_res_images();
302    if(_execute_2 == true){
303        load_ta_iframes();
304    }
305    
306    
307}
308
309
310///////////////////////////////////////////////////////////////////////////////////////
311
312function replaceToWebP(srctoReplace) {
313    print_debug("replaceToWebP_START");
314    
315    if (isWebPSupported && srctoReplace != null) {
316
317      var _parts = srctoReplace.split(",");  
318      var _webpUrls = _parts.map(function(part) {
319
320        var _url = part.trim().split(" ");
321        var _pathname = _url[0];  
322
323        if (_pathname.includes("/wp-content/themes/")) {
324            return part.trim();
325        }
326        
327        var isRelative = !_pathname.startsWith("http://") && !_pathname.startsWith("https://");
328        var isSameDomain = _pathname.startsWith(window.location.origin);
329
330        if (!(isRelative || isSameDomain)) {
331            return part.trim();
332        }
333
334        var _extension = _pathname.split(/[#?]/)[0].split('.').pop().trim();
335
336        if (_extension.endsWith(".webp")) {
337
338            return part.trim();
339
340        } else {
341
342            var supportedExtensions = ["jpg", "jpeg", "png"];
343
344            if (supportedExtensions.indexOf(_extension) !== -1) {
345
346                var _webpUrl = _pathname + '.webp';
347
348                return part.replace(_pathname, _webpUrl);
349
350            } else {
351
352                return part.trim();
353
354            }
355
356          }
357
358      });
359  
360      var _result = _webpUrls.join(", ");
361  
362      return _result;
363
364    }
365  
366    return srctoReplace;
367
368}
369  
370
371function load_ta_async_content(){
372    print_debug("load_ta_async_content_START");
373    
374
375    jQuery(function(){
376
377        jQuery('[data-ta-iframe]').each(function() {
378
379            var _this = jQuery(this);
380            
381            
382            
383            if(_this[0].hasAttribute("data-ta-iframe_loaded") ){
384                
385            }
386            else{
387            
388            _path = _this.attr("data-ta-iframe");
389
390            if ( _path != undefined ) {
391                
392                _this[0].setAttribute("data-ta-iframe_loaded","true");
393                
394                jQuery.ajax({cache:true,url: _path, success: function(result){
395                
396                    // There is only one part, we put the content directly inside ta_async tag
397                    _this.html(result);
398
399                }});
400                
401
402            }
403            }
404
405        });
406
407        jQuery('ta_async_content').each(function() {
408
409            var _this = jQuery(this);
410            
411            
412            
413
414               
415
416            if ( is_Mobile && _this.hasClass("disable-mobile") ) {
417
418                return false;
419
420            }
421
422            if ( is_Tablet && _this.hasClass("disable-tablet") ) {
423
424                return false;
425
426            }
427
428            if ( is_Desktop && _this.hasClass("disable-desktop") ) {
429
430                return false;
431
432            }
433            
434            if(_this[0].hasAttribute("ta_async_content_loaded") ){
435                
436            }
437            else{
438                _path = _this.attr("path");
439
440
441            if ( _path != undefined ) {
442                _this[0].setAttribute("ta_async_content_loaded","true");
443                jQuery.ajax({cache:true,url: _path, success: function(result){
444
445                    // Split the result in parts
446                    var _resultArr = result.split("||TA_CACHE_SPLIT||");
447                                        
448                    // If the are more than one part we need to loop through the tags 
449                    // <ta_async_partial> and replace it with the part
450                    if ( _resultArr.length > 1 ) {
451
452                        var _partials = _this.find("ta_async_partial");
453
454                        _partials.each(function( _index, _val ){
455
456                            $(this).html( _resultArr[_index] );
457
458                        });
459
460                    } else {
461
462                        // There is only one part, we put the content directly inside ta_async tag
463                        _this.html(result);
464
465                    }
466
467                }});
468
469            }
470        }
471
472        });
473
474    });
475
476}
477
478function load_ta_featured( _callback = "" ) {
479    print_debug("load_ta_featured_START");
480    jQuery(function(){
481
482        jQuery('img:not([sizes_adapted])').each(function(){
483
484            if (typeof this.getAttribute("ta-srcset") != undefined){
485
486                imageLoaded( this );
487                
488                /*
489                myAttribute=this.getAttribute("ta-srcset");
490
491                if(typeof myAttribute == "string"){
492
493                    this.setAttribute("srcset", replaceToWebP(myAttribute));
494                    
495                }
496                */
497                
498            }
499
500            if ( typeof _callback == "function" ){
501            
502                _callback.apply(this);                
503
504            }
505
506        });
507
508    });
509
510}
511
512function load_ta_images(){
513    print_debug("load_ta_images_START");
514    all_images=jQuery('img[ta-src],img[ta-srcset],img[srcset][ta-src-delete]');
515
516    for(i=0;i<all_images.length;i++){
517
518        if ( elementInViewport( all_images[i] ) ) {
519		
520		/*
521             if(all_images[i].getAttribute("srcset") != null && all_images[i].getAttribute("ta-src-delete") != null ) {
522                img_post.setAttribute("srcset",replaceToWebP( all_images[i].getAttribute("srcset")));
523            }
524		*/
525            myAttribute=all_images[i].getAttribute("ta-src");
526            myAttribute = replaceToWebP(myAttribute);
527
528            if(typeof myAttribute == "string"){
529
530                all_images[i].setAttribute("src",myAttribute);
531                all_images[i].removeAttribute("ta-src");
532                
533            }
534
535            myAttribute=all_images[i].getAttribute("ta-srcset");
536
537            if(typeof myAttribute == "string"){
538
539                all_images[i].setAttribute("srcset",replaceToWebP(myAttribute));
540                all_images[i].removeAttribute("ta-srcset");
541
542            }
543            
544        }
545
546    }
547   
548    
549}
550
551function load_ta_iframes(){
552    print_debug("load_ta_iframes_START");
553    jQuery(document).ready(function() {
554
555        jQuery('iframe').each(function() {
556
557            if (typeof this.getAttribute("src") !== "string"){
558
559                if ( elementInViewport( this ) ) {
560
561                    myAttribute=this.getAttribute("ta-src");
562
563                    if(typeof myAttribute == "string"){
564
565                        this.setAttribute("src",myAttribute);
566
567                    }
568
569                    myAttribute=this.getAttribute("ta-srcset");
570
571                    if(typeof myAttribute == "string"){
572
573                        this.setAttribute("srcset",myAttribute);
574
575                    }
576
577                }
578
579            }
580            
581        });
582
583    }) ;
584
585    
586}
587
588function load_high_res_images(){
589    print_debug("load_high_res_images_START");
590   
591    let all_images=jQuery('img:not([sizes_adapted])');
592
593    if(all_images[0] !== "undefiend"){
594
595        for(let i=0;i<all_images.length;i++){
596
597            if ( elementInViewport( all_images[i] ) && typeof all_images !== "undefiend") {
598
599                imageLoaded( all_images[i] );
600
601            }
602
603        }
604
605    }
606
607}
608
609function switch_ta_scripts(){
610    print_debug("switch_ta_scripts_START");
611    jQuery("link").each(function(){
612
613        var attr = jQuery(this).attr('ta-script-href');
614
615        if (typeof attr !== 'undefined' && attr !== false) {
616
617            jQuery(this).attr("href", attr);
618
619        }
620
621    });
622
623    jQuery("script").each(function(){
624
625        var attr = jQuery(this).attr('ta-script-src');
626        var type = jQuery(this).attr('ta-script-type');
627
628        if (typeof attr !== 'undefined' && attr !== false) {
629
630            jQuery(this).attr("src", attr);
631
632        }
633
634        if (typeof type !== 'undefined' && type !== false) {
635
636            var _script = jQuery("<script>").text( jQuery(this).text() ).appendTo("body");
637
638        }
639
640    });
641
642    jQuery("iframe").each(function(){
643
644        var attr = jQuery(this).attr('ta-script-src');
645
646        if (typeof attr !== 'undefined' && attr !== false) {
647
648            jQuery(this).attr("src", attr);
649
650        }
651
652    });
653
654    
655}
656
657function imageLoaded( img_post ) {
658    print_debug("imageLoaded_START");
659    // Run onload code.         
660    var sizes_adapted_value = "ERROR sizes";
661    //console.log("Attribute");
662    //console.log(img_post.getAttribute("sizes_adapted"));
663    if(img_post.getAttribute("sizes_adapted") == null){  
664
665        if(typeof debug_image_resolution=='undefined' || debug_image_resolution!=true){
666           
667            let sizes = [];
668            
669            if(img_post.getAttribute("srcset") == null && img_post.getAttribute("ta-srcset") != null){
670
671                img_post.setAttribute("srcset", replaceToWebP( img_post.getAttribute("ta-srcset")));
672                
673            }
674            //img_post.setAttribute("srcset",replaceToWebP( img_post.getAttribute("srcset")));
675            
676            if(img_post.getAttribute("srcset") != null && img_post.getAttribute("srcset").match(/[\d.]+(?=w)/g) != null){
677                
678                
679
680                img_post.getAttribute("srcset").match(/[\d.]+(?=w)/g).forEach(str => {
681
682                    sizes.push(Number(str));
683
684                });
685
686                for(let i=0;i<sizes.length;i++){
687
688                    if(isNaN(sizes[i])){
689
690                        sizes[i] = 0;
691                        
692                    }
693
694                }
695                
696                sizes.sort(function(a, b) {
697
698                    return a - b;
699
700                });
701
702                let acceptable_scale_factor=1;
703                let width = jQuery(img_post).width() * acceptable_scale_factor;
704
705                if (jQuery(img_post).parents('.TA_gallery_element').length > 0 && jQuery(img_post).parents('.carousel').length > 0){
706
707                    width = -1;
708
709                }
710
711                if(width == 0){
712
713                    sizes_adapted_value = "ERROR sizes: Width=0";
714
715                } else {
716                    
717                    
718                    if(width == -1 && !jQuery(img_post).closest().is(':first-child')){
719
720                        sizes_of_first_child = jQuery(img_post).parents('.carousel').first().attr('sizes');
721                        img_post.setAttribute("sizes", sizes_of_first_child);
722                        sizes_adapted_value = "true";
723
724                    } else {
725
726                        current_val=-1;
727                        
728                        for(i=0;i<sizes.length;i++){
729
730                            if(sizes[i] > width){
731
732                                current_val=i;
733                                break;
734
735                            } else {
736
737                                if(i == sizes.length-1){
738
739                                    current_val = i;
740                                    console.log("max image size reached");
741
742                                } else {
743                                    //donothing
744                                }
745
746                            }
747
748                        }
749                        //console.log("sizes[current_val]");
750                        //console.log(sizes[current_val]);
751
752                        if(current_val != -1){
753                            //console.log(isWebPSupported);
754                            //img_post.setAttribute("sizes", sizes[current_val] + "px"); // window.devicePixelRatio is 3 on phone
755                            if(window.devicePixelRatio > 1){
756                                sizes_corrector = window.devicePixelRatio / 2;
757                            }
758                            else{
759                                sizes_corrector = 1;
760                            }
761
762                           size_to_right=parseInt(width / sizes_corrector);
763                           if (size_to_right < 300){
764                               if(is_Mobile){
765                                   size_to_right=300 / (window.devicePixelRatio / 2);
766                               }
767                               else{
768                                size_to_right=300;
769                               }
770                            }
771                            
772                            Real_ratio = img_post.height / img_post.width ;
773                            Image_ratio = parseInt(img_post.style.maxHeight) / parseInt(img_post.style.maxWidth);
774                            if((Real_ratio - Image_ratio) >= 0.3){
775                                size_to_right = parseInt(size_to_right * Real_ratio / Image_ratio);
776                            }
777                            
778                            
779                            //console.log(size_to_right);
780                           img_post.setAttribute("sizes", parseInt(size_to_right) + "px");
781                            
782                            sizes_adapted_value = "true";
783
784                        } else {
785                            
786                            sizes_adapted_value = "ERROR sizes: current_val = -1";
787
788                        }
789
790                    }
791
792                }
793                    
794            } else {
795
796                sizes_adapted_value = "ERROR sizes: srcsetNotDefined";
797
798            }
799        } else {
800
801            sizes_adapted_value = "ERROR sizes: undefinedB";
802
803        }
804
805        img_post.setAttribute("sizes_adapted", sizes_adapted_value);
806
807    }
808    /*
809    else if (img_post == jQuery(".article_0 .feature_image img")[0]){ //LCP Load preloaded image  
810        if(img_post.getAttribute("srcset") == null && img_post.getAttribute("ta-srcset") != null){
811
812                img_post.setAttribute("srcset", replaceToWebP( img_post.getAttribute("ta-srcset")));
813                img_post.setAttribute("sizes", "(max-width:500px) 60vw, 40vw");
814            }
815        
816       
817    }
818    */
819}
820
821
822
823function elementInViewport(myElement) {
824    print_debug("elementInViewport_START");
825    return true;
826    var bounding = myElement.getBoundingClientRect();
827    var myElementHeight = myElement.offsetHeight;
828    var myElementWidth = myElement.offsetWidth;    
829
830    if (bounding.top >= -myElementHeight 
831        && bounding.left >= -myElementWidth
832        && bounding.right <= (window.innerWidth || document.documentElement.clientWidth) + myElementWidth
833        && bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) + myElementHeight) {
834
835        return true;
836
837    } else {
838
839        return false;
840
841    }
842    
843}
844
845
846
847
848function print_debug(mystring){
849   if(true){
850    console.log(mystring);
851}
852}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.