InstaSynchP Core

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

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

  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.0.5
  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. // ==/UserScript==
  28. function Core(version) {
  29. "use strict";
  30. this.version = version;
  31. this.listeners = {};
  32. }
  33.  
  34. function coreRef() {
  35. return window.plugins.core;
  36. }
  37.  
  38. Core.prototype.executeOnceCore = function () {
  39. var th = coreRef();
  40. window.events = (function () {
  41. return {
  42. //bind event handlers
  43. 'on': function (eventName, callback, preOld) {
  44. eventName = eventName.trim();
  45. if (th.listeners[eventName] === undefined) {
  46. th.listeners[eventName] = {
  47. 'preOld': [],
  48. 'postOld': []
  49. };
  50. }
  51. //execute it before are after the overwritten function
  52. if (preOld) {
  53. th.listeners[eventName].preOld.push({
  54. callback: callback
  55. });
  56. } else {
  57. th.listeners[eventName].postOld.push({
  58. callback: callback
  59. });
  60. }
  61. },
  62. //bind event handler and remove any previous once
  63. 'once': function (eventName, callback, preOld) {
  64. this.unbind(eventName, callback);
  65. this.on(eventName, callback, preOld);
  66. },
  67. //unbind event handlers
  68. 'unbind': function (eventName, callback) {
  69. var i;
  70. //search all occurences of callback and remove it
  71. if (th.listeners[eventName] !== undefined) {
  72. for (i = 0; i < th.listeners[eventName].preOld.length; i += 1) {
  73. if (th.listeners[eventName].preOld[i].callback === callback) {
  74. th.listeners[eventName].preOld.splice(i, 1);
  75. i -= 1;
  76. }
  77. }
  78. for (i = 0; i < th.listeners[eventName].postOld.length; i += 1) {
  79. if (th.listeners[eventName].postOld[i].callback === callback) {
  80. th.listeners[eventName].postOld.splice(i, 1);
  81. i -= 1;
  82. }
  83. }
  84. }
  85. },
  86. //fire the event with the given parameters
  87. 'fire': function (eventName, parameters, preOld) {
  88. var i,
  89. listenersCopy;
  90. if (th.listeners[eventName] === undefined) {
  91. return;
  92. }
  93. //make a copy of the listener list since some handlers
  94. //could remove listeners changing the length of the array
  95. //while iterating over it
  96. if (preOld) {
  97. listenersCopy = [].concat(th.listeners[eventName].preOld);
  98. } else {
  99. listenersCopy = [].concat(th.listeners[eventName].postOld);
  100. }
  101. //fire the events and catch possible errors
  102. for (i = 0; i < listenersCopy.length; i += 1) {
  103. try {
  104. listenersCopy[i].callback.apply(this, parameters);
  105. } catch (err) {
  106. window.console.log(
  107. ("Error: {0}, eventName {1}, function {2} %s %s %o, " +
  108. "please check the console " +
  109. "and make a pastebin of everything in there").format(
  110. err.message, eventName, listenersCopy[i].callback.name),
  111. listenersCopy[i].callback, err.stack, err);
  112. }
  113. }
  114. }
  115. };
  116. }());
  117. };
  118.  
  119. Core.prototype.main = function () {
  120. "use strict";
  121. var th = coreRef();
  122. th.executeOnceCore();
  123. events.on('ExecuteOnce', window.plugins.cssLoader.executeOnceCore);
  124. events.on('ExecuteOnce', window.plugins.settings.executeOnceCore);
  125. //prepare plugins
  126. for (var pluginName in window.plugins) {
  127. if (window.plugins.hasOwnProperty(pluginName)) {
  128. var plugin = window.plugins[pluginName];
  129. if (plugin.preConnect) {
  130. events.on('PreConnect', plugin.preConnect);
  131. }
  132. if (plugin.postConnect) {
  133. events.on('PostConnect', plugin.postConnect);
  134. }
  135. if (plugin.executeOnce) {
  136. events.on('ExecuteOnce', plugin.executeOnce);
  137. }
  138. if (Object.prototype.toString.call(plugin.settings) === '[object Array]') {
  139. window.plugins.settings.fields = window.plugins.settings.fields.concat(plugin.settings);
  140. }
  141. }
  142. }
  143.  
  144. function load() {
  145. //reset variables
  146. events.fire('ResetVariables');
  147. //we are not connected yet
  148. events.fire('PreConnect');
  149. //these need to be executed last
  150.  
  151. //if the script was loading slow and we are already connected
  152. if (window.userInfo) {
  153. events.fire('PostConnect');
  154. }
  155. }
  156. //these need to be executed last
  157. events.on('ExecuteOnce', window.plugins.eventBase.executeOnceCore);
  158. events.once('Userlist', function () {
  159. events.fire('PostConnect');
  160. });
  161. //execute one time only scripts
  162. events.fire('ExecuteOnce');
  163. //load the scripts
  164. if (global.page.name !== 'index') {
  165. load();
  166. }
  167. //reload the scripts when changing a room
  168. events.on('LoadRoom', load);
  169.  
  170. };
  171.  
  172. window.plugins = window.plugins || {};
  173. window.plugins.core = new Core("1.0.5");
  174. window.addEventListener('load', coreRef().main, false);