InstaSynchP Commands

Plugin for custom commands

目前为 2014-11-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Commands
  3. // @namespace InstaSynchP
  4. // @description Plugin for custom commands
  5.  
  6. // @version 1.0.1
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Commands
  9. // @license MIT
  10.  
  11. // @include http://*.instasynch.com/*
  12. // @include http://instasynch.com/*
  13. // @include http://*.instasync.com/*
  14. // @include http://instasync.com/*
  15. // @grant none
  16. // @run-at document-start
  17.  
  18. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js
  19. // ==/UserScript==
  20.  
  21. function Commands(version) {
  22. "use strict";
  23. this.version = version;
  24. this.name = "InstaSynchP Commands";
  25. this.commandMap = {};
  26. this.sendcmdReady = true;
  27. this.commandQueue = [];
  28. }
  29.  
  30. Commands.prototype.executeOnceCore = function () {
  31. "use strict";
  32. var th = this;
  33. window.commands = {
  34. //add a command
  35. bind: function (commands) {
  36. if (typeof commands === 'undefined') {
  37. return;
  38. }
  39.  
  40. for (var command in commands) {
  41. if (commands.hasOwnProperty(command)) {
  42. commands[command].name = command;
  43. th.commandMap[command.toLowerCase()] = commands[command];
  44. }
  45. }
  46. },
  47. //get the commands
  48. get: function (key) {
  49. return th.commandMap[key.toLowerCase()];
  50. },
  51. //get all commands
  52. getAll: function () {
  53. return th.commandMap;
  54. },
  55. //execute a command
  56. execute: function () {
  57. if (th.commandMap.hasOwnProperty(arguments[0].toLowerCase())) {
  58. //send the event to the site
  59. window.postMessage(JSON.stringify({
  60. action: 'ExecuteCommand',
  61. data: {
  62. //turn arguments to array
  63. 'arguments': [].slice.call(arguments)
  64. }
  65. }), "*");
  66. }
  67. }
  68. };
  69.  
  70. /*{
  71. "'command": {
  72. 'hasArguments':true,
  73. 'type':'mod',
  74. 'reference': this
  75. 'description':'description',
  76. 'callback': function(){
  77. }
  78. }
  79. }*/
  80.  
  81. var defaultCommands = {
  82. "'resynch": {},
  83. "'toggleFilter": {},
  84. "'toggleAutosynch": {},
  85. "'togglePlaylistLock": {'type':'mod'},
  86. "'kick": {'hasArguments':true,'type':'mod'},
  87. "'ban": {'hasArguments':true,'type':'mod'},
  88. "'unban": {'hasArguments':true,'type':'mod'},
  89. "'clean": {'type':'mod'},
  90. "'remove": {'hasArguments':true,'type':'mod'},
  91. "'purge": {'hasArguments':true,'type':'mod'},
  92. "'move": {'hasArguments':true,'type':'mod'},
  93. "'play": {'hasArguments':true,'type':'mod'},
  94. "'pause": {'type':'mod'},
  95. "'resume": {'type':'mod'},
  96. "'seekto": {'hasArguments':true,'type':'mod'},
  97. "'seekfrom": {'hasArguments':true,'type':'mod'},
  98. "'setskip": {'hasArguments':true,'type':'mod'},
  99. "'banlist": {'type':'mod'},
  100. "'modlist": {'type':'mod'},
  101. "'leaverban": {'hasArguments':true,'type':'mod'},
  102. //commented those so you can't accidently use them
  103. //"'clearbans",
  104. //"'motd ",
  105. //"'mod ",
  106. //"'demod ",
  107. //"'description ",
  108. "'next": {'type':'mod'}
  109. };
  110.  
  111. function empty() {
  112. return undefined;
  113. }
  114. //prepare default commands
  115. for (var command in defaultCommands) {
  116. if (defaultCommands.hasOwnProperty(command)) {
  117. defaultCommands[command].description = 'http://instasynch.com/help.php#commands';
  118. defaultCommands[command].callback = empty;
  119.  
  120. if (!defaultCommands[command].type) {
  121. defaultCommands[command].type = 'regular';
  122. }
  123. }
  124. }
  125. //bind them
  126. commands.bind(defaultCommands);
  127.  
  128. //commands gets executed by posting a message to the site and catching it
  129. //in the script scope
  130. events.on(th, 'ExecuteCommand', function (data) {
  131. var command = th.commandMap[data.arguments[0].toLowerCase()],
  132. opts = {
  133. usernames: [],
  134. videos: [],
  135. numbers: []
  136. },
  137. i,
  138. videoInfo;
  139. for (i = 1; i < data.arguments.length; i += 1) {
  140. videoInfo = urlParser.parse(data.arguments[i]);
  141. if(videoInfo){
  142. opts.videos.push(videoInfo);
  143. }
  144. if(isBlackname(data.arguments[i])){
  145. opts.usernames.push(data.arguments[i]);
  146. }
  147. if(data.arguments[i] !== '' && !isNaN(data.arguments[i])){
  148. opts.numbers.push(Number(data.arguments[i]));
  149. }
  150. }
  151. data.arguments.splice(0, 1, opts);
  152. command.callback.apply(command.reference, data.arguments);
  153. });
  154. events.on(th, 'SendChat', function (message) {
  155. commands.execute.apply(commands, message.split(/\s/));
  156. });
  157.  
  158. //load flood protect
  159. var oldsendcmd = window.global.sendcmd;
  160. window.global.sendcmd = function (command, data) {
  161. var i;
  162. if (command) {
  163. //add the command to the cache
  164. th.commandQueue.push({
  165. command: command,
  166. data: data
  167. });
  168. }
  169.  
  170. //early return if we can't send anything
  171. if (!th.sendcmdReady || th.commandQueue.length === 0) {
  172. return;
  173. }
  174.  
  175. //set not ready
  176. th.sendcmdReady = false;
  177. //send and remove the last 4 commands
  178. for (i = 0; i < 4 && th.commandQueue.length > 0; i += 1) {
  179. oldsendcmd(th.commandQueue[0].command, th.commandQueue[0].data);
  180. th.commandQueue.splice(0, 1);
  181. }
  182. //after a second send the next ones
  183. setTimeout(function () {
  184. th.sendcmdReady = true;
  185. window.global.sendcmd();
  186. }, 1010);
  187. };
  188. };
  189.  
  190. window.plugins = window.plugins || {};
  191. window.plugins.commands = new Commands('1.0.1');