InstaSynchP Settings

Provides the ability to store settings for the plugins

当前为 2014-10-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Settings
  3. // @namespace InstaSynchP
  4. // @description Provides the ability to store settings for the plugins
  5.  
  6. // @version 1
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Settings
  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-start
  17.  
  18. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js
  19. // ==/UserScript==
  20.  
  21. function Settings(version) {
  22. "use strict";
  23. this.version = version;
  24. }
  25.  
  26. function ref() {
  27. return window.plugins.settings;
  28. }
  29.  
  30. Settings.prototype.executeOnceCore = function () {
  31. "use strict";
  32. var th = ref();
  33. cssLoader.add({
  34. 'name': 'general',
  35. 'url': 'https://cdn.rawgit.com/Zod-/InstaSynchP-Settings/b4b071565266669398f54051ee97c9bc9391a13b/settings.css',
  36. 'autoload': true
  37. });
  38. //add the button
  39. $('#loggedInAs').children().first().before(
  40. $('<div>', {
  41. 'id': 'plugin-settings'
  42. }).append(
  43. $('<ul>').append(
  44. $('<li>').append(
  45. $('<a>', {
  46. 'class': 'clicker'
  47. }).append(
  48. $('<img>', {
  49. 'src': 'http://i.imgur.com/V3vOIkS.png'
  50. })
  51. ).append('Settings').click(function () {
  52. if (gmc.isOpen) {
  53. th.save(true);
  54. } else {
  55. gmc.open();
  56. }
  57. })
  58. )
  59. ).addClass('js')
  60. ).addClass('click-nav')
  61. );
  62. window.settingFields = window.settingFields || undefined;
  63. window.gmc = new GM_configStruct({
  64. 'id': 'GM_config',
  65. 'title': 'InstaSynchP Settings',
  66. 'fields': window.settingFields,
  67. 'events': {
  68. 'open': function (args) {
  69. //load GM_config css
  70. $('#GM_config').each(function () {
  71. //context of the iframe
  72. $('head', this.contentWindow.document || this.contentDocument).append(
  73. $('<link>', {
  74. 'type': 'text/css',
  75. 'rel': 'stylesheet',
  76. 'href': 'https://cdn.rawgit.com/Zod-/InstaSynchP-Settings/b4b071565266669398f54051ee97c9bc9391a13b/GMconfig.css'
  77. })
  78. );
  79. });
  80. $('#GM_config').css('height', '90%').css('top', '55px').css('left', '5px').css('width', '375px');
  81.  
  82. //collapse items in sections when clicking the header
  83. $('#GM_config').each(function () {
  84. $('#GM_config .section_header_holder', this.contentWindow.document || this.contentDocument).click(function () {
  85. $(this).children().filter(":not(:first-child)").slideToggle(250);
  86. console.log("slide");
  87. if (!$(this).children().eq(0).hasClass('section_desc')) {
  88. var next = $(this).next();
  89. while (next.children().eq(0).hasClass('section_desc')) {
  90. next.slideToggle(250);
  91. next = next.next();
  92. }
  93. }
  94. });
  95. });
  96. //Add a "save and close" button
  97. $('#GM_config').each(function () {
  98. var saveAndCloseButton = $('#GM_config_closeBtn', this.contentWindow.document || this.contentDocument).clone(false);
  99. saveAndCloseButton.attr({
  100. id: 'GM_config_save_closeBtn',
  101. title: 'Save and close window'
  102. }).text("Save and Close").click(function () {
  103. th.save(true);
  104. });
  105.  
  106. $('#GM_config_buttons_holder > :last-child', this.contentWindow.document || this.contentDocument).before(saveAndCloseButton);
  107. });
  108.  
  109. events.fire('SettingsOpen');
  110. },
  111. 'save': function (args) {
  112. events.fire('SettingsSave');
  113. },
  114. 'reset': function (args) {
  115. events.fire('SettingsReset');
  116. },
  117. 'close': function (args) {
  118. events.fire('SettingsClose');
  119. },
  120. 'change': function (args) {
  121. //fire an event for each setting that changed
  122. for (var setting in args) {
  123. if (args.hasOwnProperty(setting)) {
  124. events.fire('SettingChange[{0}]'.format(setting), [args[setting].old, args[setting].new]);
  125. }
  126. }
  127. }
  128.  
  129. }
  130. });
  131. events.on('SettingsSaveInternal', function (data) {
  132. gmc.save();
  133. if (data.close) {
  134. gmc.close();
  135. }
  136. });
  137. };
  138.  
  139. Settings.prototype.save = function (close) {
  140. "use strict";
  141. //post message to site and catch in the script scope fix for #21
  142. window.postMessage(JSON.stringify({
  143. action: 'SettingsSaveInternal',
  144. data: {
  145. close: close
  146. }
  147. }), "*");
  148. };
  149.  
  150. window.plugins = window.plugins || {};
  151. window.plugins.settings = new Settings("1");