Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name IB Conglomerator
- // @namespace http://lulz.net/
- // @version 1.2.0
- // @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
- // @include http*://inkbunny.net/gallery/*
- // @run-at document-start
- // ==/UserScript==
- //Retry constant
- //Default is up to 3 retries for a page to load.
- var congRetries = 3;
- //When each request either loaded or gave an error, the rCount should be back at 0.
- //Pending downlaods lead to rCount > 0
- function checkIfDone()
- {
- if (rCount == 0)
- lStatus.innerHTML = '<b> Done!</b>';
- }
- //Function called when a sub-submission page was successfully loaded.
- //req = XMLHttpRequest object of the loaded page
- //linkObj = link object within the new link window
- function onSubSubm (req, linkObj)
- {
- //Try to extract download link from sub-submission page. Try full image first
- var subSubmFile_ = req.responseText.match(/href *= *[\"\'](https?:\/\/.*?\.ib\.metapix\.net\/files\/full\/[^\"\']*)(?:\"|\')/i);
- if ( !subSubmFile_ )
- //If full image is not available, try screen image
- var subSubmFile_ = req.responseText.match(/src *= *[\"\'](https?:\/\/.*?\.ib\.metapix\.net\/files\/screen\/[^\"\']*)(?:\"|\')/i);
- if ( subSubmFile_ )
- {
- //Insert extracted download link into link list
- //Link displayed as text
- linkObj.innerHTML = subSubmFile_[1];
- //Also set the href attribut, to make the link clickable
- linkObj.setAttribute('href', subSubmFile_[1]);
- }
- else
- //If no link was found, display message.
- linkObj.innerHTML = 'No link found';
- //Request finished successfully. Decrease request counter and check if rCount = 0.
- rCount--;
- checkIfDone();
- }
- //Function called when a sub-submission page failed to load.
- //req = XMLHttpRequest object of the failed page
- //linkObj = link object within the new link window
- function onSubSubmErr (req, linkObj)
- {
- //Display "Error" in the linklist if the link couldn't be extracted.
- linkObj.innerHTML = 'Error';
- //Request finished with error. Decrease request counter and check if rCount = 0.
- rCount--;
- checkIfDone();
- }
- //Loads a sub-submission page asynchronously.
- //subSubmURL = Full URL of sub-submission.
- //linkObj = link object within the new link window
- //retry = counter that indicates the current number of retries.
- //After [congRetries] failed retries, the function fails.
- function getSubSubm (subSubmURL, linkObj, retry)
- {
- var req = new XMLHttpRequest();
- //Prepare request with full URL, containing the desired page number
- req.open('GET', subSubmURL, true);
- //Function to call when loading state of page changes
- req.onreadystatechange = function (evt)
- {
- //Check if page was loaded (or not)
- if (req.readyState == 4)
- {
- if (req.status == 200)
- //If page was sucessfully loaded
- onSubSubm (req, linkObj);
- else
- {
- //If page failed to load, retry up to [congRetries] times. Otherwise call error handler.
- if ( retry < congRetries )
- getSubSubm (subSubmURL, linkObj, retry+1);
- else
- onSubSubmErr (req, linkObj);
- }
- }
- };
- //Sends request and starts loading of the page.
- req.send(null);
- }
- //Adds new Link in the link window
- //bspan = base span that contains all links of a submission and its sub-submissions
- //no = Line number of the link
- function addNewLink(bspan, no)
- {
- var span = congLinkWin.document.createElement('span');
- span.innerHTML = '</br><b>' + no + ': </b>';
- var firstLink = congLinkWin.document.createElement('a');
- //This attribute will open a new page if the link is clicked in the conglomerator window.
- firstLink.setAttribute('target', '_blank');
- firstLink.innerHTML = 'loading...';
- span.insertBefore(firstLink, null);
- bspan.insertBefore(span, null);
- return(firstLink)
- }
- //Function called when a submission page was successfully loaded.
- //req = XMLHttpRequest object of the loaded page
- //linkObj = link object within the new link window
- function onSubm (req, linkObj, linkNo, submID)
- {
- //Try to extract download link from submission page. Try full image first
- var submFile_ = req.responseText.match(/href *= *[\"\'](https?:\/\/.*?\.ib\.metapix\.net\/files\/full\/[^\"\']*)(?:\"|\')/i);
- if ( !submFile_ )
- //If full image is not available, try screen image
- var submFile_ = req.responseText.match(/src *= *[\"\'](https?:\/\/.*?\.ib\.metapix\.net\/files\/screen\/[^\"\']*)(?:\"|\')/i);
- if ( submFile_ )
- {
- //Insert extracted download link into link list
- //Link displayed as text
- linkObj.innerHTML = submFile_[1];
- //Also set the href attribut, to make the link clickable
- linkObj.setAttribute('href', submFile_[1]);
- }
- else
- //If no link was found, display message.
- linkObj.innerHTML = 'No link found';
- //Check if and how many sub-submission pages are available
- //var subSubmCount_ = req.responseText.match(/(\d+) files in this submission/i);
- var subSubmCount_ = req.responseText.match(/<span.*?1 of (\d+).*?<\/span>/);
- if ( subSubmCount_ )
- {
- var subSubmCount = parseInt(subSubmCount_[1]);
- //Get all sub-submissions
- for (var i=2; i <= subSubmCount; i++)
- {
- //Build sub-submission URL with current sub-submission number
- var subSubmURL = '/s/' + submID + '-p' + i;
- //Add new line for sub-submission in link window and set status to "loading..."
- var linkObj = addNewLink (linkObj.parentNode.parentNode, linkNo + '.' + i);
- //New request for sub-submission. Increase request counter.
- rCount++;
- console.log(ibBaseURL + subSubmURL);
- getSubSubm (ibBaseURL + subSubmURL, linkObj, 0);
- }
- }
- //Request finished successfully. Decrease request counter and check if rCount = 0.
- rCount--;
- checkIfDone();
- }
- //Function called when a submission page failed to load.
- //req = XMLHttpRequest object of the failed page
- //linkObj = link object within the new link window
- function onSubmErr (req, linkObj, linkNo)
- {
- //Display "Error" in the linklist if the link couldn't be extracted.
- linkObj.innerHTML = 'Error';
- //Request finished with error. Decrease request counter and check if rCount = 0.
- rCount--;
- checkIfDone();
- }
- //Loads a submission page asynchronously.
- //submURL = Full URL of submission.
- //linkObj = link object within the new link window
- //retry = counter that indicates the current number of retries.
- //After [congRetries] failed retries, the function fails.
- function getSubm (submURL, linkObj, linkNo, retry, submID)
- {
- var req = new XMLHttpRequest();
- //Prepare request with full URL, containing the desired page number
- req.open('GET', submURL, true);
- //Function to call when loading state of page changes
- req.onreadystatechange = function (evt)
- {
- //Check if page was loaded (or not)
- if (req.readyState == 4)
- {
- if (req.status == 200)
- //If page was sucessfully loaded
- onSubm (req, linkObj, linkNo, submID);
- else
- {
- //If page failed to load, retry up to [congRetries] times. Otherwise call error handler.
- if ( retry < congRetries )
- getSubm (submURL, linkObj, linkNo, retry+1, submID);
- else
- onSubmErr (req, linkObj, linkNo);
- }
- }
- };
- //Sends request and starts loading of the page.
- req.send(null);
- }
- //Function called when a submission list page was successfully loaded.
- //req = XMLHttpRequest object of the loaded page
- //no = number of loaded page
- function onSubmList (req, no)
- {
- //Extract all submission links
- var rawSubmLinks = req.responseText.match(/\/s\/\d*/gi);
- //Continue as long as there are submission links found
- if ( rawSubmLinks )
- {
- //Display page number
- var span = congLinkWin.document.createElement('span');
- span.innerHTML = '</br></br><b>Page ' + no + '</b><br/>';
- congLinkWin.document.body.insertBefore(span, null);
- //Iterate through all found links.
- for (var i=0; i < rawSubmLinks.length; i++)
- {
- //Make full URL of submission page from link.
- var submURL = ibBaseURL + rawSubmLinks[i];
- var submID_ = rawSubmLinks[i].match(/\/(\d*)$/);
- if (submID_)
- {
- //Add new line for submission in link window and set status to "loading..."
- var span = congLinkWin.document.createElement('span');
- congLinkWin.document.body.insertBefore(span, null);
- var linkObj = addNewLink (span, sCount);
- //New request. Increase request counter.
- rCount++;
- getSubm (submURL, linkObj, sCount, 0, submID_[1]);
- //Increase submission counter
- sCount++;
- }
- }
- }
- //If there's a "Next Page" link on the page, there are more pages to be loaded.
- if ( req.responseText.search('>Next Page</span>') != -1 )
- {
- //New request. Increase request counter.
- rCount++;
- //Load next submission list page
- getSubmList (submBaseURL1, submBaseURL2, no+1, 0);
- }
- else
- {
- //There are no more pages. Display message.
- var span = congLinkWin.document.createElement('span');
- span.innerHTML = '</br></br><b>No more pages!<b>';
- congLinkWin.document.body.insertBefore(span, null);
- }
- //Request finished successfully. Decrease request counter and check if rCount = 0.
- rCount--;
- checkIfDone();
- }
- //Function called when a submission list page failed to load.
- //req = XMLHttpRequest object of the failed page
- //no = number of loaded page
- function onSubmListErr (req, no)
- {
- //Page could not be loaded. Display Error message.
- var span = congLinkWin.document.createElement('span');
- span.innerHTML = '</br></br><b>Error - cannot load page ' + no + '!<br/>Further conglomeration stoppped.</b><br/>';
- congLinkWin.document.body.insertBefore(span, null);
- //Request finished with error. Decrease request counter and check if rCount = 0.
- rCount--;
- checkIfDone();
- }
- //Loads a submission list page from the gallery/scraps collection asynchronously.
- //baseURL = Base URL of gallery/scraps collection page.
- //no = Nummer of list page to load
- //retry = counter that indicates the current number of retries.
- //After [congRetries] failed retries, the function fails.
- function getSubmList (baseURL1, baseURL2, no, retry)
- {
- var req = new XMLHttpRequest();
- //Prepare request with full URL, containing the desired page number
- req.open('GET', baseURL1 + no + baseURL2, true);
- //Function to call when loading state of page changes
- req.onreadystatechange = function (evt)
- {
- //Check if page was loaded (or not)
- if (req.readyState == 4)
- {
- if (req.status == 200)
- //If page was sucessfully loaded
- onSubmList (req, no);
- else
- {
- //If page failed to load, retry up to [congRetries] times. Otherwise call error handler.
- if ( retry < congRetries )
- getSubmList (baseURL1, baseURL2, no, retry+1);
- else
- onSubmListErr (req, no);
- }
- }
- };
- //Sends request and starts loading of the page.
- req.send(null);
- }
- //Global variables:
- //Base URL of IB, including protocol and subdomain
- var ibBaseURL;
- //Base URL parts of gallery
- var submBaseURL1, submBaseURL2;
- //Reference to new link window
- var congLinkWin;
- //Counter for submission index
- var sCount;
- //Name of IB user whose galery/scraps are being conglomerated
- var ibUsername;
- //Source of submission ("gallery" or "scraps")
- var lStatus;
- //Request counter. Used to determine when all submissions are loaded.
- var rCount;
- //Main script function. Called when conglomerator button is clicked
- function congStart(e)
- {
- //Prevents the form's default action for a button
- e.preventDefault();
- //Get IB base URL (including protocol and a possible subdomain)
- var ibBaseURL_ = document.location.href.match(/(.*inkbunny\.net)\/gallery\/([^\/]*?)\/[^\/]*?\/([^$]*?)$/);
- if ( ibBaseURL_ )
- {
- ibBaseURL = ibBaseURL_[1];
- var userID = ibBaseURL_[2];
- var rID = ibBaseURL_[3];
- submBaseURL1 = ibBaseURL + '/gallery/' + userID + '/';
- submBaseURL2 = '/' + rID;
- //Extract user name from page
- var uCls = document.getElementsByClassName('widget_userNameSmall');
- for (var i=uCls.length-1; i >= 0; i--)
- {
- if ( uCls[i].nodeName == 'A')
- {
- ibUsername = uCls[i].innerHTML;
- break;
- }
- }
- if ( ibUsername )
- {
- //Open new window for extracted submission links
- congLinkWin = window.open();
- if ( congLinkWin )
- {
- //Set title of new window
- congLinkWin.document.title = 'IB conglomerator';
- //Add title to link window
- var span = congLinkWin.document.createElement('span');
- span.innerHTML = '<b>List of ' + ibUsername + '\'s gallery:<b>';
- congLinkWin.document.body.insertBefore(span, null);
- //Add status message to link Window
- lStatus = congLinkWin.document.createElement('span');
- lStatus.innerHTML = '<b> Loading...</b>';
- congLinkWin.document.body.insertBefore(lStatus, null);
- //Reset submission and request counter
- sCount = 1;
- rCount = 1;
- //At this point ibBaseURL and submBaseURL both matched correctly.
- //Load first submission list page
- getSubmList (submBaseURL1, submBaseURL2, 1, 0);
- }
- }
- }
- }
- function insertButton()
- {
- //Find location of gallery controls
- var spans = document.getElementsByTagName('span');
- for (var i=0; i < spans.length; i++)
- {
- if (spans[i].innerHTML == 'thumbnail size - ')
- {
- var browsebar = spans[i].parentNode.parentNode.parentNode;
- break;
- }
- }
- //If found, insert conglomerator button
- if ( browsebar )
- {
- browsebar.insertBefore(congButton, browsebar.firstChild);
- buttonInserted = true;
- }
- }
- //Flag that is later set to true if button was inserted.
- var buttonInserted = false;
- //Create button for conglomerator script
- var congButton = document.createElement('button');
- //Set button style
- congButton.style.backgroundColor="#8AE234";
- congButton.style.borderStyle="solid";
- congButton.style.borderRadius="5px";
- congButton.style.marginLeft="12px";
- //Set button text
- congButton.innerHTML = 'Conglomerate';
- //Set function to call on a click
- congButton.addEventListener ('click', congStart, false);
- //Try to insert button into page if page is already available at this time
- insertButton();
- //Function onDCL is called when DOM tree has completely been loaded.
- //This is the savest way to start a script.
- document.addEventListener ('DOMContentLoaded',
- function onDCL(e)
- {
- //If the button wasn't inserted before because the page wasn't
- //available at that time, it will be inserted now.
- if ( !buttonInserted )
- insertButton();
- }, false
- );
- //Additional, faster way for Opera to insert and display the conglomerator button.
- //Inline scripts sometimes seem to block the DOMContentLoaded event until they have executed completely.
- //This code will insert the button as soon as (but before) an inline script is called
- //
- //Check if the opera object exists. If yes, this script runs in an opera browser.
- if (window.opera)
- {
- window.opera.addEventListener ('BeforeScript',
- function onDCL(e)
- {
- //Try to insert button, if it hasn't been inserted yet.
- if ( !buttonInserted )
- insertButton();
- //Check if the button was inserted somehow (either by this function or by the DOMContentLoaded event listener above)
- if (buttonInserted)
- {
- //Remove this event listener again if button was inserted successfully.
- window.opera.removeEventListener ('BeforeScript', onDCL, false);
- }
- }, false
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment