InstaSynchP Event Hooks

Add hooks to the events on the InstaSynch page

目前为 2015-02-23 提交的版本,查看 最新版本

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