userscript...

This commit is contained in:
Gabe Yuan
2023-08-05 20:11:02 +08:00
parent f5dd215ae7
commit b5f1da18a6
7 changed files with 96 additions and 37 deletions

View File

@@ -4,11 +4,18 @@ async function set(key, val) {
if (isExt) {
await browser.storage.local.set({ [key]: val });
} else if (isGm) {
const oldValue = await (window.GM_getValue || window.GM.getValue)(key);
await (window.GM_setValue || window.GM.setValue)(key, val);
window.dispatchEvent(
new StorageEvent("storage", {
key,
oldValue,
newValue: val,
})
);
} else {
const oldValue = window.localStorage.getItem(key);
window.localStorage.setItem(key, val);
// 手动唤起事件
window.dispatchEvent(
new StorageEvent("storage", {
key,
@@ -34,11 +41,18 @@ async function del(key) {
if (isExt) {
await browser.storage.local.remove([key]);
} else if (isGm) {
const oldValue = await (window.GM_getValue || window.GM.getValue)(key);
await (window.GM_deleteValue || window.GM.deleteValue)(key);
window.dispatchEvent(
new StorageEvent("storage", {
key,
oldValue,
newValue: null,
})
);
} else {
const oldValue = window.localStorage.getItem(key);
window.localStorage.removeItem(key);
// 手动唤起事件
window.dispatchEvent(
new StorageEvent("storage", {
key,
@@ -76,11 +90,6 @@ async function putObj(key, obj) {
function onChanged(handleChanged) {
if (isExt) {
browser.storage.onChanged.addListener(handleChanged);
} else if (isGm) {
(window.GM_addValueChangeListener || window.GM.addValueChangeListener)(
"storage",
handleChanged
);
} else {
window.addEventListener("storage", handleChanged);
}