InstaSynchP CSSLoader

Framework plugin to load and unload CSS urls

  1. // ==UserScript==
  2. // @name InstaSynchP CSSLoader
  3. // @namespace InstaSynchP
  4. // @description Framework plugin to load and unload CSS urls
  5.  
  6. // @version 1.0.6
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-CSSLoader
  9. // @license MIT
  10.  
  11. // @include *://instasync.com/r/*
  12. // @include *://*.instasync.com/r/*
  13. // @grant none
  14. // @run-at document-start
  15.  
  16. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/code.js?version=37716
  17. // ==/UserScript==
  18.  
  19. function Style(opts) {
  20. 'use strict';
  21. this.name = opts.name;
  22. this.url = opts.url;
  23. this.autoload = opts.autoload;
  24. this.id = opts.id || this.name;
  25. this.content = opts.content;
  26. this.urlSetting = '{0}-css-url'.format(this.name);
  27. this.contentSetting = '{0}-css-content'.format(this.name);
  28. if (this.autoload) {
  29. this.load();
  30. }
  31. }
  32.  
  33. Style.prototype.onLoad = function () {
  34. 'use strict';
  35. var _this = this;
  36. _this.fillElement();
  37. events.fire('CSSLoad[{0}]'.format(_this.id));
  38. };
  39.  
  40. Style.prototype.unLoad = function () {
  41. 'use strict';
  42. var _this = this;
  43. $('#{0}'.format(_this.id)).remove();
  44. };
  45.  
  46. Style.prototype.createElement = function () {
  47. 'use strict';
  48. var _this = this;
  49. $('head').append(
  50. $('<style>', {
  51. 'type': 'text/css',
  52. 'id': _this.id
  53. })
  54. );
  55. };
  56.  
  57. Style.prototype.getContentAsync = function () {
  58. 'use strict';
  59. var _this = this;
  60. $.ajax({
  61. type: 'GET',
  62. url: _this.url,
  63. success: function (content) {
  64. _this.content = content;
  65. _this.save();
  66. _this.onLoad();
  67. }
  68. });
  69. };
  70.  
  71. Style.prototype.save = function () {
  72. 'use strict';
  73. var _this = this;
  74. gmc.set(_this.urlSetting, _this.url);
  75. gmc.set(_this.contentSetting, _this.content);
  76. window.plugins.settings.save();
  77. };
  78.  
  79. Style.prototype.getContent = function () {
  80. 'use strict';
  81. var _this = this;
  82. if (!_this.url) {
  83. return true;
  84. }
  85. if (_this.url === gmc.get(_this.urlSetting)) {
  86. _this.content = gmc.get(_this.contentSetting);
  87. return true;
  88. }
  89. _this.getContentAsync();
  90. return false;
  91. };
  92.  
  93. Style.prototype.fillElement = function () {
  94. 'use strict';
  95. var _this = this;
  96. $('#{0}'.format(_this.id)).text(_this.content);
  97. };
  98.  
  99. Style.prototype.load = function () {
  100. 'use strict';
  101. var _this = this;
  102. _this.unLoad();
  103. _this.createElement();
  104. if (_this.getContent()) {
  105. _this.onLoad();
  106. }
  107. };
  108.  
  109. function CSSLoader() {
  110. 'use strict';
  111. this.version = '1.0.6';
  112. this.name = 'InstaSynchP CSSLoader';
  113. this.styles = {};
  114. this.Style = Style;
  115. }
  116.  
  117. CSSLoader.prototype.addStyle = function (opts) {
  118. 'use strict';
  119. this.styles[opts.name] = new this.Style(opts);
  120. };
  121.  
  122. CSSLoader.prototype.loadStyle = function (styleName) {
  123. 'use strict';
  124. this.styles[styleName].load();
  125. };
  126.  
  127. window.plugins = window.plugins || {};
  128. window.plugins.cssLoader = new CSSLoader();