Advertisement
thetenfold

GM_xmlhttpRequest - Asynchronous (queue method)

Aug 8th, 2013
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        GM_xmlhttpRequest - Asynchronous (queue method)
  3. // @namespace   http://userscripts.org/users/23652
  4. // @description Uses a queueing method to run multiple xml request on different urls
  5. // @include     *
  6. // @copyright   JoeSimmons
  7. // @version     1.0.0
  8. // @license     http://creativecommons.org/licenses/by-nc-nd/3.0/us/
  9. // ==/UserScript==
  10.  
  11.  
  12.  
  13. /* --------------------------- INFORMATION ----------------------------------------------------------------------
  14.  
  15.  
  16.     ## IMPORTANT INFORMATION ##
  17.  
  18.         - You may pass the arguments in any order
  19.  
  20.         - None of the arguments are strictly required
  21.  
  22. ---------------------------------------------
  23.  
  24.     ## SYNTAX ##
  25.  
  26.         - If you pass it an array, it should contain a list of URLs
  27.                 - e.g., runQueue( ['url1', 'url2', 'url3' ] );
  28.             If you do not pass it an array or the queue is empty, it will not run
  29.             because there is nothing in the queue
  30.  
  31.         - If you pass it a function, it will run that function each time a URL in the queue completes
  32.             loading, with the response as the first argument
  33.             If you do not pass it a function, it will run an empty function and do nothing (besides load the url)
  34.  
  35.         - If you pass it a number, it will use that as the delay between requests
  36.             If you do not, it will use 0 delay
  37.  
  38.  
  39. -------------------------------------------------------------------------------------------------------------- */
  40.  
  41.  
  42. // stack of requests
  43. var $queue = [];
  44.  
  45. function xhr(func, url, delay) {
  46.     if (typeof url !== 'string') { return; }
  47.  
  48.     GM_xmlhttpRequest({
  49.         'url' : url,
  50.         'method' : 'GET',
  51.         'onload' : function (r) {
  52.             func(r); // run callback
  53.                 window.setTimeout(function () {
  54.                     runQueue(func, delay); // run the next in queue, if any
  55.                 }, delay);
  56.         }
  57.     });
  58. }
  59.  
  60. // this takes any order of arguments and determines whether or not
  61. // xhr() should be run
  62. function runQueue() {
  63.     var i, func = null, delay = 0, arg;
  64.  
  65.     for (i = 0; i < arguments.length; i += 1) {
  66.         arg = arguments[i];
  67.         if ( Array.isArray(arg) ) $queue = $queue.concat(arg);
  68.             else if (typeof arg === 'function') func = arg;
  69.             else if (typeof arg === 'number' && arg > 0) delay = Math.floor(arg);
  70.     }
  71.  
  72.     if ($queue.length === 0) { return; }
  73.     if (func === null) func = function () {};
  74.  
  75.     xhr(func, $queue.shift(), delay); // run an xml request with the first url in the queue, then remove it from the queue
  76. }
  77.  
  78.  
  79.  
  80. // the handleRes function is an example.
  81. // you may edit this function or specify a new function in runQueue()
  82. function handleRes(res) {
  83.     var rText = res.responseText,  // response text
  84.         rStatus = res.status,      // response status
  85.         rUrl = res.finalUrl;       // response url
  86.  
  87.     // do stuff here
  88.  
  89.     // debug snippet for showing each url as it's loaded
  90.     alert('URL: ' + rUrl + '\n\nStatus: ' + rStatus + ($queue.length === 0 ? '\n\nEND OF QUEUE.' : ''));
  91.  
  92.     // debug snippet for showing when the queue is finished
  93.     if ($queue.length === 0) alert('The queue is finished.');
  94. }
  95.  
  96.  
  97. // example function
  98. // -----
  99. // it requests an array of URLs individually, with a 250ms delay
  100. // between each, and calls handleRes when each request is done (it doesn't check the status of the request)
  101. runQueue(250, handleRes, [
  102.     'http://www.google.com?num=1',
  103.     'http://www.google.com?num=2',
  104.     'http://www.google.com?num=3'
  105. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement