SuperGM

Extends the GM_setValue and GM_getValue functions for any javascript variable type. basis on https://userscripts-mirror.org/scripts/source/107941.user.js

当前为 2019-08-21 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/389206/726970/SuperGM.js

  1. // ==UserScript==
  2. // @name SuperGM
  3. // @version 0.0.1
  4. // @description Extends the GM_setValue and GM_getValue functions for any javascript variable type. basis on https://userscripts-mirror.org/scripts/source/107941.user.js
  5. // @namespace userscripts.org/users/158640
  6. // ==/UserScript==
  7.  
  8. if (!String.prototype.startsWith)
  9. String.prototype.startsWith = function (str) {
  10. return this.slice(0, str.length) == str;
  11. };
  12. if (!String.prototype.endsWith)
  13. String.prototype.endsWith = function (str) {
  14. return this.slice(-str.length) == str;
  15. };
  16. if (!String.prototype.contains)
  17. String.prototype.contains = function (str) {
  18. return this.indexOf(str) > -1;
  19. };
  20. var SuperGM = function (version, _expiredMilliseconds) {
  21. var versionkeybasic = 'tsharp.org:key:v:';
  22. var expiredkeybasic = 'tsharp.org:key:e:';
  23. var JSON_MarkerStr = 'json_val: ';
  24. var FunctionMarker = 'function_code: ';
  25. var expiredMilliseconds = -1;
  26. if (typeof version != 'undefine') version = -1;
  27. if (typeof _expiredMilliseconds != 'undefine')
  28. expiredMilliseconds = -1;
  29. else
  30. expiredMilliseconds = _expiredMilliseconds;
  31. function ReportError(msg) {
  32. if (console && console.error)
  33. console.log(msg);
  34. else
  35. throw new Error(msg);
  36. }
  37.  
  38. //--- Check that the environment is proper.
  39. if (typeof GM_setValue != "function")
  40. ReportError('This library requires Greasemonkey! GM_setValue is missing.');
  41. if (typeof GM_getValue != "function")
  42. ReportError('This library requires Greasemonkey! GM_getValue is missing.');
  43. if (typeof version != 'integer') {
  44. ReportError('Arg version should be a integer type if you wana set it.');
  45. }
  46. if (typeof expiredMilliseconds != 'integer') {
  47. ReportError('Arg expiredMilliseconds should be a integer type if you wana set it.');
  48. }
  49.  
  50. this.set = function (varName, varValue) {
  51.  
  52. if (!varName) {
  53. ReportError('Illegal varName sent to GM_SuperValue.set().');
  54. return;
  55. }
  56. if (/[^\w _-]/.test(varName)) {
  57. ReportError('Suspect, probably illegal, varName sent to GM_SuperValue.set().');
  58. }
  59. var versionKey = versionkeybasic + varName;
  60. GM_setValue(versionKey, version);
  61. var expiredkey = expiredkeybasic + varName;
  62. GM_setValue(expiredkey, new Date().getTime());
  63. switch (typeof varValue) {
  64. case 'undefined':
  65. ReportError('Illegal varValue sent to GM_SuperValue.set().');
  66. break;
  67. case 'boolean':
  68. case 'string':
  69. //--- These 2 types are safe to store, as is.
  70. GM_setValue(varName, varValue);
  71. break;
  72. case 'number':
  73. /*--- Numbers are ONLY safe if they are integers.
  74. Note that hex numbers, EG 0xA9, get converted
  75. and stored as decimals, EG 169, automatically.
  76. That's a feature of JavaScript.
  77.  
  78. Also, only a 32-bit, signed integer is allowed.
  79. So we only process +/-2147483647 here.
  80. */
  81. if (varValue === parseInt(varValue) && Math.abs(varValue) < 2147483647) {
  82. GM_setValue(varName, varValue);
  83. break;
  84. }
  85. case 'object':
  86. /*--- For all other cases (but functions), and for
  87. unsafe numbers, store the value as a JSON string.
  88. */
  89. var safeStr = JSON_MarkerStr + JSON.stringify(varValue);
  90. GM_setValue(varName, safeStr);
  91. break;
  92. case 'function':
  93. /*--- Functions need special handling.
  94. */
  95. var safeStr = FunctionMarker + varValue.toString();
  96. GM_setValue(varName, safeStr);
  97. break;
  98.  
  99. default:
  100. ReportError('Unknown type in GM_SuperValue.set()!');
  101. break;
  102. }
  103. }//-- End of set()
  104.  
  105. this.get = function (varName, defaultValue) {
  106.  
  107. if (!varName) {
  108. ReportError('Illegal varName sent to GM_SuperValue.get().');
  109. return;
  110. }
  111. if (/[^\w _-]/.test(varName)) {
  112. ReportError('Suspect, probably illegal, varName sent to GM_SuperValue.get().');
  113. }
  114. var versionKey = versionkeybasic + varName;
  115. var savedversion = GM_getValue(versionKey);
  116. if (version != savedversion) { //需判断设置默认-1时
  117. if (typeof GM_deleteValue != "function") {
  118. GM_deleteValue(varName);
  119. GM_deleteValue(versionKey);
  120. }
  121. if (typeof defaultValue == 'function') {
  122. var varvalue = defaultValue();
  123. set(varName, varvalue);
  124. return varvalue;
  125. }
  126. else
  127. return defaultValue;
  128. }
  129. var expiredkey = expiredkeybasic + varName;
  130. var expire = GM_getValue(expiredkey);
  131. if (new Date().getTime() - expire > expiredMilliseconds) {
  132. if (typeof GM_deleteValue != "function") {
  133. GM_deleteValue(varName);
  134. GM_deleteValue(expiredkey);
  135. }
  136. }
  137. //--- Attempt to get the value from storage.
  138. var varValue = GM_getValue(varName);
  139. if (!varValue)
  140. return defaultValue;
  141.  
  142. //--- We got a value from storage. Now unencode it, if necessary.
  143. if (typeof varValue == "string") {
  144. //--- Is it a JSON value?
  145. var regxp = new RegExp('^' + JSON_MarkerStr + '(.+)$');
  146. var m = varValue.match(regxp);
  147. if (m && m.length > 1) {
  148. varValue = JSON.parse(m[1]);
  149. return varValue;
  150. }
  151.  
  152. //--- Is it a function?
  153. var regxp = new RegExp('^' + FunctionMarker + '((?:.|\n|\r)+)$');
  154. var m = varValue.match(regxp);
  155. if (m && m.length > 1) {
  156. varValue = eval('(' + m[1] + ')');
  157. return varValue;
  158. }
  159. }
  160.  
  161. return varValue;
  162. }//-- End of get()
  163. }