GM_config_lz-string

Refactor GM_config, this version uses lz-string to access data for a Library script

当前为 2018-10-02 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/372760/633378/GM_config_lz-string.js

  1. // ==UserScript==
  2. // @namespace http://tampermonkey.net/
  3. // @exclude *
  4.  
  5. // ==UserLibrary==
  6. // @name GM_config_lz-string
  7. // @description Refactor GM_config, this version uses lz-string to access data for a Library script
  8. // @author avan
  9. // @license MIT
  10. // @version 0.3
  11.  
  12. // ==/UserScript==
  13.  
  14. // ==/UserLibrary==
  15.  
  16. GM_configStruct.prototype.read = function (store) {
  17. var rval, cKey, dValue;
  18. try {
  19. cKey = LZString.compressToUTF16(store || this.id);
  20. dValue = LZString.decompressFromUTF16(this.getValue(cKey, '{}'));
  21. rval = this.parser(dValue);
  22. } catch(e) {
  23. this.log("GM_config failed to read saved settings!");
  24. rval = {};
  25. }
  26. return rval;
  27. };
  28.  
  29. GM_configStruct.prototype.write = function (store, obj) {
  30. if (!obj) {
  31. var values = {},
  32. forgotten = {},
  33. fields = this.fields;
  34.  
  35. for (var id in fields) {
  36. var field = fields[id];
  37. var value = field.toValue();
  38.  
  39. if (field.save) {
  40. if (value !== null) {
  41. values[id] = value;
  42. field.value = value;
  43. } else
  44. values[id] = field.value;
  45. } else
  46. forgotten[id] = value;
  47. }
  48. }
  49. try {
  50. var cKey = LZString.compressToUTF16(store || this.id),
  51. cValue = LZString.compressToUTF16(this.stringify(obj || values));
  52. this.setValue(cKey, cValue);
  53. } catch(e) {
  54. this.log("GM_config failed to save settings!");
  55. }
  56.  
  57. return forgotten;
  58. };
  59. GM_configField.prototype.toNode = function() {
  60. var field = this.settings,
  61. value = this.value,
  62. options = field.options,
  63. type = field.type,
  64. className = field.class,
  65. style = field.style,
  66. id = this.id,
  67. configId = this.configId,
  68. labelPos = field.labelPos,
  69. create = this.create;
  70. function addLabel(pos, labelEl, parentNode, beforeEl) {
  71. if (!beforeEl) beforeEl = parentNode.firstChild;
  72. switch (pos) {
  73. case 'right':
  74. case 'below':
  75. if (pos == 'below')
  76. parentNode.appendChild(create('br', {}));
  77. parentNode.appendChild(labelEl);
  78. break;
  79. default:
  80. if (pos == 'above')
  81. parentNode.insertBefore(create('br', {}), beforeEl);
  82. parentNode.insertBefore(labelEl, beforeEl);
  83. }
  84. }
  85. var retNode = create('div', {
  86. className: 'config_var' + (className ? " " + className : ''),
  87. id: configId + '_' + id + '_var',
  88. title: field.title || '',
  89. style: style || '',
  90. }),
  91. firstProp;
  92. // Retrieve the first prop
  93. for (var i in field) {
  94. firstProp = i;
  95. break;
  96. }
  97. var label = field.label && type != "button" ?
  98. create('label', {
  99. id: configId + '_' + id + '_field_label',
  100. for: configId + '_field_' + id,
  101. className: 'field_label'
  102. }, field.label) : null;
  103. switch (type) {
  104. case 'textarea':
  105. retNode.appendChild((this.node = create('textarea', {
  106. innerHTML: value,
  107. id: configId + '_field_' + id,
  108. className: 'block',
  109. cols: (field.cols ? field.cols : 20),
  110. rows: (field.rows ? field.rows : 2)
  111. })));
  112. break;
  113. case 'radio':
  114. var wrap = create('div', {
  115. id: configId + '_field_' + id
  116. });
  117. this.node = wrap;
  118. for (var i = 0, len = options.length; i < len; ++i) {
  119. var radLabel = create('label', {
  120. className: 'radio_label'
  121. }, options[i]);
  122. var rad = wrap.appendChild(create('input', {
  123. value: options[i],
  124. type: 'radio',
  125. name: id,
  126. checked: options[i] == value
  127. }));
  128. var radLabelPos = labelPos &&
  129. (labelPos == 'left' || labelPos == 'right') ?
  130. labelPos : firstProp == 'options' ? 'left' : 'right';
  131. addLabel(radLabelPos, radLabel, wrap, rad);
  132. }
  133. retNode.appendChild(wrap);
  134. break;
  135. case 'select':
  136. var wrap = create('select', {
  137. id: configId + '_field_' + id
  138. });
  139. this.node = wrap;
  140. for (var i = 0, len = options.length; i < len; ++i) {
  141. var option = options[i];
  142. wrap.appendChild(create('option', {
  143. value: option,
  144. selected: option == value
  145. }, option));
  146. }
  147. retNode.appendChild(wrap);
  148. break;
  149. default: // fields using input elements
  150. var props = {
  151. id: configId + '_field_' + id,
  152. type: type,
  153. value: type == 'button' ? field.label : value
  154. };
  155. switch (type) {
  156. case 'checkbox':
  157. props.checked = value;
  158. break;
  159. case 'button':
  160. props.size = field.size ? field.size : 25;
  161. if (field.script) field.click = field.script;
  162. if (field.click) props.onclick = field.click;
  163. break;
  164. case 'hidden':
  165. break;
  166. case 'password':
  167. props.size = field.size ? field.size : 25;
  168. break;
  169. default:
  170. // type = text, int, or float
  171. props.type = 'text';
  172. props.size = field.size ? field.size : 25;
  173. }
  174. retNode.appendChild((this.node = create('input', props)));
  175. }
  176. if (label) {
  177. // If the label is passed first, insert it before the field
  178. // else insert it after
  179. if (!labelPos)
  180. labelPos = firstProp == "label" || type == "radio" ?
  181. "left" : "right";
  182. addLabel(labelPos, label, retNode);
  183. }
  184. return retNode;
  185. };

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址