WM App Object

This is the app class which is created under the WM version 4.x script

当前为 2014-12-09 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/6895/27560/WM%20App%20Object.js

  1. // ==UserScript==
  2. // @name WM App Object
  3. // @namespace MerricksdadWMAppObject
  4. // @description This is the app class 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. //***** App Class
  18. //***************************************************************************************************************************************
  19. WM.App = function(params){try{
  20. this.objType="app";
  21. var self=this;
  22. //expected: id, name, namespace, icon
  23. params=params||{};
  24.  
  25. //create the masterswitch
  26. var testms=WM.quickOpts.masterSwitch[params.appID];
  27. WM.quickOpts.masterSwitch[params.appID]=(testms==null||testms=="undefined")?true:testms;
  28.  
  29. //set defaults
  30. this._enabled=WM.quickOpts.masterSwitch[params.appID]||false;
  31. this._paused=false;
  32. this.tests={};
  33. this.typesPaused=[];
  34. this.pausedTypesListNodes={};
  35. this._acceptCount=0;
  36. this._failCount=0;
  37. this.node=null;
  38. this.expanded=false;
  39. this.kids=[]; //contains additional filtered apps
  40. //setup config for this sidekick
  41. this.opts = {};
  42. this.config = new Config({
  43. storageName:"settings_"+params.appID+"_"+(WM.quickOpts.useGlobalSettings?"global":WM.currentUser.profile),
  44. onSave:WM.onSave,
  45. title:"FB Wall Manager "+WM.version+(WM.quickOpts.useGlobalSettings?" (!! Global Settings !!)":""),
  46. logo:createElement("span",{}[
  47. createElement("img",{className:"logo",src:"",textContent:"v"+WM.version}),
  48. createElement("text","v"+WM.version)
  49. ]),
  50. css:(
  51. WM.console.dynamicIcons()+
  52. jsForms.globalStyle()
  53. ),
  54. settings:{
  55. btn_useGlobal:{
  56. type:"button",
  57. label:"Use Global Settings",
  58. title:"Switch to using a global storage for settings. Those settings can then be used by other accounts (not browser profiles).",
  59. script:function(){
  60. if (WM.quickOpts.useGlobalSettings||false) {
  61. //already using global settings
  62. return;
  63. }
  64. if (confirm("Switch to using global (shared) settings?")){
  65. WM.quickOpts.useGlobalSettings=true;
  66. WM.saveQuickOpts();
  67. this.config.title = "FB Wall Manager "+WM.version+" (!! Global Settings !!))";
  68. this.config.storageName = "settings_"+params.appID+"_global";
  69. this.config.values=this.config.read();
  70. this.config.configure();
  71. this.config.reload();
  72. }
  73. },
  74. },
  75. btn_useOwnProfile:{
  76. type:"button",
  77. label:"Use Profile Settings",
  78. title:"Switch to using your own profile storage for settings.",
  79. script:function(){
  80. if (!(WM.quickOpts.useGlobalSettings||false)) {
  81. //already using profile settings
  82. return;
  83. }
  84. if (confirm("Switch to using your own profile settings?")){
  85. WM.quickOpts.useGlobalSettings=false;
  86. WM.saveQuickOpts();
  87. this.config.title = "FB Wall Manager "+WM.version;
  88. this.config.storageName = "settings_"+params.appID+"_"+WM.currentUser.profile;
  89. this.config.values=this.config.read();
  90. this.config.configure();
  91. this.config.reload();
  92. }
  93. },
  94. },
  95. },
  96. });
  97. //setup user defined accept texts
  98. try{
  99. if (WM.quickOpts.userDefinedTypes) {
  100. this.userDefinedTypes=WM.quickOpts.userDefinedTypes[params.appID]||{};
  101. } else {
  102. WM.quickOpts.userDefinedTypes={};
  103. WM.quickOpts.userDefinedTypes[params.appID]={};
  104. WM.saveQuickOpts();
  105. }
  106. }catch(e){log("WM.App.init: userDefinedTypes: "+e);}
  107.  
  108. //use passed params
  109. for (var p in params) this[p]=params[p];
  110.  
  111. //enable/disable all sidekick functions
  112. this.enable=function(){try{this.enabled=true;}catch(e){log("WM.App.enable: "+e);}};
  113. this.disable=function(){try{this.enabled=false;}catch(e){log("WM.App.disable: "+e);}};
  114. this.toggle=function(){try{this.enabled=!this.enabled;}catch(e){log("WM.App.toggle: "+e);}};
  115.  
  116. //pause collection for this app
  117. this.pause=function(){try{this.paused=true;}catch(e){log("WM.App.pause: "+e);}}
  118. this.unPause=function(){try{this.paused=false;}catch(e){log("WM.App.unPause: "+e);}}
  119.  
  120. //user defined types
  121. this.addUDT=function(params,drawOnly){try{
  122. //validate params or ask for input
  123. if (!exists(params) || !params.id) {
  124. params=params||{};
  125. var udtname=prompt("Enter the text name of the bonus type you wish to make (ie 'Horse')\n","");
  126. var udtid=this.appID+udtname.noSpaces().toLowerCase();
  127. udtid=prompt("OK, your type will read as '"+udtname+"'.\nNow modify this bonus type code to suit your needs.\n\nTip: You should prefix this code with the appID '"+this.appID+"', but it is not required.\nTip: Most sidekicks use lowercase and no spaces, but again, this is not a requirement.\n", udtid);
  128. if (udtid.trim()){
  129. params.id=udtid.trim();
  130. params.name=udtname;
  131. } else {
  132. alert("You supplied a blank user defined type ID. No type was created.");
  133. return false;
  134. }
  135. }
  136. if (!drawOnly){
  137. this.userDefinedTypes[params.id]=params.name;
  138. WM.quickOpts.userDefinedTypes[this.appID]=this.userDefinedTypes;
  139. WM.saveQuickOpts();
  140. }
  141. //draw the udt node
  142. if (this.udtNode){
  143. this.udtNode.appendChild(
  144. createElement("div",{className:"listItem"},[
  145. createElement("label",{textContent:params.id+" : "}),
  146. createElement("input",{value:params.name,title:"The display name of this type, used wherever bonus types are identified or selected.", onchange:function(){
  147. self.userDefinedTypes[params.id]=this.value;
  148. WM.quickOpts.userDefinedTypes[self.appID]=self.userDefinedTypes;
  149. WM.saveQuickOpts();
  150. }}),
  151. createElement("div",{className:"littleButton oddOrange", title:"Remove User-Defined Type"},[
  152. createElement("img",{className:"resourceIcon trash" +WM.opts.littleButtonSize,onclick:function(){
  153. var ask=WM.opts.appsConfirmDeleteUDT;
  154. if (!ask || (ask && confirm("Delete User Defined Type?"))) {
  155. delete self.userDefinedTypes[params.id];
  156. WM.quickOpts.userDefinedTypes[self.appID]=self.userDefinedTypes;
  157. WM.saveQuickOpts();
  158. remove (this.parentNode.parentNode);
  159. }
  160. }})
  161. ]),
  162. (this.accText[params.id]||null)?createElement("span",{title:"The type id you created exactly matches one provided by the sidekick for this app. If you did not intend to overwrite that bonus's display text, you may wish to create another type id and destroy this one.",style:"color:red;",textContent:"Overwrites a sidekick-provided bonus type id."}):null,
  163. ])
  164. );
  165. }
  166. }catch(e){log("WM.App.addUDT: "+e);}}
  167.  
  168. //unpause all bonus types for this app
  169. this.unpauseAllTypes=function(){try{
  170. for (var i=this.typesPaused.length-1;i>=0;i--){
  171. WM.unPauseByType(this,this.typesPaused[i]);
  172. }
  173. }catch(e){log("WM.App.unpauseAllTypes: "+e);}};
  174.  
  175.  
  176. //mass set priority for entire app post collection
  177. this.setPriority=function(n){try{
  178. for (var p in WM.posts) {
  179. var post=WM.posts[p];
  180. if (post.app==this) post.setPriority(n);
  181. }
  182. }catch(e){log("WM.App.setPriority: "+e);}};
  183.  
  184. //mass set priority for all posts of type
  185. this.setPriorityByType=function(w,n){try{
  186. for (var p in WM.posts) {
  187. var post=WM.posts[p];
  188. if (post.app==this && post.which==w) post.setPriority(n);
  189. }
  190. }catch(e){log("WM.App.setPriorityByType: "+e);}};
  191. //reset accept/fail counters
  192. this.resetCounter=function(){try{
  193. this.acceptCount=0;
  194. this.failCount=0;
  195. }catch(e){log("WM.App.resetCounter: "+e);}};
  196.  
  197. //reset all config options for this app
  198. //except those outside the standard branch (dontsteal,blockautolike,etc.)
  199. this.resetConfig=function(){try{
  200. var ask=WM.opts.configConfirmRestore;
  201. if (!ask || (ask && confirm("Restore sidekick settings to defaults?"))) {
  202. this.config.configure({reset:true});
  203. this.config.save();
  204. }
  205. }catch(e){log("WM.App.resetConfig: "+e);}};
  206. //fetch posts only for this app
  207. //normally used for initial fetching only
  208. this.fetchPosts=function(){try{
  209. WM.fetch({bypassPause:true, apps:this});
  210. }catch(e){log("WM.App.fetchPosts: "+e);}};
  211.  
  212. this.fetchNewer=function(){try{
  213. WM.fetch({
  214. newer:true,
  215. apps:this,
  216. bypassPause:true,
  217. bypassAppDisabled:true
  218. });
  219. }catch(e){log("WM.App.fetchNewer: "+e);}};
  220.  
  221. this.fetchOlder=function(){try{
  222. WM.fetch({
  223. older:true,
  224. apps:this,
  225. bypassPause:true,
  226. bypassAppDisabled:true
  227. });
  228. }catch(e){log("WM.App.fetchOlder: "+e);}};
  229.  
  230. //get a list of posts for this app from the global posts list
  231. this.__defineGetter__("posts",function(){try{
  232. return matchByParam(WM.posts,"app",this,"object");
  233. }catch(e){log("WM.App.getPosts: "+e);}});
  234. //detect if this sidekick said it was chrome compatible
  235. this.__defineGetter__("isVer3",function(){try{
  236. return this.flags.postMessageCompatible || this.flags.worksInChrome;
  237. }catch(e){log("WM.App.isVer3: "+e);}});
  238.  
  239. //detect if is paused
  240. this.__defineGetter__("paused",function(){try{
  241. return this._paused;
  242. }catch(e){log("WM.App.paused: "+e);}});
  243. this.__defineSetter__("paused",function(v){try{
  244. this._paused=v;
  245. //update the sidekick page button graphics
  246. var btn=this.pauseButtonNode;
  247. if (btn) {
  248. var btnSize=WM.opts.littleButtonSize;
  249. with (btn.parentNode)
  250. className=className.swapWordB(this._paused,"oddGreen","oddOrange");
  251. with (btn)
  252. className=className.swapWordB(this._paused,"playRight"+btnSize,"pause"+btnSize);
  253. }
  254. //do events
  255. if (this._paused) WM.rulesManager.doEvent("onAppPaused",this);
  256. else WM.rulesManager.doEvent("onAppUnpaused",this);
  257. }catch(e){log("WM.App.paused: "+e);}});
  258. //detect if is enabled
  259. this.__defineGetter__("enabled",function(){try{
  260. return this._enabled;
  261. }catch(e){log("WM.App.enabled: "+e);}});
  262. this.__defineSetter__("enabled",function(v){try{
  263. this._enabled=v;
  264. //update the WM.quickOpts
  265. WM.quickOpts.masterSwitch[this.appID]=this._enabled;
  266. WM.saveQuickOpts();
  267. //update the sidekick page graphics
  268. if (this.toggleNode) this.toggleNode.checked=this._enabled;
  269. if (this.node) with (this.node){
  270. className=className.swapWordB(this._enabled,"enabled","disabled");
  271. }
  272. //do events
  273. if (this._enabled) WM.rulesManager.doEvent("onAppEnabled",this);
  274. else WM.rulesManager.doEvent("onAppDisabled",this);
  275. }catch(e){log("WM.App.enabled: "+e);}});
  276. this.__defineGetter__("acceptCount",function(){try{
  277. return this._acceptCount;
  278. }catch(e){log("WM.App.acceptCount: "+e);}});
  279. this.__defineSetter__("acceptCount",function(v){try{
  280. this._acceptCount=v;
  281. if (this.acceptCounterNode) this.acceptCounterNode.textContent=v;
  282. }catch(e){log("WM.App.acceptCount: "+e);}});
  283. this.__defineGetter__("failCount",function(){try{
  284. return this._failCount;
  285. }catch(e){log("WM.App.failCount: "+e);}});
  286. this.__defineSetter__("failCount",function(v){try{
  287. this._failCount=v;
  288. if (this.failCounterNode) this.failCounterNode.textContent=v;
  289. }catch(e){log("WM.App.failCount: "+e);}});
  290.  
  291. this.__defineGetter__("totalCount",function(){try{
  292. return this._failCount+this._acceptCount;
  293. }catch(e){log("WM.App.totalCount: "+e);}});
  294.  
  295. //detect if this app is bundled with another app
  296. //return the main app in this bundle
  297. this.__defineGetter__("synApp",function(){try{
  298. return this.parent||this;
  299. }catch(e){log("WM.App.synApp: "+e);}});
  300. this.toggleContent=function(){try{
  301. this.expanded=!this.expanded;
  302. var btnSize=WM.opts.littleButtonSize;
  303. with (this.contentNode)
  304. className=className.swapWordB(this.expanded,"expanded","collapsed");
  305. with (this.toggleImgNode)
  306. className=className.swapWordB(this.expanded,"treeCollapse"+btnSize,"treeExpand"+btnSize);
  307. }catch(e){log("WM.App.toggleContent: "+e);}};
  308.  
  309. this.showConfig=function(){try{
  310. this.config.open();
  311. }catch(e){log("WM.App.showConfig: "+e);}};
  312.  
  313. this.disableOpt=function(w){try{
  314. this.opts[w]=false;
  315. this.config.set(w,false);
  316. this.config.save();
  317. }catch(e){log("WM.App.disableOpt: "+e);}};
  318.  
  319. this.enableOpt=function(w){try{
  320. this.opts[w]=true;
  321. this.config.set(w,true);
  322. this.config.save();
  323. }catch(e){log("WM.App.enableOpt: "+e);}};
  324. //add menu elements
  325. try{
  326. /* no longer used in WM3
  327. if (this.menu) {
  328. //prefix all menu elements with the appID
  329. this.menu=WM.dock.fixMenu(this.menu,this.appID);
  330. //append this app's menu settings
  331. this.settingsBranch=WM.config.append({branch:"wmtab_games",data:this.menu});
  332. }
  333. //prefix all test returns with the appID
  334. WM.dock.fixTests(this.tests,this);
  335. //prefix all accept text id's with the appID
  336. WM.dock.fixAcceptTexts(this);
  337. */
  338. //new method
  339. if (this.menu) this.config.append({data:this.menu});
  340. //I should really move these into the sidekick realm
  341. var data={};
  342. data["dynamic"+this.appID]=checkBox(this.name+" ("+this.appID+")",true);
  343. WM.config.append({branch:"enableDynamic",data:data});
  344.  
  345. data={}; data[this.appID+"dontsteal"]=checkBox(this.name);
  346. WM.config.append({branch:"dontstealBlock",data:data});
  347. data={}; data["hide"+this.appID]=checkBox(this.name);
  348. WM.config.append({branch:"filterapps",data:data});
  349. data={}; data["nolike"+this.appID]=checkBox(this.name);
  350. WM.config.append({branch:"blockautolikebygame",data:data});
  351. } catch(e) {log("WM.App.init:addMenuElements: "+e);};
  352. //draw to #sidekickList (WM.console.sidekickNode)
  353. try{
  354. WM.console.sidekickNode.appendChild(
  355. this.node=createElement("div",{className:"listItem "+((this.enabled)?"enabled":"disabled")},[
  356. createElement("div",{className:"line"},[
  357. createElement("div",{className:"littleButton",title:"Toggle Content",onclick:function(){self.toggleContent();}},[
  358. this.toggleImgNode=createElement("img",{className:"resourceIcon "+(this.expanded?"treeCollapse"+WM.opts.littleButtonSize:"treeExpand"+WM.opts.littleButtonSize)}),
  359. ]),
  360. this.toggleNode=createElement("input",{type:"checkbox",checked:this.enabled,onchange:function(){
  361. self.enabled=this.checked;
  362. with (self.node) className=className.toggleWordB(!this.checked,"disabled");
  363. }}),
  364. (this.icon)?createElement("img",{className:"icon crisp", src:this.icon,style:"width: 32px;vertical-align: middle"}):null,
  365. createElement("label",{textContent: this.name}),
  366. //toolbox
  367. createElement("div",{className:"littleButton odd"+(this.paused?"Green":"Orange"), title:"Pause/Unpause"},[
  368. this.pauseButtonNode=createElement("img",{className:"resourceIcon "+(this.paused?"playRight":"pause")+WM.opts.littleButtonSize,onclick:function(){self.paused=!self.paused;}})]),
  369. createElement("div",{className:"littleButton oddBlue", title:"Reset config for this app"},[
  370. createElement("img",{className:"resourceIcon uncheckAll"+WM.opts.littleButtonSize,onclick:function(){self.resetConfig();}})]),
  371. createElement("div",{className:"littleButton oddBlue", title:"Fetch Newer Posts"},[
  372. createElement("img",{className:"resourceIcon rssUpRight"+WM.opts.littleButtonSize,onclick:function(){self.fetchNewer();}})]),
  373. createElement("div",{className:"littleButton", title:"Fetch Older Posts"},[
  374. createElement("img",{className:"resourceIcon rssDownLeft" +WM.opts.littleButtonSize,onclick:function(){self.fetchOlder();}})]),
  375. //new sidekick config button
  376. this.configButton=createElement("button",{textContent:"Options", onclick:function(){self.config.open();}}),
  377. ]),
  378. this.contentNode=createElement("div",{className:"subsection "+(this.expanded?"expanded":"collapsed")},[
  379. createElement("div",{className:"line"},[
  380. createElement("label",{textContent:"App ID:"}),
  381. createElement("span",{textContent:this.appID}),
  382. ]),
  383. createElement("div",{className:"line"},[
  384. createElement("label",{textContent:"Support Provided By:"}),
  385. (this.desc)?createElement("span",{textContent: this.desc}):null, //provided in sidekick block
  386. ]),
  387. createElement("div",{className:"line"},[
  388. createElement("label",{textContent:"Sidekick Help Link:"}),
  389. (this.helpLink)?createElement("a",{href:this.helpLink,textContent:this.helpLink}):null, //provided in sidekick block
  390. ]),
  391. //browsers supported
  392. createElement("div",{className:"line"},[
  393. createElement("label",{textContent:"Browsers Supported:",style:"vertical-align:top;"}),
  394. createElement("img",{className:"resourceIcon firefox16", style:"display:inline-block;",title:"FireFox"}),
  395. (this.isVer3)?createElement("img",{className:"resourceIcon chrome16", style:"display:inline-block;",title:"Google Chrome"}):null,
  396. ]),
  397. //types paused subbox
  398. createElement("div",{className:"line"},[
  399. createElement("label",{textContent:"Types Paused:",title:"This is a list of bonus types that are currently paused for this app."}),
  400. createElement("div",{className:"littleButton oddGreen",onclick:function(){self.unpauseAllTypes();},title:"Unpause all types by this app."},[
  401. createElement("img",{className:"resourceIcon playRight"+WM.opts.littleButtonSize}),
  402. ]),
  403. this.typesPausedNode=createElement("div",{className:"subsection"}),
  404. ]),
  405. //attached apps
  406. createElement("div",{className:"line"},[
  407. createElement("label",{textContent:"Attached Apps:",title:"Additional apps filtered and processed by this sidekick."}),
  408. this.filtersNode=createElement("div",{className:"subsection"}),
  409. ]),
  410. //helpers subbox
  411. createElement("div",{className:"line"},[
  412. createElement("label",{textContent:"Helpers:",title:"Sidekick helpers"}),
  413. this.helpersNode=createElement("div",{className:"subsection"}),
  414. ]),
  415. //user defined types subbox
  416. createElement("div",{className:"line"},[
  417. createElement("label",{textContent:"User-Defined Types:",title:"User Defined Types ('which')"}),
  418. createElement("div",{className:"littleButton oddGreen",onclick:function(){self.addUDT();},title:"Add New User Defined Type"},[
  419. createElement("img",{className:"resourceIcon plus"+WM.opts.littleButtonSize}),
  420. ]),
  421. this.udtNode=createElement("div",{className:"subsection"}),
  422. ]),
  423. ]),
  424. ])
  425. );
  426. }catch(e){log("WM.App.init:addSidekickElement: "+e);};
  427. //create feed filters for this app
  428. try{
  429. var feeds=WM.feedManager.feeds;
  430. for (var f=0,len=feeds.length;f<len;f++){
  431. feeds[f].addFilter({id:"app_"+this.appID});
  432. }
  433. }catch(e){log("WM.App.init:createFeedFilters: ")+e;}
  434.  
  435. //draw to collection filter coolbar
  436. try{
  437. //create game filter buttons on the WM.console
  438. var coolBar = WM.console.collectTabControl;
  439. if (coolBar) {
  440. //add a tab for this filter
  441. var tab = coolBar.addTab({
  442. text:(this.name||""),
  443. image:(this.icon||null),
  444. appFilter:this.appID,
  445. onSelect:WM.setAppFilter,
  446. selected:(WM.quickOpts.filterApp==this.appID),
  447. });
  448. this.collectionTabNode=tab.buttonNode;
  449. //force the image to have the 'crisp' drawing style
  450. tab.buttonNode.childNodes[0].className="icon crisp";
  451. //add accept/fail counters
  452. this.failCount=0;
  453. this.acceptCount=0;
  454. tab.buttonNode.insertBefore(
  455. createElement("div",{className:"accFailBlock"},[
  456. this.failCounterNode=createElement("span",{className:"fail",textContent:"0"}),
  457. this.acceptCounterNode=createElement("span",{className:"accept",textContent:"0"}),
  458. ])
  459. , tab.textNode);
  460. }
  461. } catch(e) {log("WM.App.init:addConsoleElement: "+e);};
  462. //show additional filtered apps
  463. try{
  464. if (isArrayAndNotEmpty(this.addFilters)) {
  465. for (var f,filt;(filt=this.addFilters[f]);f++){
  466. //create an app object for this filter
  467. filt.parent=this;
  468. this.kids.push(new WM.App(filt));
  469. if (this.filtersNode) this.filtersNode.appendChild(
  470. createElement("div",{className:"line"},[
  471. createElement("img",{className:"icon crisp", src:filt.icon||null}),
  472. createElement("text",filt.name),
  473. ])
  474. );
  475. }
  476. }
  477. } catch(e) {log("WM.App.init:addFilteredApps: "+e);};
  478. //draw my user defined types
  479. try{
  480. for (var u in this.userDefinedTypes){
  481. this.addUDT({id:u,name:this.userDefinedTypes[u]},true);
  482. }
  483. }catch(e){log("WM.App.init: drawUDTs: "+e);}
  484. //do events
  485. WM.rulesManager.doEvent("onSidekickReady",this);
  486. return self;
  487. }catch(e){log("WM.App.init: "+e);}};
  488.  
  489.  
  490.  
  491. })();