InstaSynchP Core

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

当前为 2014-11-22 提交的版本,查看 最新版本

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