lulzScript

IB Conglomerator 1.2.0 [2017-10-24]

Jul 11th, 2012
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           IB Conglomerator
  3. // @namespace      http://lulz.net/
  4. // @version        1.2.0
  5. // @description    Asynchronously extracts all submissions from an InkBunny main gallery or scraps gallery and displays them as a list of links in a new window.  A button "Conglomerate" is inserted into the gallery page which activates the script. Install guide, updates, changelog and other scripts @ http://pastebin.com/u/lulzScript
  6. // @include        http*://inkbunny.net/gallery/*
  7. // @run-at document-start
  8. // ==/UserScript==
  9.  
  10.  
  11. //Retry constant
  12. //Default is up to 3 retries for a page to load.
  13. var congRetries = 3;
  14.  
  15. //When each request either loaded or gave an error, the rCount should be back at 0.
  16. //Pending downlaods lead to rCount > 0
  17. function checkIfDone()
  18. {
  19.   if (rCount == 0)
  20.     lStatus.innerHTML = '<b> Done!</b>';
  21. }
  22.  
  23.  
  24. //Function called when a sub-submission page was successfully loaded.
  25. //req = XMLHttpRequest object of the loaded page
  26. //linkObj = link object within the new link window
  27. function onSubSubm (req, linkObj)
  28. {
  29.   //Try to extract download link from sub-submission page. Try full image first
  30.   var subSubmFile_ = req.responseText.match(/href *= *[\"\'](https?:\/\/.*?\.ib\.metapix\.net\/files\/full\/[^\"\']*)(?:\"|\')/i);
  31.   if ( !subSubmFile_ )
  32.     //If full image is not available, try screen image
  33.     var subSubmFile_ = req.responseText.match(/src *= *[\"\'](https?:\/\/.*?\.ib\.metapix\.net\/files\/screen\/[^\"\']*)(?:\"|\')/i);
  34.  
  35.   if ( subSubmFile_ )
  36.   {
  37.     //Insert extracted download link into link list
  38.     //Link displayed as text
  39.     linkObj.innerHTML = subSubmFile_[1];
  40.     //Also set the href attribut, to make the link clickable
  41.     linkObj.setAttribute('href', subSubmFile_[1]);
  42.   }
  43.   else
  44.     //If no link was found, display message.
  45.     linkObj.innerHTML = 'No link found';
  46.  
  47.  
  48.   //Request finished successfully. Decrease request counter and check if rCount = 0.
  49.   rCount--;
  50.   checkIfDone();
  51.  
  52. }
  53.  
  54. //Function called when a sub-submission page failed to load.
  55. //req = XMLHttpRequest object of the failed page
  56. //linkObj = link object within the new link window
  57. function onSubSubmErr (req, linkObj)
  58. {
  59.   //Display "Error" in the linklist if the link couldn't be extracted.
  60.   linkObj.innerHTML = 'Error';
  61.  
  62.   //Request finished with error. Decrease request counter and check if rCount = 0.
  63.   rCount--;
  64.   checkIfDone();
  65. }
  66.  
  67. //Loads a sub-submission page asynchronously.
  68. //subSubmURL = Full URL of sub-submission.
  69. //linkObj = link object within the new link window
  70. //retry = counter that indicates the current number of retries.
  71. //After [congRetries] failed retries, the function fails.
  72. function getSubSubm (subSubmURL, linkObj, retry)
  73. {
  74.   var req = new XMLHttpRequest();
  75.   //Prepare request with full URL, containing the desired page number
  76.   req.open('GET', subSubmURL, true);
  77.   //Function to call when loading state of page changes
  78.   req.onreadystatechange = function (evt)
  79.   {
  80.     //Check if page was loaded (or not)
  81.     if (req.readyState == 4)
  82.     {
  83.       if (req.status == 200)
  84.         //If page was sucessfully loaded
  85.         onSubSubm (req, linkObj);
  86.       else
  87.       {
  88.         //If page failed to load, retry up to [congRetries] times. Otherwise call error handler.
  89.         if ( retry < congRetries )
  90.           getSubSubm (subSubmURL, linkObj, retry+1);
  91.         else
  92.           onSubSubmErr (req, linkObj);
  93.       }
  94.     }
  95.   };
  96.      
  97.   //Sends request and starts loading of the page.
  98.   req.send(null);
  99. }
  100.  
  101.  
  102. //Adds new Link in the link window
  103. //bspan = base span that contains all links of a submission and its sub-submissions
  104. //no = Line number of the link
  105. function addNewLink(bspan, no)
  106. {
  107.   var span = congLinkWin.document.createElement('span');
  108.   span.innerHTML = '</br><b>' + no + ': </b>';
  109.   var firstLink = congLinkWin.document.createElement('a');
  110.   //This attribute will open a new page if the link is clicked in the conglomerator window.
  111.   firstLink.setAttribute('target', '_blank');
  112.   firstLink.innerHTML = 'loading...';
  113.   span.insertBefore(firstLink, null);
  114.   bspan.insertBefore(span, null);
  115.  
  116.   return(firstLink)
  117. }
  118.  
  119.  
  120. //Function called when a submission page was successfully loaded.
  121. //req = XMLHttpRequest object of the loaded page
  122. //linkObj = link object within the new link window
  123. function onSubm (req, linkObj, linkNo, submID)
  124. {
  125.   //Try to extract download link from submission page. Try full image first
  126.   var submFile_ = req.responseText.match(/href *= *[\"\'](https?:\/\/.*?\.ib\.metapix\.net\/files\/full\/[^\"\']*)(?:\"|\')/i);
  127.   if ( !submFile_ )
  128.     //If full image is not available, try screen image
  129.     var submFile_ = req.responseText.match(/src *= *[\"\'](https?:\/\/.*?\.ib\.metapix\.net\/files\/screen\/[^\"\']*)(?:\"|\')/i);
  130.  
  131.   if ( submFile_ )
  132.   {
  133.     //Insert extracted download link into link list
  134.     //Link displayed as text
  135.     linkObj.innerHTML = submFile_[1];
  136.     //Also set the href attribut, to make the link clickable
  137.     linkObj.setAttribute('href', submFile_[1]);
  138.   }
  139.   else
  140.     //If no link was found, display message.
  141.     linkObj.innerHTML = 'No link found';
  142.  
  143.  
  144.   //Check if and how many sub-submission pages are available
  145.   //var subSubmCount_ = req.responseText.match(/(\d+) files in this submission/i);
  146.   var subSubmCount_ = req.responseText.match(/<span.*?1 of (\d+).*?<\/span>/);
  147.  
  148.   if ( subSubmCount_ )
  149.   {
  150.     var subSubmCount = parseInt(subSubmCount_[1]);
  151.    
  152.     //Get all sub-submissions
  153.     for (var i=2; i <= subSubmCount; i++)
  154.     {
  155.       //Build sub-submission URL with current sub-submission number
  156.       var subSubmURL = '/s/' + submID + '-p' + i;
  157.        
  158.       //Add new line for sub-submission in link window and set status to "loading..."
  159.       var linkObj = addNewLink (linkObj.parentNode.parentNode, linkNo + '.' + i);
  160.  
  161.       //New request for sub-submission. Increase request counter.
  162.       rCount++;
  163.  
  164.       console.log(ibBaseURL + subSubmURL);
  165.      
  166.       getSubSubm (ibBaseURL + subSubmURL, linkObj, 0);
  167.     }
  168.   }
  169.  
  170.   //Request finished successfully. Decrease request counter and check if rCount = 0.
  171.   rCount--;
  172.   checkIfDone();
  173. }
  174.  
  175. //Function called when a submission page failed to load.
  176. //req = XMLHttpRequest object of the failed page
  177. //linkObj = link object within the new link window
  178. function onSubmErr (req, linkObj, linkNo)
  179. {
  180.   //Display "Error" in the linklist if the link couldn't be extracted.
  181.   linkObj.innerHTML = 'Error';
  182.  
  183.   //Request finished with error. Decrease request counter and check if rCount = 0.
  184.   rCount--;
  185.   checkIfDone();
  186. }
  187.  
  188. //Loads a submission page asynchronously.
  189. //submURL = Full URL of submission.
  190. //linkObj = link object within the new link window
  191. //retry = counter that indicates the current number of retries.
  192. //After [congRetries] failed retries, the function fails.
  193. function getSubm (submURL, linkObj, linkNo, retry, submID)
  194. {
  195.   var req = new XMLHttpRequest();
  196.   //Prepare request with full URL, containing the desired page number
  197.   req.open('GET', submURL, true);
  198.   //Function to call when loading state of page changes
  199.   req.onreadystatechange = function (evt)
  200.   {
  201.     //Check if page was loaded (or not)
  202.     if (req.readyState == 4)
  203.     {
  204.       if (req.status == 200)
  205.         //If page was sucessfully loaded
  206.         onSubm (req, linkObj, linkNo, submID);
  207.       else
  208.       {
  209.         //If page failed to load, retry up to [congRetries] times. Otherwise call error handler.
  210.         if ( retry < congRetries )
  211.           getSubm (submURL, linkObj, linkNo, retry+1, submID);
  212.         else
  213.           onSubmErr (req, linkObj, linkNo);
  214.       }
  215.     }
  216.   };
  217.      
  218.   //Sends request and starts loading of the page.
  219.   req.send(null);
  220. }
  221.  
  222.  
  223. //Function called when a submission list page was successfully loaded.
  224. //req = XMLHttpRequest object of the loaded page
  225. //no = number of loaded page
  226. function onSubmList (req, no)
  227. {
  228.   //Extract all submission links
  229.   var rawSubmLinks = req.responseText.match(/\/s\/\d*/gi);
  230.  
  231.  
  232.   //Continue as long as there are submission links found
  233.   if ( rawSubmLinks )
  234.   {
  235.     //Display page number
  236.     var span = congLinkWin.document.createElement('span');
  237.     span.innerHTML = '</br></br><b>Page ' + no + '</b><br/>';
  238.     congLinkWin.document.body.insertBefore(span, null);
  239.  
  240.     //Iterate through all found links.
  241.     for (var i=0; i < rawSubmLinks.length; i++)
  242.     {
  243.       //Make full URL of submission page from link.
  244.       var submURL = ibBaseURL + rawSubmLinks[i];
  245.      
  246.       var submID_ = rawSubmLinks[i].match(/\/(\d*)$/);
  247.  
  248.       if (submID_)
  249.       {
  250.         //Add new line for submission in link window and set status to "loading..."
  251.         var span = congLinkWin.document.createElement('span');
  252.         congLinkWin.document.body.insertBefore(span, null);
  253.         var linkObj = addNewLink (span, sCount);
  254.  
  255.         //New request. Increase request counter.
  256.         rCount++;
  257.         getSubm (submURL, linkObj, sCount, 0, submID_[1]);
  258.  
  259.         //Increase submission counter
  260.         sCount++;
  261.       }
  262.  
  263.     }
  264.   }
  265.  
  266.   //If there's a "Next Page" link on the page, there are more pages to be loaded.
  267.   if ( req.responseText.search('>Next Page</span>') != -1 )
  268.   {
  269.     //New request. Increase request counter.
  270.     rCount++;
  271.     //Load next submission list page
  272.     getSubmList (submBaseURL1, submBaseURL2, no+1, 0);
  273.   }
  274.   else
  275.   {
  276.     //There are no more pages. Display message.
  277.     var span = congLinkWin.document.createElement('span');
  278.     span.innerHTML = '</br></br><b>No more pages!<b>';
  279.     congLinkWin.document.body.insertBefore(span, null);
  280.   }
  281.    
  282.   //Request finished successfully. Decrease request counter and check if rCount = 0.
  283.   rCount--;
  284.   checkIfDone();
  285.  
  286. }
  287.  
  288. //Function called when a submission list page failed to load.
  289. //req = XMLHttpRequest object of the failed page
  290. //no = number of loaded page
  291. function onSubmListErr (req, no)
  292. {
  293.   //Page could not be loaded. Display Error message.
  294.   var span = congLinkWin.document.createElement('span');
  295.   span.innerHTML = '</br></br><b>Error - cannot load page ' + no + '!<br/>Further conglomeration stoppped.</b><br/>';
  296.   congLinkWin.document.body.insertBefore(span, null);
  297.  
  298.   //Request finished with error. Decrease request counter and check if rCount = 0.
  299.   rCount--;
  300.   checkIfDone();
  301. }
  302.  
  303. //Loads a submission list page from the gallery/scraps collection asynchronously.
  304. //baseURL = Base URL of gallery/scraps collection page.
  305. //no = Nummer of list page to load
  306. //retry = counter that indicates the current number of retries.
  307. //After [congRetries] failed retries, the function fails.
  308. function getSubmList (baseURL1, baseURL2, no, retry)
  309. {
  310.   var req = new XMLHttpRequest();
  311.   //Prepare request with full URL, containing the desired page number
  312.   req.open('GET', baseURL1 + no + baseURL2, true);
  313.   //Function to call when loading state of page changes
  314.   req.onreadystatechange = function (evt)
  315.   {
  316.     //Check if page was loaded (or not)
  317.     if (req.readyState == 4)
  318.     {
  319.       if (req.status == 200)
  320.         //If page was sucessfully loaded
  321.         onSubmList (req, no);
  322.       else
  323.       {
  324.         //If page failed to load, retry up to [congRetries] times. Otherwise call error handler.
  325.         if ( retry < congRetries )
  326.           getSubmList (baseURL1, baseURL2, no, retry+1);
  327.         else
  328.           onSubmListErr (req, no);
  329.       }
  330.     }
  331.   };
  332.      
  333.   //Sends request and starts loading of the page.
  334.   req.send(null);
  335. }
  336.  
  337. //Global variables:
  338.  
  339. //Base URL of IB, including protocol and subdomain
  340. var ibBaseURL;
  341. //Base URL parts of gallery
  342. var submBaseURL1, submBaseURL2;
  343. //Reference to new link window
  344. var congLinkWin;
  345. //Counter for submission index
  346. var sCount;
  347. //Name of IB user whose galery/scraps are being conglomerated
  348. var ibUsername;
  349. //Source of submission ("gallery" or "scraps")
  350. var lStatus;
  351. //Request counter. Used to determine when all submissions are loaded.
  352. var rCount;
  353.  
  354. //Main script function. Called when conglomerator button is clicked
  355. function congStart(e)
  356. {
  357.   //Prevents the form's default action for a button
  358.   e.preventDefault();
  359.  
  360.   //Get IB base URL (including protocol and a possible subdomain)
  361.   var ibBaseURL_ = document.location.href.match(/(.*inkbunny\.net)\/gallery\/([^\/]*?)\/[^\/]*?\/([^$]*?)$/);
  362.   if ( ibBaseURL_ )
  363.   {  
  364.     ibBaseURL = ibBaseURL_[1];
  365.     var userID = ibBaseURL_[2];
  366.     var rID =  ibBaseURL_[3];
  367.  
  368.    
  369.     submBaseURL1 = ibBaseURL + '/gallery/' + userID + '/';
  370.     submBaseURL2 = '/' + rID;
  371.  
  372.     //Extract user name from page
  373.     var uCls = document.getElementsByClassName('widget_userNameSmall');
  374.     for (var i=uCls.length-1; i >= 0; i--)
  375.     {
  376.       if ( uCls[i].nodeName == 'A')
  377.       {
  378.         ibUsername = uCls[i].innerHTML;
  379.         break;
  380.       }
  381.     }
  382.        
  383.     if ( ibUsername )
  384.     {
  385.       //Open new window for extracted submission links
  386.       congLinkWin = window.open();
  387.       if ( congLinkWin )
  388.       {
  389.         //Set title of new window
  390.         congLinkWin.document.title = 'IB conglomerator';
  391.         //Add title to link window
  392.         var span = congLinkWin.document.createElement('span');
  393.         span.innerHTML = '<b>List of ' + ibUsername + '\'s gallery:<b>';
  394.         congLinkWin.document.body.insertBefore(span, null);
  395.    
  396.         //Add status message to link Window
  397.         lStatus = congLinkWin.document.createElement('span');
  398.         lStatus.innerHTML = '<b> Loading...</b>';
  399.         congLinkWin.document.body.insertBefore(lStatus, null);
  400.  
  401.         //Reset submission and request counter
  402.         sCount = 1;
  403.         rCount = 1;
  404.  
  405.         //At this point ibBaseURL and submBaseURL both matched correctly.
  406.         //Load first submission list page
  407.         getSubmList (submBaseURL1, submBaseURL2, 1, 0);
  408.       }
  409.     }
  410.   }
  411. }
  412.  
  413. function insertButton()
  414. {
  415.   //Find location of gallery controls
  416.   var spans = document.getElementsByTagName('span');
  417.   for (var i=0; i < spans.length; i++)
  418.   {
  419.     if (spans[i].innerHTML == 'thumbnail size - ')
  420.     {
  421.       var browsebar = spans[i].parentNode.parentNode.parentNode;
  422.       break;
  423.     }      
  424.   }
  425.  
  426.   //If found, insert conglomerator button
  427.   if ( browsebar )
  428.   {
  429.     browsebar.insertBefore(congButton, browsebar.firstChild);
  430.     buttonInserted = true;
  431.   }
  432. }
  433.  
  434. //Flag that is later set to true if button was inserted.
  435. var buttonInserted = false;
  436.  
  437. //Create button for conglomerator script
  438. var congButton = document.createElement('button');
  439. //Set button style
  440. congButton.style.backgroundColor="#8AE234";
  441. congButton.style.borderStyle="solid";
  442. congButton.style.borderRadius="5px";
  443. congButton.style.marginLeft="12px";
  444.  
  445. //Set button text
  446. congButton.innerHTML = 'Conglomerate';
  447. //Set function to call on a click
  448. congButton.addEventListener ('click', congStart, false);
  449.  
  450. //Try to insert button into page if page is already available at this time
  451. insertButton();
  452.  
  453. //Function onDCL is called when DOM tree has completely been loaded.
  454. //This is the savest way to start a script.
  455. document.addEventListener ('DOMContentLoaded',
  456.   function onDCL(e)
  457.   {
  458.     //If the button wasn't inserted before because the page wasn't
  459.     //available at that time, it will be inserted now.
  460.     if ( !buttonInserted )
  461.       insertButton();
  462.    
  463.   }, false
  464. );
  465.  
  466.  
  467. //Additional, faster way for Opera to insert and display the conglomerator button.
  468. //Inline scripts sometimes seem to block the DOMContentLoaded event until they have executed completely.
  469. //This code will insert the button as soon as (but before) an inline script is called
  470. //
  471. //Check if the opera object exists. If yes, this script runs in an opera browser.
  472. if (window.opera)
  473. {
  474.   window.opera.addEventListener ('BeforeScript',
  475.     function onDCL(e)
  476.     {
  477.       //Try to insert button, if it hasn't been inserted yet.
  478.       if ( !buttonInserted )
  479.         insertButton();
  480.  
  481.       //Check if the button was inserted somehow (either by this function or by the DOMContentLoaded event listener above)
  482.       if (buttonInserted)
  483.       {
  484.         //Remove this event listener again if button was inserted successfully.
  485.         window.opera.removeEventListener ('BeforeScript', onDCL, false);
  486.       }
  487.  
  488.     }, false
  489.   );
  490. }
Advertisement
Add Comment
Please, Sign In to add comment