InstaSynchP Commands

Plugin for custom commands

目前為 2014-11-08 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Commands
  3. // @namespace InstaSynchP
  4. // @description Plugin for custom commands
  5.  
  6. // @version 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. command.callback.apply(command.reference, data.arguments);
  133. });
  134. events.on(th, 'SendChat', function (message) {
  135. commands.execute.apply(commands, message.split(/\s/));
  136. });
  137.  
  138. //load flood protect
  139. var oldsendcmd = window.global.sendcmd;
  140. window.global.sendcmd = function (command, data) {
  141. var i;
  142. if (command) {
  143. //add the command to the cache
  144. th.commandQueue.push({
  145. command: command,
  146. data: data
  147. });
  148. }
  149.  
  150. //early return if we can't send anything
  151. if (!th.sendcmdReady || th.commandQueue.length === 0) {
  152. return;
  153. }
  154.  
  155. //set not ready
  156. th.sendcmdReady = false;
  157. //send and remove the last 4 commands
  158. for(i = 0; i < 4 && th.commandQueue.length > 0; i += 1){
  159. oldsendcmd(th.commandQueue[0].command, th.commandQueue[0].data);
  160. th.commandQueue.splice(0, 1);
  161. }
  162. //after a second send the next ones
  163. setTimeout(function () {
  164. th.sendcmdReady = true;
  165. window.global.sendcmd();
  166. }, 1010);
  167. };
  168. };
  169.  
  170. window.plugins = window.plugins || {};
  171. window.plugins.commands = new Commands('1');