InstaSynchP Commands

Plugin for custom commands

当前为 2015-02-21 提交的版本,查看 最新版本

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