InstaSynchP Core

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

目前為 2014-10-14 提交的版本,檢視 最新版本

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