InstaSynchP Event Hooks

Add hooks to the events on the InstaSynch page

目前为 2014-10-11 提交的版本,查看 最新版本

  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
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Event-Hooks
  9. // @license GPL-3.0
  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/2857-jquery-bind-first/code/jquerybind-first.js
  19. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js
  20. // ==/UserScript==
  21.  
  22. function Plugin() {
  23. "use strict";
  24. this.version = 1;
  25. }
  26.  
  27. function ref() {
  28. return window.plugins.eventBase;
  29. }
  30.  
  31. Plugin.prototype.executeOnceCore = function () {
  32. "use strict";
  33.  
  34. var oldSendCmd = window.global.sendcmd,
  35. oldOnConnecting = window.global.onConnecting,
  36. oldOnConnected = window.global.onConnected,
  37. oldOnJoining = window.global.onJoining,
  38. oldOnJoined = window.global.onJoined,
  39. oldLoadRoomObj = window.global.loadRoomObj,
  40. oldOnReconnecting = window.global.onReconnecting,
  41. oldOnReconnect = window.global.onReconnect,
  42. oldReconnectFailed = window.global.reconnectFailed,
  43. oldOnError = window.global.onError,
  44. oldOnDisconnect = window.global.onDisconnect,
  45. oldRequestPartialPage = window.global.requestPartialPage,
  46. oldAddMessage = window.addMessage,
  47. oldAddUser = window.addUser,
  48. oldRemoveUser = window.removeUser,
  49. oldMakeLeader = window.makeLeader,
  50. oldRenameUser = window.renameUser,
  51. oldAddVideo = window.addVideo,
  52. oldRemoveVideo = window.removeVideo,
  53. oldMoveVideo = window.moveVideo,
  54. oldPlayVideo = window.playVideo,
  55. oldResume = window.resume,
  56. oldPause = window.pause,
  57. oldSeekTo = window.seekTo,
  58. oldPurge = window.purge,
  59. oldSkips = window.skips,
  60. oldLoadPlaylist = window.loadPlaylist,
  61. oldLoadUserlist = window.loadUserlist,
  62. oldCreatePoll = window.createPoll,
  63. oldAddPollVote = window.addPollVote,
  64. oldRemovePollVote = window.removePollVote,
  65. oldEndPoll = window.endPoll;
  66.  
  67. function fireOverwrittenEvent(name, old, args) {
  68. events.fire(name, args, true);
  69. old.apply(undefined, args);
  70. events.fire(name, args, false);
  71. }
  72. window.global.sendcmd = function () {
  73. fireOverwrittenEvent('SendCmd', oldSendCmd, arguments);
  74. };
  75. window.global.onConnecting = function () {
  76. fireOverwrittenEvent('Connecting', oldOnConnecting, arguments);
  77. };
  78. window.global.onConnected = function () {
  79. fireOverwrittenEvent('Connected', oldOnConnected, arguments);
  80. };
  81. window.global.onJoining = function () {
  82. fireOverwrittenEvent('Joining', oldOnJoining, arguments);
  83. };
  84. window.global.onJoined = function () {
  85. fireOverwrittenEvent('Joined', oldOnJoined, arguments);
  86. };
  87. window.global.loadRoomObj = function () {
  88. var args = [].slice.call(arguments);
  89. args.push(window.global.page); //page
  90. fireOverwrittenEvent('LoadRoom', oldLoadRoomObj, args);
  91. };
  92. window.global.onReconnecting = function () {
  93. fireOverwrittenEvent('Reconnecting', oldOnReconnecting, arguments);
  94. };
  95. window.global.onReconnect = function () {
  96. fireOverwrittenEvent('Reconnect', oldOnReconnect, arguments);
  97. };
  98. window.global.reconnectFailed = function () {
  99. fireOverwrittenEvent('ReconnectFailed', oldReconnectFailed, arguments);
  100. };
  101. window.global.onError = function () {
  102. fireOverwrittenEvent('OnError', oldOnError, arguments);
  103. };
  104. window.global.onDisconnect = function () {
  105. fireOverwrittenEvent('Disconnect', oldOnDisconnect, arguments);
  106. };
  107. window.global.requestPartialPage = function () {
  108. fireOverwrittenEvent('RequestPartialPage', oldRequestPartialPage, arguments);
  109. };
  110. window.addMessage = function () {
  111. fireOverwrittenEvent('AddMessage', oldAddMessage, arguments);
  112. };
  113. window.addUser = function () {
  114. fireOverwrittenEvent('AddUser', oldAddUser, arguments);
  115. };
  116. window.removeUser = function () {
  117. var args = [].slice.call(arguments);
  118. args.push(findUserId(args[0])); //user
  119. fireOverwrittenEvent('RemoveUser', oldRemoveUser, args);
  120. };
  121. window.makeLeader = function () {
  122. fireOverwrittenEvent('MakeLeader', oldMakeLeader, arguments);
  123. };
  124. window.renameUser = function () {
  125. fireOverwrittenEvent('RenameUser', oldRenameUser, arguments);
  126. };
  127. window.addVideo = function () {
  128. fireOverwrittenEvent('AddVideo', oldAddVideo, arguments);
  129. };
  130. window.removeVideo = function () {
  131. var indexOfVid = window.getVideoIndex(arguments[0].info),
  132. video = window.playlist[indexOfVid],
  133. args = [].slice.call(arguments);
  134. args.push(video);
  135. args.push(indexOfVid);
  136. fireOverwrittenEvent('RemoveVideo', oldRemoveVideo, args);
  137. };
  138. window.moveVideo = function () {
  139. var args = [].slice.call(arguments);
  140. args.push(window.getVideoIndex(args[0]).info); //old position
  141. fireOverwrittenEvent('MoveVideo', oldMoveVideo, args);
  142. };
  143. window.playVideo = function () {
  144. fireOverwrittenEvent('PlayVideo', oldPlayVideo, arguments);
  145. };
  146. window.resume = function () {
  147. fireOverwrittenEvent('Resume', oldResume, arguments);
  148. };
  149. window.pause = function () {
  150. fireOverwrittenEvent('Pause', oldPause, arguments);
  151. };
  152. window.seekTo = function () {
  153. fireOverwrittenEvent('SeekTo', oldSeekTo, arguments);
  154. };
  155. window.purge = function () {
  156. fireOverwrittenEvent('Purge', oldPurge, arguments);
  157. };
  158. window.skips = function () {
  159. fireOverwrittenEvent('Skips', oldSkips, arguments);
  160. };
  161. window.loadPlaylist = function () {
  162. fireOverwrittenEvent('LoadPlaylist', oldLoadPlaylist, arguments);
  163. };
  164. window.loadUserlist = function () {
  165. fireOverwrittenEvent('LoadUserlist', oldLoadUserlist, arguments);
  166. };
  167. window.createPoll = function () {
  168. fireOverwrittenEvent('CreatePoll', oldCreatePoll, arguments);
  169. };
  170. window.addPollVote = function () {
  171. fireOverwrittenEvent('AddPollVote', oldAddPollVote, arguments);
  172. };
  173. window.removePollVote = function () {
  174. fireOverwrittenEvent('RemovePollVote', oldRemovePollVote, arguments);
  175. };
  176. window.endPoll = function () {
  177. fireOverwrittenEvent('EndPoll', oldEndPoll, arguments);
  178. };
  179. window.addEventListener("message", function (event) {
  180. try {
  181. var parsed = JSON.parse(event.data);
  182. if (parsed.action) {
  183. //own events
  184. events.fire(parsed.action, [parsed.data], false);
  185. }
  186. //all
  187. events.fire('onPageMessage', [parsed], false);
  188. } catch (ignore) {}
  189. }, false);
  190. };
  191.  
  192. Plugin.prototype.preConnect = function () {
  193. "use strict";
  194. var oldPlayerDestroy = window.video.destroy;
  195. $("#chat input").bindFirst('keypress', function (event) {
  196. events.fire('InputKeypress[{0}]'.format(event.keyCode), [event, $("#chat input").val()], false);
  197. if (event.keyCode === 13 && $("#chat input").val() !== '') {
  198. events.fire('SendChat', [event, $("#chat input").val()], false);
  199. }
  200. });
  201. $("#chat input").bindFirst('keydown', function (event) {
  202. events.fire('InputKeydown[{0}]'.format(event.keyCode), [event, $("#chat input").val()], false);
  203. });
  204. $("#chat input").bindFirst('keyup', function (event) {
  205. events.fire('InputKeyup[{0}]'.format(event.keyCode), [event, $("#chat input").val()], false);
  206. });
  207. window.video.destroy = function () {
  208. events.fire('PlayerDestroy', [], true);
  209. oldPlayerDestroy();
  210. events.fire('PlayerDestroy', [], false);
  211. };
  212. };
  213.  
  214.  
  215. window.plugins = window.plugins || {};
  216. window.plugins.eventBase = new Plugin();