lskdb

locale storage key database

目前為 2024-06-07 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name lskdb
  3. // @description locale storage key database
  4. // @namespace http://tampermonkey.net/
  5. // @author smartacephale
  6. // @license MIT
  7. // @version 1.0
  8. // @match *://*/*
  9. // ==/UserScript==
  10.  
  11. /*
  12. raison d'etre for this insane degenerative garbage:
  13. if you have to store 1000+ unique keys,
  14. you don't have to parse whole json array to check one value
  15. */
  16.  
  17. class LSKDB {
  18. lockKey = 'lsmngr-lock';
  19. prefix = 'lsm-';
  20.  
  21. constructor() {
  22. // migration
  23. const old = localStorage.getItem('lsmngr');
  24. if (!old) return;
  25. const list = JSON.parse(old);
  26. localStorage.removeItem('lsmngr');
  27. list.forEach(l => {
  28. const i = localStorage.getItem(l);
  29. if (i) {
  30. const v = JSON.parse(i);
  31. v.forEach(x => this.setKey(x));
  32. }
  33. localStorage.removeItem(l);
  34. });
  35. }
  36.  
  37. getKeys(n = 12) {
  38. const res = [];
  39. for (const key in localStorage) {
  40. if (res.length >= n) break;
  41. if (key.startsWith(this.prefix)) {
  42. res.push(key);
  43. }
  44. }
  45. res.forEach(k => localStorage.removeItem(k));
  46. return res.map(r => r.slice(this.prefix.length));
  47. }
  48.  
  49. hasKey(key) {
  50. return localStorage.hasOwnProperty(`${this.prefix}${key}`);
  51. }
  52.  
  53. setKey(key) {
  54. localStorage.setItem(`${this.prefix}${key}`, '');
  55. }
  56.  
  57. isLocked() {
  58. const lock = localStorage.getItem(this.lockKey);
  59. const locktime = 5*60*1000;
  60. return !(!lock || Date.now() - lock > locktime);
  61. }
  62.  
  63. lock(value) {
  64. if (value) {
  65. localStorage.setItem(this.lockKey, Date.now());
  66. } else {
  67. localStorage.removeItem(this.lockKey);
  68. }
  69. }
  70. }

QingJ © 2025

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