lib:indexeddb ls

none

当前为 2024-05-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name lib:indexeddb ls
  3. // @version 5
  4. // @description none
  5. // @license GPLv3
  6. // @run-at document-start
  7. // @author You
  8. // @match *://*/*
  9. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAMAAABiM0N1AAAAAXNSR0IB2cksfwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAHJQTFRFAAAAEIijAo2yAI60BYyuF4WaFIifAY6zBI2wB4usGIaZEYigIoiZCIyrE4igG4iYD4mjEomhFoedCoqpDIqnDomlBYyvE4efEYmiDYqlA42xBoytD4mkCYqqGYSUFYidC4qoC4upAo6yCoupDYqmCYur4zowOQAAACZ0Uk5TAO////9vr////1+/D/+/L+/Pf/////+f3///////H4////////+5G91rAAACgUlEQVR4nM2Y22KjIBCGidg1264liZqDadK03X3/V2wNKHMC7MpF/xthHD5mgERAqZhWhfYqH6K+Qf2qNNf625hCoFj9/gblMUi5q5jLkXLCKudgyiRm0FMK82cWJp1fLbV5VmvJbCIc0GCYaFqqlDJgADdBjncqAXYobm1xh72aFMflbysteFfdy2Yi1XGOm5HGBzQ1dq7TzEoxjeNTjQZb7VA3e1c7+ImgasAgQ9+xusNVNZIo5xmOMgihIS2PbCQIiHEUdTvhxCcS/kPomfFI2zHy2PkWmA6aNatIJpKFJyekyy02xh5Y3DI9T4aOT6VhIUrsNTFp1pf79Z4SIIVDegl6IJO6cHiL/GimIZDhgTu/BlYWCQzHMl0zBWT/T3KAhtxOuUB9FtBrpsz0RV4xsjHmW+UCaffcSy/5viMGer0/6HdFNMZBq/vjJL38H9Dqx4Fuy0Em12DbZy+9pGtiDijbglwAehyj11n0tRD3WUBm+lwulE/8h4BuA+iWAQQnteg2Xm63WQLTpnMnpjdge0Mgu/GRPsV4xdjQ94Lfi624fabhDkfUqIKNrM64Q837v8yL0prasepCgrtvw1sJpoqanGEX7b5mQboNW8eawXaWXTMfMGxub472hzWzHSn6Sg2G9+6TAyRruE71s+zAzjWaknoyJCQzwxrghH2k5FDT4eqWunuNxyN9QCGcxVod5oADbYnIUkDTGZEf1xDJnSFteQ3KdsT8zYDMQXcHxsevcLH1TrsABzkNPyA/L7b0jg704viMMlpQI96WsHknCt/3YH0kOEo9zcGkwrFK39ck72rmoehmKqo2RKlilzSy/nJKEV45CT38myJp456fezktHjN5aeMAAAAASUVORK5CYII=
  10. // @grant none
  11. // @exclude /livereload.net\/files\/ffopen\/index.html$/
  12. // @namespace https://greasyfork.org/users/1184528
  13. // ==/UserScript==
  14. ;(() => {
  15. function isFilesystemHandle(obj) {
  16. // Check if the object is a FilesystemFileHandle or FilesystemDirectoryHandle
  17. return (
  18. obj &&
  19. (obj instanceof FileSystemFileHandle ||
  20. obj instanceof FileSystemDirectoryHandle)
  21. )
  22. }
  23. var x = loadlib("libloader")
  24. const a = loadlib("allfuncs", "indexeddb ls")
  25. x.savelib("indexeddb ls", async function newdbproxy(name) {
  26. var db = await a({
  27. storeName: name,
  28. keyPath: "id",
  29. }).indexeddb_setup()
  30. let indexedData = await a(db).indexeddb_getall() // Retrieve data from IndexedDB
  31. var localData = {} // Initialize local data object
  32. // Parse the retrieved data to match the expected format
  33. indexedData.forEach((item) => {
  34. localData[item.id] = item.val
  35. })
  36. function set(prop, val) {
  37. localData[prop] = val // Update local data
  38. a(db).indexeddb_set({ id: prop, val: val }) // Save to IndexedDB asynchronously
  39. }
  40. function remove(prop) {
  41. delete localData[prop]
  42. a(db).indexeddb_remove(prop) // Remove from IndexedDB asynchronously
  43. }
  44. // Create a proxy handler to intercept property access and modifications
  45. const dbProxyHandler = {
  46. set(target, prop, value) {
  47. target[prop] = value
  48. set(prop, value)
  49. return true
  50. },
  51. get(target, prop) {
  52. switch (prop) {
  53. case Symbol.iterator:
  54. var ld = Object.entries(localData).map(([id, val]) => {
  55. return { id, val }
  56. })
  57. return function* () {
  58. for (var i in ld) {
  59. yield ld[i]
  60. }
  61. }
  62. case Symbol.toStringTag:
  63. return "ls"
  64. case "clear":
  65. return async function () {
  66. Object.keys(localData).forEach((key) => {
  67. Reflect.deleteProperty(localData, key)
  68. })
  69. await a(db).indexeddb_clearall()
  70. return localData
  71. }
  72. case "all":
  73. return localData
  74. case "loadall":
  75. return async function () {
  76. localData = await a(db).indexeddb_getall()
  77. }
  78. case "saveall":
  79. return async function () {
  80. localData.forEach((item) => a(db).indexeddb_set(item))
  81. }
  82. default: // Update local data
  83. // Save changes to IndexedDB asynchronously
  84. var value = Reflect.get(target, prop)
  85. if (
  86. a(value).gettype(["object", "array"]).val &&
  87. !isFilesystemHandle(value)
  88. ) {
  89. value = new Proxy(value, {
  90. ...nestedProxyHandler,
  91. mainprop: prop,
  92. mainval: value,
  93. }) // Create a nested proxy for objects
  94. }
  95. return value
  96. }
  97. },
  98. deleteProperty(target, prop) {
  99. var val = Reflect.deleteProperty(target, prop)
  100. remove(prop)
  101. return val
  102. },
  103. }
  104. // Nested proxy handler for objects
  105. const nestedProxyHandler = {
  106. set(target, prop, value) {
  107. target[prop] = value // Update nested object
  108. set(this.mainprop, this.mainval) // Save changes to IndexedDB asynchronously
  109. return true
  110. },
  111. get(target, prop) {
  112. var val = target[prop]
  113. if (
  114. a(val).gettype(["object", "array"]).val &&
  115. !isFilesystemHandle(val)
  116. ) {
  117. return new Proxy(val, nestedProxyHandler) // Return a nested proxy for objects
  118. }
  119. return val
  120. },
  121. deleteProperty(target, prop) {
  122. var val = Reflect.deleteProperty(target, prop)
  123. set(this.mainprop, this.mainval)
  124. return val
  125. },
  126. }
  127. let proxy = new Proxy(localData, dbProxyHandler)
  128. // Create a proxy object for automatic syncing with IndexedDB
  129. return proxy
  130. })
  131. })()