🏠 Home 

GM storage wrapper

simple wrapper for GM_storage with added functions

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greasyfork.org/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. var gmStorageWrapper = {
  8. options : {
  9. prefix : ''
  10. },
  11. // “Set” means “add if absent, replace if present.”
  12. set : function(key, value) {
  13. let storageVals = this.read(key);
  14. if (typeof storageVals === 'undefined' || !storageVals) {
  15. // add if absent
  16. return this.add(key, value);
  17. } else {
  18. // replace if present
  19. this.write(key, value);
  20. return true;
  21. }
  22. },
  23. // “Add” means “add if absent, do nothing if present” (if a uniquing collection).
  24. add : function(key, value) {
  25. let storageVals = this.read(key, false);
  26. if (typeof storageVals === 'undefined' || !storageVals) {
  27. this.write(key, value);
  28. return true;
  29. } else {
  30. if (this._isArray(storageVals)) { // is array
  31. let index = storageVals.indexOf(value);
  32. if (index !== -1) {
  33. // do nothing if present
  34. return false;
  35. } else {
  36. // add if absent
  37. storageVals.push(value);
  38. this.write(key, storageVals);
  39. return true;
  40. }
  41. } else if (this._isObject(storageVals)) { // is object
  42. // merge obj value on obj
  43. let r###lt,
  44. objToMerge = value;
  45. r###lt = Object.assign(storageVals, objToMerge);
  46. this.write(key, r###lt);
  47. return false;
  48. }
  49. return false;
  50. }
  51. },
  52. // “Replace” means “replace if present, do nothing if absent.”
  53. replace : function(key, itemFind, itemReplacement) {
  54. let storageVals = this.read(key, false);
  55. if (typeof storageVals === 'undefined' || !storageVals) {
  56. // do nothing if absent
  57. return false;
  58. } else {
  59. if (this._isArray(storageVals)) { // is Array
  60. let index = storageVals.indexOf(itemFind);
  61. if (index !== -1) {
  62. // replace if present
  63. storageVals[index] = itemReplacement;
  64. this.write(key, storageVals);
  65. return true;
  66. } else {
  67. // do nothing if absent
  68. return false;
  69. }
  70. } else if (this._isObject(storageVals)) {
  71. // is Object
  72. // replace property's value
  73. storageVals[itemFind] = itemReplacement;
  74. this.write(key, storageVals);
  75. return true;
  76. }
  77. return false;
  78. }
  79. },
  80. // “Remove” means “remove if present, do nothing if absent.”
  81. remove : function(key, value) {
  82. if (typeof value === 'undefined') { // remove key
  83. this.delete(key);
  84. return true;
  85. } else { // value present
  86. let storageVals = this.read(key);
  87. if (typeof storageVals === 'undefined' || !storageVals) {
  88. return true;
  89. } else {
  90. if (this._isArray(storageVals)) { // is Array
  91. let index = storageVals.indexOf(value);
  92. if (index !== -1) {
  93. // remove if present
  94. storageVals.splice(index, 1);
  95. this.write(key, storageVals);
  96. return true;
  97. } else {
  98. // do nothing if absent
  99. return false;
  100. }
  101. } else if (this._isObject(storageVals)) { // is Object
  102. let property = value;
  103. delete storageVals[property];
  104. this.write(key, storageVals);
  105. return true;
  106. }
  107. return false;
  108. }
  109. }
  110. },
  111. get : function(key, defaultValue) {
  112. return this.read(key, defaultValue);
  113. },
  114. // GM storage API
  115. read : function(key, defaultValue) {
  116. return this.unserialize(GM_getValue(this._prefix(key), defaultValue));
  117. },
  118. write : function(key, value) {
  119. return GM_setValue(this._prefix(key), this.serialize(value));
  120. },
  121. delete : function(key) {
  122. return GM_deleteValue(this._prefix(key));
  123. },
  124. readKeys : function() {
  125. return GM_listValues();
  126. },
  127. // /GM Storage API
  128. getAll : function() {
  129. const keys = this._listKeys();
  130. let obj = {};
  131. for (let i = 0, len = keys.length; i < len; i++) {
  132. obj[keys[i]] = this.read(keys[i]);
  133. }
  134. return obj;
  135. },
  136. getKeys : function() {
  137. return this._listKeys();
  138. },
  139. getPrefix : function() {
  140. return this.options.prefix;
  141. },
  142. empty : function() {
  143. const keys = this._listKeys();
  144. for (let i = 0, len = keys.lenght; i < len; i++) {
  145. this.delete(keys[i]);
  146. }
  147. },
  148. has : function(key) {
  149. return this.get(key) !== null;
  150. },
  151. forEach : function(callbackFunc) {
  152. const allContent = this.getAll();
  153. for (let prop in allContent) {
  154. callbackFunc(prop, allContent[prop]);
  155. }
  156. },
  157. unserialize : function(value) {
  158. if (this._isJson(value)) {
  159. return JSON.parse(value);
  160. }
  161. return value;
  162. },
  163. serialize : function(value) {
  164. if (this._isJson(value)) {
  165. return JSON.stringify(value);
  166. }
  167. return value;
  168. },
  169. _listKeys : function(usePrefix = false) {
  170. const prefixed = this.readKeys();
  171. let unprefixed = [];
  172. if (usePrefix) {
  173. return prefixed;
  174. } else {
  175. for (let i = 0, len = prefixed.length; i < len; i++) {
  176. unprefixed[i] = this._unprefix(prefixed[i]);
  177. }
  178. return unprefixed;
  179. }
  180. },
  181. _prefix : function(key) {
  182. return this.options.prefix + key;
  183. },
  184. _unprefix : function(key) {
  185. return key.substring(this.options.prefix.length);
  186. },
  187. _isJson : function(item) {
  188. try {
  189. JSON.parse(item);
  190. } catch (e) {
  191. return false;
  192. }
  193. return true;
  194. },
  195. _isObject : function(a) {
  196. return (!!a) && (a.constructor === Object);
  197. },
  198. _isArray : function(a) {
  199. return (!!a) && (a.constructor === Array);
  200. }
  201. };