存储(wad.storage.*)
类 Chrome 扩展的持久存储,面板与逻辑层可用。自动命名空间:插件自己的 key 会存为 __wad_<pluginId>_<key>,与业务 storage 互不污染。
API
| 方法 | 签名 | 说明 |
|---|---|---|
wad.storage.get | get(key) → Promise<any> | 取值,无值返回 null / undefined |
wad.storage.set | set(key, value) → Promise | 写值,任意 JSON 可序列化 |
wad.storage.remove | remove(key) → Promise | 删除 |
wad.storage.keys | keys() → Promise<string[]> | 本插件全部 key(不含前缀) |
状态记忆
js
// 面板:记住上次的筛选条件
const last = await wad.storage.get('filter');
const filter = last || 'all';
await wad.storage.set('filter', filter);
await wad.storage.set('history', [1, 2, 3]);
const keys = await wad.storage.keys(); // ['filter', 'history']
await wad.storage.remove('history');逻辑层与面板共享状态
js
// 逻辑层:业务 token 变化时记录
wad.on('storageWrite', function (e) {
if (e.key === 'session_token') {
wx.setStorageSync('__wad_tokenwatch_token', e.value);
}
});
wad.register('lastToken', function () {
return wx.getStorageSync('__wad_tokenwatch_token');
});
// 面板读取
const token = await wad.logic.call('lastToken');两条铁律
- 直接用
wx.setStorageSync写业务 key 是可见的(会触发storageWrite),但不要绕过wad.storage写自己的数据 —— 命名空间会丢; - 绕过 wx 的 native 写入不可见:
storageWrite只覆盖wx.setStorage* / removeStorage* / clearStorage*。
安装即恢复
js
wad.ready.then(async function () {
const cfg = (await wad.storage.get('cfg')) || {};
if (cfg.autoStart) startCapture(cfg.keyword);
});面板每次都是新 iframe
所有状态都应放 storage 或逻辑层,内存变量只在当次面板生命周期内有效。