InstaSynchP Commands

Plugin for custom commands

目前為 2015-04-29 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Commands
  3. // @namespace InstaSynchP
  4. // @description Plugin for custom commands
  5.  
  6. // @version 1.0.3
  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=49210
  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. "'shuffle": {
  118. 'hasArguments': true,
  119. 'type': 'mod',
  120. 'callback': function (){
  121. sendcmd('shuffle');
  122. }
  123. },
  124. "'pause": {
  125. 'type': 'mod'
  126. },
  127. "'resume": {
  128. 'type': 'mod'
  129. },
  130. "'seekto": {
  131. 'hasArguments': true,
  132. 'type': 'mod'
  133. },
  134. "'seekfrom": {
  135. 'hasArguments': true,
  136. 'type': 'mod'
  137. },
  138. "'setskip": {
  139. 'hasArguments': true,
  140. 'type': 'mod'
  141. },
  142. "'banlist": {
  143. 'type': 'mod'
  144. },
  145. "'modlist": {
  146. 'type': 'mod'
  147. },
  148. "'leaverban": {
  149. 'hasArguments': true,
  150. 'type': 'mod'
  151. },
  152. //commented those so you can't accidently use them
  153. //"'clearbans",
  154. //"'motd ",
  155. //"'mod ",
  156. //"'demod ",
  157. //"'description ",
  158. "'next": {
  159. 'type': 'mod'
  160. }
  161. };
  162.  
  163. function empty() {
  164. return undefined;
  165. }
  166. //prepare default commands
  167. for (var command in defaultCommands) {
  168. if (defaultCommands.hasOwnProperty(command)) {
  169. defaultCommands[command].description = 'http://instasynch.com/help.php#commands';
  170. defaultCommands[command].callback = defaultCommands[command].callback || empty;
  171.  
  172. if (!defaultCommands[command].type) {
  173. defaultCommands[command].type = 'regular';
  174. }
  175. }
  176. }
  177. //bind them
  178. commands.bind(defaultCommands);
  179.  
  180. //commands gets executed by posting a message to the site and catching it
  181. //in the script scope
  182. events.on(th, 'ExecuteCommand', function (data) {
  183. var command = th.commandMap[data.arguments[0].toLowerCase()],
  184. opts = {
  185. usernames: [],
  186. videos: [],
  187. numbers: []
  188. },
  189. i,
  190. videoInfo;
  191. for (i = 1; i < data.arguments.length; i += 1) {
  192. videoInfo = urlParser.parse(data.arguments[i]);
  193. if (videoInfo) {
  194. opts.videos.push(videoInfo);
  195. }
  196. if (isBlackname(data.arguments[i])) {
  197. opts.usernames.push(data.arguments[i]);
  198. }
  199. if (data.arguments[i] !== '' && !isNaN(data.arguments[i])) {
  200. opts.numbers.push(Number(data.arguments[i]));
  201. }
  202. }
  203. data.arguments.splice(0, 1, opts);
  204. logger().debug(th.name, "Calling command", JSON.stringify(data.arguments));
  205. command.callback.apply(command.reference, data.arguments);
  206. });
  207. events.on(th, 'SendChat', function (message) {
  208. commands.execute.apply(commands, message.split(/\s/));
  209. });
  210.  
  211. //flood protect
  212. var oldsendcmd = window.room.sendcmd;
  213. window.room.sendcmd = function (command, data) {
  214. var i;
  215. if (command) {
  216. //add the command to the cache
  217. th.commandQueue.push({
  218. command: command,
  219. data: data
  220. });
  221. }
  222.  
  223. //early return if we can't send anything
  224. if (!th.sendcmdReady || th.commandQueue.length === 0) {
  225. return;
  226. }
  227.  
  228. //set not ready
  229. th.sendcmdReady = false;
  230. oldsendcmd(th.commandQueue[0].command, th.commandQueue[0].data);
  231. th.commandQueue.splice(0, 1);
  232. //after a second send the next ones
  233. setTimeout(function () {
  234. th.sendcmdReady = true;
  235. window.room.sendcmd();
  236. }, 275);
  237. };
  238. };
  239.  
  240. window.plugins = window.plugins || {};
  241. window.plugins.commands = new Commands('1.0.3');