userscript...

This commit is contained in:
Gabe Yuan
2023-08-05 16:32:49 +08:00
parent 1e82c280ad
commit f5dd215ae7
4 changed files with 19 additions and 7 deletions

View File

@@ -20,9 +20,15 @@ const banner = `// ==UserScript==
// @grant GM_xmlhttpRequest // @grant GM_xmlhttpRequest
// @grant GM.xmlhttpRequest // @grant GM.xmlhttpRequest
// @grant GM_setValue // @grant GM_setValue
// @grant GM_getValue
// @grant GM.setValue // @grant GM.setValue
// @grant GM_getValue
// @grant GM.getValue // @grant GM.getValue
// @grant GM_deleteValue
// @grant GM.deleteValue
// @grant GM_addValueChangeListener
// @grant GM.addValueChangeListener
// @grant GM_removeValueChangeListener
// @grant GM.removeValueChangeListener
// @connect translate.googleapis.com // @connect translate.googleapis.com
// @connect api-edge.cognitive.microsofttranslator.com // @connect api-edge.cognitive.microsofttranslator.com
// @connect edge.microsoft.com // @connect edge.microsoft.com

View File

@@ -72,7 +72,10 @@ export function StoragesProvider({ children }) {
if (isExt) { if (isExt) {
browser.storage.onChanged.removeListener(handleChanged); browser.storage.onChanged.removeListener(handleChanged);
} else if (isGm) { } else if (isGm) {
window.GM.removeValueChangeListener(handleChanged); (
window.GM_removeValueChangeListener ||
window.GM.removeValueChangeListener
)(handleChanged);
} else { } else {
window.removeEventListener("storage", handleChanged); window.removeEventListener("storage", handleChanged);
} }

View File

@@ -16,7 +16,7 @@ import {
const fetchGM = async (input, { method = "GET", headers, body } = {}) => const fetchGM = async (input, { method = "GET", headers, body } = {}) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
try { try {
window.GM_xmlhttpRequest({ (window.GM_xmlhttpRequest || window.GM.xmlhttpRequest)({
method, method,
url: input, url: input,
headers, headers,

View File

@@ -4,7 +4,7 @@ async function set(key, val) {
if (isExt) { if (isExt) {
await browser.storage.local.set({ [key]: val }); await browser.storage.local.set({ [key]: val });
} else if (isGm) { } else if (isGm) {
await window.GM.setValue(key, val); await (window.GM_setValue || window.GM.setValue)(key, val);
} else { } else {
const oldValue = window.localStorage.getItem(key); const oldValue = window.localStorage.getItem(key);
window.localStorage.setItem(key, val); window.localStorage.setItem(key, val);
@@ -24,7 +24,7 @@ async function get(key) {
const val = await browser.storage.local.get([key]); const val = await browser.storage.local.get([key]);
return val[key]; return val[key];
} else if (isGm) { } else if (isGm) {
const val = await window.GM.getValue(key); const val = await (window.GM_getValue || window.GM.getValue)(key);
return val; return val;
} }
return window.localStorage.getItem(key); return window.localStorage.getItem(key);
@@ -34,7 +34,7 @@ async function del(key) {
if (isExt) { if (isExt) {
await browser.storage.local.remove([key]); await browser.storage.local.remove([key]);
} else if (isGm) { } else if (isGm) {
await window.GM.deleteValue(key); await (window.GM_deleteValue || window.GM.deleteValue)(key);
} else { } else {
const oldValue = window.localStorage.getItem(key); const oldValue = window.localStorage.getItem(key);
window.localStorage.removeItem(key); window.localStorage.removeItem(key);
@@ -77,7 +77,10 @@ function onChanged(handleChanged) {
if (isExt) { if (isExt) {
browser.storage.onChanged.addListener(handleChanged); browser.storage.onChanged.addListener(handleChanged);
} else if (isGm) { } else if (isGm) {
window.GM.addValueChangeListener("storage", handleChanged); (window.GM_addValueChangeListener || window.GM.addValueChangeListener)(
"storage",
handleChanged
);
} else { } else {
window.addEventListener("storage", handleChanged); window.addEventListener("storage", handleChanged);
} }