Greasy Fork is available in English.

WM Collector Object

This is the collector object which is created under the WM version 4.x script

Este script no debería instalarse directamente. Es una biblioteca que utilizan otros scripts mediante la meta-directiva de inclusión // @require https://update.greatest.deepsurf.us/scripts/6905/27573/WM%20Collector%20Object.js

  1. // ==UserScript==
  2. // @name WM Collector Object
  3. // @namespace MerricksdadWMCollectorObject
  4. // @description This is the collector object which is created under the WM version 4.x script
  5. // @license http://creativecommons.org/licenses/by-nc-nd/3.0/us/
  6. // @version 4.0.0.0
  7. // @copyright Charlie Ewing except where noted
  8. // ==/UserScript==
  9.  
  10. //this script requires some functions in the WM Common Library
  11. //this script needs access to a pre-defined JSON object
  12.  
  13.  
  14. (function(){
  15.  
  16. //***************************************************************************************************************************************
  17. //***** Collector Object
  18. //***************************************************************************************************************************************
  19. WM.collector = {
  20. tabs : {}, //container for window objects
  21. recycle : [], //container for reusable window objects
  22. queue : [], //container for urls to do in order
  23. count : 0,
  24.  
  25. windowExists : function(hwnd){
  26. try{
  27. var testUrl=tab.hwnd.location.toString();
  28. return true;
  29. }catch(e) {
  30. return false;
  31. }
  32. },
  33. //requires id, url and callback
  34. open : function(params) {try{
  35. //log("WM.collector.open()",{level:0});
  36.  
  37. //check for tab queueing
  38. if (WM.opts.queuetabs && WM.collector.count && !(params.emergency||false)) {
  39. if (params.first||false) {
  40. //cut in line to be next processed
  41. WM.collector.queue.unshift(params);
  42. return;
  43. }
  44. //toss the next action in the queue while we wait for the current one to finish
  45. WM.collector.queue.push(params);
  46. //log("WM.collector.open: request queued",{level:1});
  47. return;
  48. }
  49.  
  50. var url = params.url;
  51. var id = params.id;
  52.  
  53. //create a window or use a recycled one
  54. var tabHwnd;
  55. if (WM.collector.recycle.length) {
  56. tabHwnd = WM.collector.recycle.shift();
  57. //watch for missing window objects
  58. try{
  59. //use the existing window object if it responds
  60. tabHwnd.location.href=url;
  61. } catch (e) {
  62. //window object missing, make a new one
  63. //FF22 version
  64. tabHwnd = GM_openInTab(url,"_blank");
  65. //FF21 version
  66. //tabHwnd = ((WM.opts.useGM_openInTab)?GM_openInTab:window.open)(url,"_blank");
  67. }
  68. } else {
  69. //we do not use recycling, just make a new one
  70. //FF22 version
  71. tabHwnd = GM_openInTab(url,"_blank");
  72. //FF21 version
  73. //tabHwnd = ((WM.opts.useGM_openInTab)?GM_openInTab:window.open)(url,"_blank");
  74. }
  75.  
  76. //window opening
  77. if (tabHwnd) {
  78. WM.collector.count++;
  79. params.hwnd=tabHwnd; //store the window handle
  80. params.openTime=timeStamp();
  81. WM.collector.tabs[id]=params; //add the tab and all its data to the array
  82.  
  83. //pass data to the sidekick top window
  84. var callback = params.callback;
  85. if (callback) delete params.callback;
  86. if (params.msg) {
  87. remove($(params.msg));
  88. delete(params.msg);
  89. }
  90. //details for posts, not for likes
  91. var app, synApp, isPost;
  92. if (isPost=(params.post||null)){
  93. app=params.post.app;
  94. synApp=app.parent||app;
  95. }
  96.  
  97. if (callback) {
  98. //log("WM.collector.open: callback fired",{level:3});
  99. doAction(function(){
  100. callback(params);
  101. });
  102. }
  103. } else {
  104. log("WM.collector: Tab or Window is not opening or your browser does not support controlling tabs and windows via scripts. Check your popup blocker.",{level:5});
  105. }
  106. }catch(e){log("WM.collector.open: "+e);}},
  107.  
  108. doNext : function(){try{WM.collector.open(WM.collector.queue.shift());}catch(e){log("WM.collector.doNext: "+e);}},
  109.  
  110. close : function(tab) {try{
  111. //recycle or close the passed tab
  112. try{
  113. if (WM.opts.recycletabsall || WM.opts.queuetabs || (WM.collector.recycle.length < WM.opts.recycletabs)) {
  114. //wipe it and put it away
  115. if (tab.hwnd){
  116. WM.collector.recycle.push(tab.hwnd);
  117. tab.hwnd.location.href="about:blank";
  118. if (WM.collector.windowExists(tab.hwnd)){
  119. tab.hwnd.location.hash="";
  120. }
  121. } else {
  122. //tab is busy, laggy or missing
  123. tab.closeRetries=(tab.closeRetries||0)+1;
  124. if (tab.closeRetries<3) {
  125. setTimeout(function(){WM.collector.close(tab);},1000);
  126. } else {
  127. log("WM.collector.close: Control of window handle lost; cannot recycle. Window may be too busy to communicate with, or has been closed manually.");
  128. }
  129. return;
  130. }
  131. } else {
  132. if (tab.hwnd) tab.hwnd.close();
  133. }
  134. } catch (e){log("WM.collector.close: recycler: "+e);}
  135.  
  136. try{
  137. tab.hwnd=null;
  138. delete tab.signal;
  139. delete tab.stage;
  140. delete tab.closeTries;
  141. if (tab.toIntv) clearInterval(tab.toIntv);
  142. delete tab;
  143. tab=null;
  144. WM.collector.count--
  145. }catch(e){log("WM.collector.close: destroy tab: "+e);}
  146.  
  147. //check for items in queue to do next
  148. if (WM.collector.queue.length) {
  149. //check that queueing is still in practice
  150. if (WM.opts.queuetabs) {
  151. setTimeout(WM.collector.doNext,1000); //just do one
  152. } else {
  153. //options have changed since queueing was enacted, release all the queue into windows right now
  154. var offset=1000;
  155. while (WM.collector.queue.length && (WM.collector.count < WM.opts.maxrequests)) {
  156. setTimeout(WM.collector.doNext,offset); //open all, up to the limit set in options
  157. offset+=100;
  158. }
  159. }
  160. }
  161.  
  162. } catch (e){log("WM.collector.close: "+e);}},
  163.  
  164. closeAll : function() {try{
  165. //first delete the queue so close fx doesnt pick them up
  166. WM.collector.queue=[]; //empty but dont destroy
  167.  
  168. //then close the active windows, moving any to the recycler if that is enabled
  169. for (var t in WM.collector.tabs) {
  170. WM.collector.close(WM.collector.tabs[t]);
  171. }
  172.  
  173. //then close any recycled windows
  174. if (WM.collector.recycle.length) {
  175. for (var r=0, hwnd; r < WM.collector.recycle.length; r++) {
  176. if (hwnd=WM.collector.recycle[r]) {
  177. hwnd.close();
  178. }
  179. }
  180. WM.collector.recycle=[];
  181. }
  182. } catch (e){log("WM.collector.closeAll: "+e);}},
  183. createTimer : function(tab) {try{
  184. //create a timeout handler based on options and store the timer on the tab
  185. tab.toIntv=setTimeout(function(){
  186. if (tab) if (tab.stage!=4) doAction(function(){
  187. //tab has been active too long, do timeout
  188. log("WM.collector.timer: request timeout ("+tab.id+")",{level:3});
  189. WM.setAsFailed(null, -14, tab.post);
  190. WM.clearURL(tab);
  191. })
  192. },WM.opts.reqtimeout*1000);
  193. } catch (e){
  194. log("WM.collector.createTimer: "+e);}
  195. },
  196.  
  197. cancelProcess : function(params) {try{
  198. params=params||{};
  199. var c = WM.collector;
  200. for (t in c.tabs) {
  201. if (c.tabs[t] && c.tabs[t][params.search] && c.tabs[t][params.search]==params.find){
  202. //matching collector tab found
  203. tab=c.tabs[t];
  204. //close the window
  205. c.close(tab);
  206. }
  207. }
  208. } catch (e){log("WM.collector.cancelProcess: "+e);}},
  209. refreshProcess : function(params) {try{
  210. params=params||{};
  211. var c = WM.collector;
  212. for (t in c.tabs) {
  213. if (c.tabs[t] && c.tabs[t][params.search] && c.tabs[t][params.search]==params.find){
  214. //matching collector tab found
  215. tab=c.tabs[t];
  216. //restart the window at its initial url
  217. if (tab.hwnd.location.href==tab.url) {
  218. tab.hwnd.location.reload();
  219. } else {
  220. tab.hwnd.location.href=tab.url;
  221. }
  222. }
  223. }
  224. } catch (e){log("WM.collector.refreshProcess: "+e);}},
  225.  
  226. };
  227.  
  228. })();