myStorage

myStorage localstorage

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

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

QingJ © 2025

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