InstaSynchP Event Hooks

Add hooks to the events on the InstaSynch page

目前为 2015-04-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Event Hooks
  3. // @namespace InstaSynchP
  4. // @description Add hooks to the events on the InstaSynch page
  5.  
  6. // @version 1.1.6
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Event-Hooks
  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/2857-jquery-bind-first/code/jquerybind-first.js?version=26080
  17. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/code.js?version=37716
  18. // ==/UserScript==
  19.  
  20. function EventHooks(version) {
  21. "use strict";
  22. this.version = version;
  23. this.name = 'InstaSynchP Event Hooks';
  24. this.resetVariables();
  25. this.isPlaylistLoaded = false;
  26. this.isShuffle = false;
  27. }
  28.  
  29. EventHooks.prototype.executeOnceCore = function () {
  30. 'use strict';
  31. this.isPlaylistLoaded = false;
  32. };
  33.  
  34. EventHooks.prototype.executeOnceCore = function () {
  35. "use strict";
  36. var th = this,
  37. oldLinkify = window.linkify,
  38. hooks = [
  39. {'connected':{'location': 'events', 'name':'Connected'}},
  40. {'joining':{'location': 'events', 'name':'Joining'}},
  41. {'joined':{'location': 'events', 'name':'Joined'}},
  42. {'reconnecting':{'location': 'events', 'name':'Reconnecting'}},
  43. {'reconnect':{'location': 'events', 'name':'Reconnect'}},
  44. {'disconnect':{'location': 'events', 'name':'Disconnect'}},
  45. {'addMessage':{'name':'AddMessage'}},
  46. {'addUser':{'location':'userlist', 'name':'AddUser'}},
  47. {'removeUser':{'location':'userlist', 'name':'RemoveUser'}},
  48. {'load':{'location':'userlist', 'name':'LoadUserlist'}},
  49. {'renameUser':{'location':'userlist', 'name':'RenameUser'}},
  50. {'makeLead':{'name':'MakeLeader'}},
  51. {'addVideo':{'location':'playlist', 'name':'AddVideo'}},
  52. {'removeVideo':{'location':'playlist', 'name':'RemoveVideo'}},
  53. {'moveVideo':{'location':'playlist', 'name':'MoveVideo'}},
  54. {'playVideo':{'name':'PlayVideo'}},
  55. {'load':{'location':'playlist', 'name':'LoadPlaylist'}},
  56. {'purge':{'location':'playlist', 'name':'Purge'}},
  57. {'resume':{'name':'Resume'}},
  58. {'pause':{'name':'Pause'}},
  59. {'seekTo':{'name':'SeekTo'}},
  60. {'setSkips':{'name':'Skips'}},
  61. {'create':{'location':'poll', 'name':'CreatePoll'}},
  62. {'addVote':{'location':'poll', 'name':'AddPollVote'}},
  63. {'removeVote':{'location':'poll', 'name':'RemovePollVote'}},
  64. {'end':{'location':'poll', 'name':'EndPoll'}},
  65. {'sendcmd':{'name':'SendCMD'}}
  66. ];
  67.  
  68. window.linkify = function (str, buildHashtagUrl, includeW3, target) {
  69. var tags = [],
  70. index = -1;
  71. //remove image urls so they wont get linkified
  72. str = str.replace(/(src|href)=\"([^\"]*)\"/gi, function ($0, $1, $2) {
  73. tags.push({
  74. 'tagName': $1,
  75. 'url': $2
  76. });
  77. return '{0}=\"\"'.format($1);
  78. });
  79. str = oldLinkify(str, buildHashtagUrl, includeW3, target);
  80. //put them back in
  81. str = str.replace(/(src|href)=\"\"/gi, function () {
  82. index += 1;
  83. return '{0}="{1}"'.format(tags[index].tagName, tags[index].url);
  84. });
  85. return str;
  86. };
  87.  
  88. function countUser(user, neg) {
  89. var inc = (typeof neg === 'boolean' && neg) ? -1 : 1;
  90. if (user.permissions > 0) {
  91. th.mods += inc;
  92. }
  93. if (user.loggedin) {
  94. th.blacknames += inc;
  95. } else {
  96. th.greynames += inc;
  97. }
  98. }
  99.  
  100. function subtractUser(user) {
  101. countUser(user, true);
  102. }
  103.  
  104. function createHookFunction(ev) {
  105. function defaultFunction() {
  106. events.fire(ev.name, arguments, true);
  107. ev.old.apply(undefined, arguments);
  108. events.fire(ev.name, arguments, false);
  109. }
  110. function arrayFunction() {
  111. arguments[0].forEach(function(arg){
  112. events.fire(ev.name, [arg], true);
  113. });
  114. ev.old.apply(undefined, arguments);
  115. arguments[0].forEach(function(arg){
  116. events.fire(ev.name, [arg], false);
  117. });
  118. }
  119.  
  120. if(ev.location === 'events'){
  121. room.e.on(ev.hook, function(){
  122. events.fire(ev.name, arguments, false);
  123. });
  124. return;
  125. }
  126. //custom hooks
  127. switch (ev.name) {
  128. case 'LoadPlaylist':
  129. return function(){
  130. if(!th.isPlaylistLoaded){
  131. defaultFunction.apply(undefined, arguments);
  132. th.isPlaylistLoaded = true;
  133. }else{
  134. events.fire('Shuffle', arguments, true);
  135. ev.old.apply(undefined, arguments);
  136. events.fire('Shuffle', arguments, false);
  137. }
  138. };
  139. case 'AddUser':
  140. return function () {
  141. if (Array.isArray(arguments[0])){
  142. arguments[0].forEach(function(user){
  143. countUser(user);
  144. });
  145. arrayFunction.apply(undefined, arguments);
  146. }else{
  147. countUser(arguments[0]);
  148. defaultFunction.apply(undefined, arguments);
  149. }
  150. };
  151. case 'AddVideo':
  152. return function () {
  153. console.log('AddVideo');
  154. if (th.isShuffle) {
  155. ev.old.apply(undefined, arguments);
  156. return;
  157. }
  158. if (Array.isArray(arguments[0])){
  159. arrayFunction.apply(undefined, arguments);
  160. }else{
  161. defaultFunction.apply(undefined, arguments);
  162. }
  163. };
  164. case 'RemoveUser':
  165. return function () {
  166. var args = [].slice.call(arguments),
  167. user = findUserId(args[0]);
  168. args.push(user);
  169. subtractUser(user);
  170. defaultFunction.apply(undefined, args);
  171. };
  172. case 'RenameUser':
  173. return function () {
  174. var args = [].slice.call(arguments),
  175. user = findUserId(args[0]);
  176. args.push(user);
  177. subtractUser(user);
  178. defaultFunction.apply(undefined, args);
  179. };
  180. case 'RemoveVideo':
  181. return function () {
  182. var indexOfVid = window.room.playlist.indexOf(arguments[0]),
  183. video = window.room.playlist.videos[indexOfVid],
  184. args = [].slice.call(arguments);
  185. args.push(video);
  186. args.push(indexOfVid);
  187. defaultFunction.apply(undefined, args);
  188. };
  189. case 'MoveVideo':
  190. return function () {
  191. var args = [].slice.call(arguments),
  192. oldPosition = window.room.playlist.indexOf(args[0]),
  193. video = window.room.playlist.videos[oldPosition];
  194. args.push(oldPosition);
  195. args.push(video);
  196. defaultFunction.apply(undefined, args);
  197. };
  198. case 'Skips':
  199. return function () {
  200. var args = [].slice.call(arguments);
  201. args.push((args[1] / th.blacknames) * 100); //skip percentage
  202. defaultFunction.apply(undefined, args);
  203. };
  204. }
  205. return defaultFunction;
  206. }
  207. hooks.forEach(function (temp) {
  208. for (var hook in temp) {
  209. if (!temp.hasOwnProperty(hook)) {
  210. continue;
  211. }
  212. var ev = temp[hook];
  213. ev.hook = hook;
  214. if(ev.location === 'events'){
  215. createHookFunction(ev);
  216. } else if (ev.location &&
  217. window.room[ev.location] &&
  218. window.room[ev.location][hook]) {
  219. ev.old = window.room[ev.location][hook];
  220. window.room[ev.location][hook] = createHookFunction(ev);
  221. } else if (window.room[hook]) {
  222. ev.old = window.room[hook];
  223. window.room[hook] = createHookFunction(ev);
  224. } else {
  225. logger().error(th.name, "Hook not found", hook, "with location", ev.location);
  226. }
  227. }
  228. });
  229.  
  230. window.addEventListener("message", function (event) {
  231. try {
  232. var parsed = JSON.parse(event.data);
  233. if (parsed.action) {
  234. //own events
  235. events.fire(parsed.action, [parsed.data], false);
  236. }
  237. //all
  238. events.fire('PageMessage', [parsed], false);
  239. } catch (ignore) {}
  240. }, false);
  241.  
  242. //get user count when already connected
  243. if (window.plugins.core.connected) {
  244. window.users.forEach(countUser);
  245. }
  246.  
  247. events.on(th, 'Shuffle', function (){
  248. th.isShuffle = true;
  249. }, true);
  250.  
  251. events.on(th, 'Shuffle', function (){
  252. th.isShuffle = false;
  253. }, false);
  254. };
  255.  
  256. EventHooks.prototype.preConnect = function () {
  257. "use strict";
  258. var csel = '#cin',
  259. oldPlayerDestroy = window.room.video.destroy;
  260. window.room.video.destroy = function () {
  261. events.fire('PlayerDestroy', [], true);
  262. oldPlayerDestroy();
  263. events.fire('PlayerDestroy', [], false);
  264. };
  265. $(csel).bindFirst('keypress', function (event) {
  266. events.fire('InputKeypress[{0}]'.format(event.keyCode), [event, $(csel).val()], false);
  267. events.fire('InputKeypress', [event, $(csel).val()], false);
  268. if (event.keyCode === 13 && $(csel).val() !== '') {
  269. events.fire('SendChat', [$(csel).val()], false);
  270. }
  271. });
  272. $(csel).bindFirst('keydown', function (event) {
  273. //prevent loosing focus on tab
  274. if (event.keyCode === 9) {
  275. event.preventDefault();
  276. }
  277. events.fire('InputKeydown[{0}]'.format(event.keyCode), [event, $(csel).val()], false);
  278. events.fire('InputKeydown', [event, $(csel).val()], false);
  279. });
  280. $(csel).bindFirst('keyup', function (event) {
  281. events.fire('InputKeyup[{0}]'.format(event.keyCode), [event, $(csel).val()], false);
  282. events.fire('InputKeyup', [event, $(csel).val()], false);
  283. });
  284. };
  285.  
  286. EventHooks.prototype.resetVariables = function () {
  287. "use strict";
  288. this.mods = 0;
  289. this.blacknames = 0;
  290. this.greynames = 0;
  291. };
  292.  
  293. window.plugins = window.plugins || {};
  294. window.plugins.eventHooks = new EventHooks('1.1.6');