js-storeData

轻量级原生js本地数据存储管理工具(可选localstorage或GM油猴API)

Skrip ini tidak untuk dipasang secara langsung. Ini adalah pustaka skrip lain untuk disertakan dengan direktif meta // @require https://update.greatest.deepsurf.us/scripts/444155/1045094/js-storeData.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

  1. // ==UserScript==
  2. // @name js-storeData
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 轻量级原生js本地数据存储管理工具(可选localstorage或GM油猴API)
  6. // @author tutu辣么可爱(greasyfork) IcedWatermelonJuice(github)
  7. // @dayr 2022.4.29 GMT+0800 (中国标准时间)
  8. // @license MIT License
  9. // @note 相关参考信息请前往greasyfork仓库或github仓库
  10. // @note greasyfork仓库:https://greatest.deepsurf.us/zh-CN/scripts/444155-js-storedata
  11. // @note github仓库:https://github.com/IcedWatermelonJuice/js-storeData
  12. // ==/UserScript==
  13. var storeDataJS = function(dataKey, defaultData, isGM = false) {
  14. var defaultGM = function() {
  15. console.log("未获得GM油猴API,请确认是否已授权");
  16. return null;
  17. }
  18. this.check = function(data) {
  19. try {
  20. data = JSON.stringify(data);
  21. data = JSON.parse(data);
  22. if (data && typeof data === "object") {
  23. return true
  24. } else {
  25. return false
  26. }
  27. } catch (e) {
  28. return false
  29. }
  30. }
  31. this.dataKey = dataKey;
  32. this.defaultData = this.check(defaultData) ? defaultData : {};
  33. this.isGM = isGM;
  34. this.data = {};
  35. this.defaultGet = function(key) {
  36. return isGM ? (typeof GM_getValue === "function" ? GM_getValue(key) : defaultFn(key)) : localStorage
  37. .getItem(key)
  38. }
  39. this.defaultSet = function(key, val) {
  40. return isGM ? (typeof GM_setValue === "function" ? GM_setValue(key, val) : defaultFn(key, val)) :
  41. localStorage.setItem(key, val)
  42. }
  43. this.defaultRemove = function(key) {
  44. return isGM ? (typeof GM_deleteValue === "function" ? GM_deleteValue(key) : defaultFn(key)) :
  45. localStorage.removeItem(key)
  46. }
  47. this.save = function() {
  48. if (this.check(this.data)) {
  49. this.defaultSet(this.dataKey, JSON.stringify(this.data))
  50. } else {
  51. console.log("保存失败!待保存数据损坏")
  52. }
  53. }
  54. this.init = function() {
  55. let data = this.defaultGet(this.dataKey);
  56. try {
  57. data = data ? JSON.parse(data) : {};
  58. } catch (e) {
  59. data = {}
  60. }
  61. data = {
  62. ...this.defaultData,
  63. ...data
  64. };
  65. this.data = data;
  66. }
  67. this.remove = function() {
  68. this.defaultRemove(this.dataKey);
  69. }
  70. this.init();
  71. }
  72. storeDataJS.prototype = {
  73. copy: function(data) {
  74. return JSON.parse(JSON.stringify(data));
  75. },
  76. get: function(key, isDefault = false) {
  77. let data = this.copy(isDefault ? this.defaultData : this.data);
  78. return key ? data[key] : data;
  79. },
  80. set: function(key, val, isSave = false) {
  81. if (val !== undefined && val !== null) {
  82. this.data[key] = val;
  83. } else if (this.check(key)) {
  84. this.data = key;
  85. }
  86. if (isSave) {
  87. this.save();
  88. }
  89. },
  90. reset: function(isSave = false) {
  91. let data = this.get(null, true);
  92. this.set(data, null, isSave);
  93. },
  94. delete: function(key, isSave = false) {
  95. delete this.data[key];
  96. if (isSave) {
  97. this.save();
  98. }
  99. }
  100. }