Temporary-Script-Storage

Sets a maximum age (minimum one day) for script storage values, after which they are deleted

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/562172/1881822/Temporary-Script-Storage.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

作者
JasonMel
版本
1.0.4
创建于
2026-01-11
更新于
2026-07-21
大小
31.2 KB
许可证
MIT (https://mit-license.org/)

通过此类的实例设置和访问的存储将仅在您在构造函数中设置的生命周期内存在,但至少必须是一天。默认值为 30 天。之后,当调用 sweepExpiredEntries() 时,它将过期并被删除,您可以使用 setInterval() 安排此调用。

此库对通过 GM 的 setValue() 直接设置的脚本存储没有任何影响。这样的存储将正常运行。

使用示例:

const persistentStorage = new TTLStorage();
await persistentStorage.ready();
setInterval(() => persistentStorage.sweepExpiredEntries(), 1000 * 60 * 60 * 24);

try { 
  await persistentStorage.set("key1", "hello"); // throws
  const msg = persistentStorage.get("key1");    // fast memory read
  await persistentStorage.delete("key1");       // throws
} catch {}

此外,任何使用此库的脚本都必须包含以下元数据:

// @grant GM.getValue
// @grant GM.setValue
// @grant GM.deleteValue
// @grant GM.listValues
// @grant GM.info

这是必要的,因为将每个库所需的内容放在其自己的元数据中是不够的。顶级脚本必须为其所有依赖项提供必要的 @require@grant