InstaSynchP Core

Base to load all the Plugins, also includes some mandatory plugins

目前为 2014-10-23 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Core
  3. // @namespace InstaSynchP
  4. // @description Base to load all the Plugins, also includes some mandatory plugins
  5.  
  6. // @version 1.1
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Core
  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-end
  17.  
  18. // @require https://greasyfork.org/scripts/2855-gm-config/code/GM_config.js
  19. // @require https://greasyfork.org/scripts/2859-video-url-parser/code/Video%20Url%20Parser.js
  20.  
  21. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js
  22. // @require https://greasyfork.org/scripts/5718-instasynchp-cssloader/code/InstaSynchP%20CSSLoader.js
  23. // @require https://greasyfork.org/scripts/5719-instasynchp-settings/code/InstaSynchP%20Settings.js
  24.  
  25. // @require https://greasyfork.org/scripts/2857-jquery-bind-first/code/jquerybind-first.js
  26. // @require https://greasyfork.org/scripts/5651-instasynchp-event-hooks/code/InstaSynchP%20Event%20Hooks.js
  27.  
  28. // @require https://greasyfork.org/scripts/2858-jquery-fullscreen/code/jqueryfullscreen.js
  29. // ==/UserScript==
  30. function Core(version) {
  31. "use strict";
  32. this.version = version;
  33. this.listeners = {};
  34. this.connected = false;
  35. }
  36.  
  37. Core.prototype.executeOnceCore = function () {
  38. "use strict";
  39. var th = this;
  40. window.events = (function () {
  41. return {
  42. //bind event handlers
  43. 'on': function (ref, eventNames, callback, preOld) {
  44. var arr = eventNames.split(','),
  45. eventName,
  46. i;
  47. for (i = 0; i < arr.length; i += 1) {
  48. eventName = arr[i].trim();
  49. if (th.listeners[eventName] === undefined) {
  50. th.listeners[eventName] = {
  51. 'preOld': [],
  52. 'postOld': []
  53. };
  54. }
  55. //execute it before are after the overwritten function
  56. if (preOld) {
  57. th.listeners[eventName].preOld.push({
  58. ref: ref,
  59. callback: callback
  60. });
  61. } else {
  62. th.listeners[eventName].postOld.push({
  63. ref: ref,
  64. callback: callback
  65. });
  66. }
  67. }
  68. },
  69. //bind event handler and remove any previous once
  70. 'once': function (ref, eventNames, callback, preOld) {
  71. this.unbind(eventNames, callback);
  72. this.on(ref, eventNames, callback, preOld);
  73. },
  74. //unbind event handlers
  75. 'unbind': function (eventNames, callback) {
  76. var arr = eventNames.split(','),
  77. eventName,
  78. i, j;
  79. for (i = 0; i < arr.length; i += 1) {
  80. eventName = arr[i].trim();
  81. //search all occurences of callback and remove it
  82. if (th.listeners[eventName] !== undefined) {
  83. for (j = 0; j < th.listeners[eventName].preOld.length; j += 1) {
  84. if (th.listeners[eventName].preOld[j].callback === callback) {
  85. th.listeners[eventName].preOld.splice(j, 1);
  86. j -= 1;
  87. }
  88. }
  89. for (j = 0; j < th.listeners[eventName].postOld.length; j += 1) {
  90. if (th.listeners[eventName].postOld[j].callback === callback) {
  91. th.listeners[eventName].postOld.splice(j, 1);
  92. j -= 1;
  93. }
  94. }
  95. }
  96. }
  97. },
  98. //fire the event with the given parameters
  99. 'fire': function (eventName, parameters, preOld) {
  100. var i,
  101. listenersCopy;
  102. if (th.listeners[eventName] === undefined) {
  103. return;
  104. }
  105. //make a copy of the listener list since some handlers
  106. //could remove listeners changing the length of the array
  107. //while iterating over it
  108. if (preOld) {
  109. listenersCopy = [].concat(th.listeners[eventName].preOld);
  110. } else {
  111. listenersCopy = [].concat(th.listeners[eventName].postOld);
  112. }
  113. //fire the events and catch possible errors
  114. for (i = 0; i < listenersCopy.length; i += 1) {
  115. try {
  116. listenersCopy[i].callback.apply(listenersCopy[i].ref, parameters);
  117. } catch (err) {
  118. window.console.log(
  119. ("Error: {0}, eventName {1}, function {2} %s %s %o, " +
  120. "please check the console " +
  121. "and make a pastebin of everything in there").format(
  122. err.message, eventName, listenersCopy[i].callback.name),
  123. listenersCopy[i].callback, err.stack, err);
  124. }
  125. }
  126. }
  127. };
  128. }());
  129. };
  130.  
  131. Core.prototype.resetVariables = function () {
  132. "use strict";
  133. this.connected = false;
  134. };
  135.  
  136. Core.prototype.main = function () {
  137. "use strict";
  138. var th = this;
  139. th.executeOnceCore();
  140. events.on(window.plugins.cssLoader, 'ExecuteOnce', window.plugins.cssLoader.executeOnceCore);
  141. events.on(window.plugins.settings, 'ExecuteOnce', window.plugins.settings.executeOnceCore);
  142. events.on(th, 'PreConnect,Disconnect', function () {
  143. events.fire('ResetVariables');
  144. });
  145. //prepare plugins
  146. for (var pluginName in window.plugins) {
  147. if (window.plugins.hasOwnProperty(pluginName)) {
  148. var plugin = window.plugins[pluginName];
  149. if (plugin.preConnect) {
  150. events.on(plugin, 'PreConnect', plugin.preConnect);
  151. }
  152. if (plugin.postConnect) {
  153. events.on(plugin, 'PostConnect', plugin.postConnect);
  154. }
  155. if (plugin.executeOnce) {
  156. events.on(plugin, 'ExecuteOnce', plugin.executeOnce);
  157. }
  158. if (plugin.resetVariables) {
  159. events.on(plugin, 'ResetVariables', plugin.resetVariables);
  160. }
  161. if (Object.prototype.toString.call(plugin.settings) === '[object Array]') {
  162. window.plugins.settings.fields = window.plugins.settings.fields.concat(plugin.settings);
  163. }
  164. }
  165. }
  166.  
  167. function load() {
  168. //reset variables
  169. events.fire('ResetVariables');
  170. //we are not connected yet
  171. events.fire('PreConnect');
  172. //these need to be executed last
  173.  
  174. //if the script was loading slow and we are already connected
  175. if (window.userInfo) {
  176. th.connected = true;
  177. events.fire('PostConnect');
  178. }
  179. }
  180. //these need to be executed last
  181. events.on(window.plugins.eventHooks, 'ExecuteOnce', window.plugins.eventHooks.executeOnceCore);
  182. events.once(th, 'Userlist', function () {
  183. th.connected = true;
  184. events.fire('PostConnect');
  185. });
  186. //execute one time only scripts
  187. events.fire('ExecuteOnce');
  188. //load the scripts
  189. if (global.page.name !== 'index') {
  190. load();
  191. }
  192. //reload the scripts when changing a room
  193. events.on(th, 'LoadRoom', load);
  194. };
  195.  
  196. window.plugins = window.plugins || {};
  197. window.plugins.core = new Core("1.1");
  198. window.addEventListener('load', function () {
  199. window.plugins.core.main();
  200. }, false);