GreaseMonkey_SuperValues

Extends the GM_setValue and GM_getValue functions for any javascript variable type.

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

  1. /***************************************************************************************
  2. ****************************************************************************************
  3. ***** Super GM_setValue and GM_getValue.js
  4. *****
  5. ***** This library extends the Greasemonkey GM_setValue and GM_getValue functions to
  6. ***** handle any javascript variable type.
  7. *****
  8. ***** Add it to your GM script with:
  9. *****
  10. *****
  11. ***** Usage:
  12. ***** GM_SuperValue.set (varName, varValue);
  13. ***** var x = GM_SuperValue.get (varName, defaultValue);
  14. *****
  15. ***** Test mode:
  16. ***** GM_SuperValue.runTestCases (bUseConsole);
  17. *****
  18. */
  19.  
  20. var GM_SuperValue = new function() {
  21.  
  22. var JSON_MarkerStr = 'json_val: ';
  23. var FunctionMarker = 'function_code: ';
  24.  
  25. function ReportError(msg) {
  26. if (console && console.error)
  27. console.error(msg);
  28. else
  29. throw new Error(msg);
  30. }
  31.  
  32. //--- Check that the environment is proper.
  33. if (typeof(GM_setValue) != "function")
  34. ReportError('This library requires Greasemonkey! GM_setValue is missing.');
  35. if (typeof(GM_getValue) != "function")
  36. ReportError('This library requires Greasemonkey! GM_getValue is missing.');
  37.  
  38.  
  39. /*--- set ()
  40. GM_setValue (http://wiki.greasespot.net/GM_setValue) only stores:
  41. strings, booleans, and integers (a limitation of using Firefox
  42. preferences for storage).
  43.  
  44. This function extends that to allow storing any data type.
  45.  
  46. Parameters:
  47. varName
  48. String: The unique (within this script) name for this value.
  49. Should be restricted to valid Javascript identifier characters.
  50. varValue
  51. Any valid javascript value. Just note that it is not advisable to
  52. store too much data in the Firefox preferences.
  53.  
  54. Returns:
  55. undefined
  56. */
  57. this.set = function(varName, varValue) {
  58.  
  59. if (typeof varName != "string") {
  60. ReportError('Illegal varName sent to GM_SuperValue.set(). Name: ' + varName + ' Value: ' + varValue);
  61. return;
  62. }
  63. if (/[^\w _-]/.test(varName)) {
  64. ReportError('Suspect, probably illegal, varName sent to GM_SuperValue.set(). Name: ' + varName + ' Value: ' + varValue);
  65. }
  66.  
  67. switch (typeof varValue) {
  68. case 'undefined':
  69. ReportError('Illegal varValue sent to GM_SuperValue.set(). Name: ' + varName + ' Value: ' + varValue);
  70. break;
  71. case 'boolean':
  72. case 'string':
  73. //--- These 2 types are safe to store, as is.
  74. GM_setValue(varName, varValue);
  75. break;
  76. case 'number':
  77. /*--- Numbers are ONLY safe if they are integers.
  78. Note that hex numbers, EG 0xA9, get converted
  79. and stored as decimals, EG 169, automatically.
  80. That's a feature of JavaScript.
  81.  
  82. Also, only a 32-bit, signed integer is allowed.
  83. So we only process +/-2147483647 here.
  84. */
  85. if (varValue === parseInt(varValue) && Math.abs(varValue) < 2147483647) {
  86. GM_setValue(varName, varValue);
  87. break;
  88. }
  89. case 'object':
  90. /*--- For all other cases (but functions), and for
  91. unsafe numbers, store the value as a JSON string.
  92. */
  93. var safeStr = JSON_MarkerStr + JSON.stringify(varValue);
  94. GM_setValue(varName, safeStr);
  95. break;
  96. case 'function':
  97. /*--- Functions need special handling.
  98. */
  99. var safeStr = FunctionMarker + varValue.toString();
  100. GM_setValue(varName, safeStr);
  101. break;
  102.  
  103. default:
  104. ReportError('Unknown type in GM_SuperValue.set()! Name: ' + varName + ' DefaultValue: ' + varValue);
  105. break;
  106. }
  107. }; //-- End of set()
  108.  
  109.  
  110. /*--- get ()
  111. GM_getValue (http://wiki.greasespot.net/GM_getValue) only retieves:
  112. strings, booleans, and integers (a limitation of using Firefox
  113. preferences for storage).
  114.  
  115. This function extends that to allow retrieving any data type -- as
  116. long as it was stored with GM_SuperValue.set().
  117.  
  118. Parameters:
  119. varName
  120. String: The property name to get. See GM_SuperValue.set for details.
  121. defaultValue
  122. Optional. Any value to be returned, when no value has previously
  123. been set.
  124.  
  125. Returns:
  126. When this name has been set...
  127. The variable or function value as previously set.
  128.  
  129. When this name has not been set, and a default is provided...
  130. The value passed in as a default
  131.  
  132. When this name has not been set, and default is not provided...
  133. undefined
  134. */
  135. this.get = function(varName, defaultValue) {
  136.  
  137. if (typeof varName != "string") {
  138. ReportError('Illegal varName sent to GM_SuperValue.get(). Name: ' + varName + ' DefaultValue: ' + defaultValue);
  139. return;
  140. }
  141. if (/[^\w _-]/.test(varName)) {
  142. ReportError('Suspect, probably illegal, varName sent to GM_SuperValue.get(). Name: ' + varName + ' DefaultValue: ' + defaultValue);
  143. }
  144.  
  145. //--- Attempt to get the value from storage.
  146. var varValue = GM_getValue(varName);
  147. if (typeof varValue == "undefined")
  148. return defaultValue;
  149.  
  150. //--- We got a value from storage. Now unencode it, if necessary.
  151. if (typeof varValue == "string") {
  152. //--- Is it a JSON value?
  153. var regxp = new RegExp('^' + JSON_MarkerStr + '(.+)$');
  154. var m = varValue.match(regxp);
  155. if (m && m.length > 1) {
  156. varValue = JSON.parse(m[1]);
  157. return varValue;
  158. }
  159.  
  160. //--- Is it a function?
  161. var regxp = new RegExp('^' + FunctionMarker + '((?:.|\n|\r)+)$');
  162. var m = varValue.match(regxp);
  163. if (m && m.length > 1) {
  164. varValue = eval('(' + m[1] + ')');
  165. return varValue;
  166. }
  167. }
  168.  
  169. return varValue;
  170. }; //-- End of get()
  171. };