Advertisement
martixy

Webcomic Reader 2

Aug 10th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name       Webcomic Reader 2
  3. // @namespace  http://to.do.later
  4. // @version    0.1
  5. // @description  WIP
  6. /// @match      http://*/*
  7. // @include    http://*.keenspot.com/*
  8. // @include    http://www.interrobangstudios.com/potluck*
  9. // @include    http://satwcomic.com/*
  10. // @include    http://www.twogag.com/*
  11. // @include    http://www.vgcats.com/comics/*
  12. // @require    http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
  13. // @copyright  2012+, martixy
  14. // ==/UserScript==
  15.  
  16. (function()
  17. {
  18.     //Init page images first
  19.     var iterator = -1;
  20.  
  21.     (function()
  22.     {
  23.         var boo = $("img");
  24.         console.log("There are " + boo.length + " images on this page.");
  25.         iterator = boo.length;
  26.        
  27.         setTimeout(function() {
  28.             if (iterator > 0)
  29.             {
  30.                 console.log("Timed out while waiting for " + iterator + " items.");
  31.                 iterator=0;
  32.             }
  33.         }, 5000);
  34.        
  35.         for(var i=0; i<boo.length; i++)
  36.         {
  37.             if( boo[i].clientWidth == 0 && boo[i].clientHeight == 0)//Register the load events for unloaded images.
  38.             {
  39.                 $(boo[i])
  40.                 .load( function() {
  41.                     iterator--;
  42.                     console.log((boo.length - iterator) + " out of " + boo.length + " images loaded");
  43.                     console.log("Image: " + this.src + "; Dimensions: " + $(this).width() + "*" + $(this).height());
  44.                 })
  45.                 .error(function() {
  46.                     iterator--;
  47.                     console.log("Error loading image " + (boo.length - iterator) + " out of " + boo.length + ", moving on...");
  48.                 });
  49.             }
  50.             else
  51.             {
  52.                 iterator--;
  53.                 console.log((boo.length - iterator) + " out of " + boo.length + " images loaded(no event)");
  54.                 console.log("Image: " + boo[i].src + "; Dimensions: " + boo[i].clientWidth + "*" + boo[i].clientHeight);
  55.             }
  56.         }
  57.     })();
  58.    
  59.    
  60.     function getLargestImage()
  61.     {
  62.         var max = 0;
  63.         var images = $("img");
  64.         console.log("Images on page:" + images.length);
  65.         var dimensions = 0;
  66.         for(var i=0; i<images.length; i++)
  67.         {
  68.             dimensions = images[i].clientWidth * images[i].clientHeight;
  69.             if(dimensions > (images[max].clientWidth * images[max].clientHeight))
  70.             {
  71.                 max = i;
  72.                 console.log("New biggest image: " + images[i].src + "[" + images[i].clientWidth + "*" + images[i].clientHeight + "]");
  73.             }
  74.         }
  75.         return images[max];
  76.     }
  77.    
  78.    
  79.     //continue... fuck JS
  80.     function getLargestImageCallback(){
  81.     setTimeout(function() { if (iterator != 0) getLargestImageCallback();
  82.         else {
  83.     var LargestImage = getLargestImage();
  84.     $(LargestImage).css('border', '10px solid #FF0000');
  85.     console.log("height:" + LargestImage.clientHeight);
  86.     console.log("width:" + LargestImage.clientWidth);
  87.        
  88.     function loadNext()
  89.     {
  90.         // =============================
  91.         //  ASSUMPTION(May not be true)
  92.         // =============================
  93.         // Get its location, without the file name(the common part of the all locations)
  94.         var imageLocation = LargestImage.outerHTML.match(/src="(.*\/).*?(?:png|jpg|gif)"/i)[1];
  95.         console.log("imageLocation as reported: " + imageLocation);
  96.        
  97.         // Get the link to the next page
  98.         var next
  99.         $('a').each( function(index) {
  100.             if ($(this).prop("outerHTML").toLowerCase().indexOf("next") >= 0)
  101.                 next = $(this);
  102.         });
  103.         console.log(next);
  104.        
  105.        
  106.         // AJAX to get next page, and find the image location by the common element in its address
  107.         $.ajax({
  108.             url: next[0].href,
  109.             complete: function(jqXHR, status) {
  110.                 console.log("XHR status: " + status);
  111.                 //$(getLargestImage()).after('<textarea rows="2" cols="20">' + jqXHR.responseText + '</textarea>');
  112.                 var regex = new RegExp("<img.*?src=\"(" + imageLocation + ")(?:.*\.(?:png|jpg|gif)).*?>", "gi");
  113.                 var nextImage = regex.exec(jqXHR.responseText);
  114.                 console.log("Regex matches: " + nextImage.length);
  115.                 console.log("Next Image address: " + $(nextImage[0]).attr('src'));
  116.                 LargestImage = $(LargestImage).after(nextImage[0].toString());
  117.             },
  118.         });
  119.        
  120.     }
  121.     loadNext();
  122.            
  123.            
  124.     //=========================================================================
  125.         }
  126.         }, 200); // Fuck JS
  127.     }
  128.     getLargestImageCallback();
  129. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement