InstaSynchP CSSLoader

Plugin to load and unload CSS urls

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

  1. // ==UserScript==
  2. // @name InstaSynchP CSSLoader
  3. // @namespace InstaSynchP
  4. // @description Plugin to load and unload CSS urls
  5.  
  6. // @version 1.0.3
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-CSSLoader
  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-start
  17.  
  18. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js
  19. // ==/UserScript==
  20.  
  21. function CSSLoader(version) {
  22. "use strict";
  23. this.version = version;
  24. this.name = 'InstaSynchP CSSLoader';
  25. this.styles = {};
  26. }
  27.  
  28. CSSLoader.prototype.executeOnceCore = function() {
  29. "use strict";
  30. var th = this;
  31. window.cssLoader = (function () {
  32.  
  33. return {
  34. 'add': function (style) {
  35. //set the id as the name if it didn't get set
  36. if (!style.id) {
  37. style.id = style.name;
  38. }
  39.  
  40. //save the style
  41. th.styles[style.name] = style;
  42.  
  43. //load it
  44. if (style.autoload) {
  45. cssLoader.load(style.name);
  46. }
  47. },
  48. 'load': function (styleName) {
  49. var style = th.styles[styleName],
  50. id = '#{0}'.format(style.id);
  51. $(id).remove();
  52.  
  53. $('head').append(
  54. $('<link>', {
  55. 'rel': 'stylesheet',
  56. 'type': 'text/css',
  57. 'id': style.id,
  58. 'href': style.url
  59. }).on('load', function () {
  60. //fire event after the CSS has been loaded
  61. events.fire('CSSLoad[{0}]'.format($(this).attr('id')));
  62. })
  63. );
  64. //if the is nothing to load fire the event directly
  65. if (style.url === '') {
  66. events.fire('CSSLoad[{0}]'.format(style.id));
  67. }
  68. }
  69. };
  70. }());
  71. };
  72.  
  73. window.plugins = window.plugins || {};
  74. window.plugins.cssLoader = new CSSLoader('1.0.3');