GM storage wrapper

simple wrapper for GM_storage with added functions

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/38717/253024/GM%20storage%20wrapper.js

  1. /*jshint esversion: 6 */
  2. // ==UserScript==
  3. // @name GM storage wrapper
  4. // @version 1.0
  5. // @description simple wrapper for GM_storage with added functions
  6. // ==/UserScript==
  7.  
  8. var gmStorageWrapper = {
  9. options : {
  10. prefix : ''
  11. },
  12. // “Set” means “add if absent, replace if present.”
  13. set : function(key, value) {
  14. let storageVals = this.read(key);
  15.  
  16. if (typeof storageVals === 'undefined' || !storageVals) {
  17. // add if absent
  18. return this.add(key, value);
  19. } else {
  20. // replace if present
  21. this.write(key, value);
  22. return true;
  23. }
  24. },
  25. // “Add” means “add if absent, do nothing if present” (if a uniquing collection).
  26. add : function(key, value) {
  27. let storageVals = this.read(key, false);
  28.  
  29. if (typeof storageVals === 'undefined' || !storageVals) {
  30. this.write(key, value);
  31. return true;
  32. } else {
  33. if (this._isArray(storageVals)) { // is array
  34. let index = storageVals.indexOf(value);
  35.  
  36. if (index !== -1) {
  37. // do nothing if present
  38. return false;
  39. } else {
  40. // add if absent
  41. storageVals.push(value);
  42. this.write(key, storageVals);
  43. return true;
  44. }
  45. } else if (this._isObject(storageVals)) { // is object
  46. // merge obj value on obj
  47. let result,
  48. objToMerge = value;
  49.  
  50. result = Object.assign(storageVals, objToMerge);
  51. this.write(key, result);
  52. return false;
  53. }
  54. return false;
  55. }
  56. },
  57. // “Replace” means “replace if present, do nothing if absent.”
  58. replace : function(key, itemFind, itemReplacement) {
  59. let storageVals = this.read(key, false);
  60.  
  61. if (typeof storageVals === 'undefined' || !storageVals) {
  62. // do nothing if absent
  63. return false;
  64. } else {
  65. if (this._isArray(storageVals)) { // is Array
  66. let index = storageVals.indexOf(itemFind);
  67.  
  68. if (index !== -1) {
  69. // replace if present
  70. storageVals[index] = itemReplacement;
  71. this.write(key, storageVals);
  72. return true;
  73. } else {
  74. // do nothing if absent
  75. return false;
  76. }
  77. } else if (this._isObject(storageVals)) {
  78. // is Object
  79. // replace property's value
  80. storageVals[itemFind] = itemReplacement;
  81. this.write(key, storageVals);
  82. return true;
  83. }
  84. return false;
  85. }
  86. },
  87. // “Remove” means “remove if present, do nothing if absent.”
  88. remove : function(key, value) {
  89. if (typeof value === 'undefined') { // remove key
  90. this.delete(key);
  91. return true;
  92. } else { // value present
  93. let storageVals = this.read(key);
  94.  
  95. if (typeof storageVals === 'undefined' || !storageVals) {
  96. return true;
  97. } else {
  98. if (this._isArray(storageVals)) { // is Array
  99. let index = storageVals.indexOf(value);
  100.  
  101. if (index !== -1) {
  102. // remove if present
  103. storageVals.splice(index, 1);
  104. this.write(key, storageVals);
  105. return true;
  106. } else {
  107. // do nothing if absent
  108. return false;
  109. }
  110. } else if (this._isObject(storageVals)) { // is Object
  111. let property = value;
  112.  
  113. delete storageVals[property];
  114. this.write(key, storageVals);
  115. return true;
  116. }
  117. return false;
  118. }
  119. }
  120. },
  121. get : function(key, defaultValue) {
  122. return this.read(key, defaultValue);
  123. },
  124.  
  125. // GM storage API
  126. read : function(key, defaultValue) {
  127. return this.unserialize(GM_getValue(this._prefix(key), defaultValue));
  128. },
  129. write : function(key, value) {
  130. return GM_setValue(this._prefix(key), this.serialize(value));
  131. },
  132. delete : function(key) {
  133. return GM_deleteValue(this._prefix(key));
  134. },
  135. readKeys : function() {
  136. return GM_listValues();
  137. },
  138. // /GM Storage API
  139.  
  140. getAll : function() {
  141. const keys = this._listKeys();
  142. let obj = {};
  143.  
  144. for (let i = 0, len = keys.length; i < len; i++) {
  145. obj[keys[i]] = this.read(keys[i]);
  146. }
  147. return obj;
  148. },
  149. getKeys : function() {
  150. return this._listKeys();
  151. },
  152. getPrefix : function() {
  153. return this.options.prefix;
  154. },
  155.  
  156. empty : function() {
  157. const keys = this._listKeys();
  158.  
  159. for (let i = 0, len = keys.lenght; i < len; i++) {
  160. this.delete(keys[i]);
  161. }
  162. },
  163. has : function(key) {
  164. return this.get(key) !== null;
  165. },
  166. forEach : function(callbackFunc) {
  167. const allContent = this.getAll();
  168.  
  169. for (let prop in allContent) {
  170. callbackFunc(prop, allContent[prop]);
  171. }
  172. },
  173. unserialize : function(value) {
  174. if (this._isJson(value)) {
  175. return JSON.parse(value);
  176. }
  177. return value;
  178. },
  179. serialize : function(value) {
  180. if (this._isJson(value)) {
  181. return JSON.stringify(value);
  182. }
  183. return value;
  184. },
  185. _listKeys : function(usePrefix = false) {
  186. const prefixed = this.readKeys();
  187. let unprefixed = [];
  188.  
  189. if (usePrefix) {
  190. return prefixed;
  191. } else {
  192. for (let i = 0, len = prefixed.length; i < len; i++) {
  193. unprefixed[i] = this._unprefix(prefixed[i]);
  194. }
  195. return unprefixed;
  196. }
  197. },
  198. _prefix : function(key) {
  199. return this.options.prefix + key;
  200. },
  201. _unprefix : function(key) {
  202. return key.substring(this.options.prefix.length);
  203. },
  204. _isJson : function(item) {
  205. try {
  206. JSON.parse(item);
  207. } catch (e) {
  208. return false;
  209. }
  210. return true;
  211. },
  212. _isObject : function(a) {
  213. return (!!a) && (a.constructor === Object);
  214. },
  215. _isArray : function(a) {
  216. return (!!a) && (a.constructor === Array);
  217. }
  218. };

QingJ © 2025

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