fix GM.xmlHttpRequest & use GM/unsafeWindow global

This commit is contained in:
Gabe Yuan
2023-08-16 11:38:53 +08:00
parent 819018c3bd
commit f1f15c7eb1
4 changed files with 33 additions and 37 deletions

View File

@@ -34,7 +34,11 @@
"extends": [ "extends": [
"react-app", "react-app",
"react-app/jest" "react-app/jest"
] ],
"globals": {
"GM": true,
"unsafeWindow": true
}
}, },
"browserslist": { "browserslist": {
"production": [ "production": [

View File

@@ -21,8 +21,7 @@ import { msAuth } from "./auth";
*/ */
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 { GM.xmlHttpRequest({
(window.GM_xmlhttpRequest || window.GM.xmlhttpRequest)({
method, method,
url: input, url: input,
headers, headers,
@@ -31,7 +30,7 @@ const fetchGM = async (input, { method = "GET", headers, body } = {}) =>
if (response.status === 200) { if (response.status === 200) {
const headers = new Headers(); const headers = new Headers();
response.responseHeaders.split("\n").forEach((line) => { response.responseHeaders.split("\n").forEach((line) => {
let [name, value] = line.split(":").map((item) => item.trim()); const [name, value] = line.split(":").map((item) => item.trim());
if (name && value) { if (name && value) {
headers.append(name, value); headers.append(name, value);
} }
@@ -43,9 +42,6 @@ const fetchGM = async (input, { method = "GET", headers, body } = {}) =>
}, },
onerror: reject, onerror: reject,
}); });
} catch (error) {
reject(error);
}
}); });
/** /**

View File

@@ -4,8 +4,8 @@ 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) {
const oldValue = await (window.GM_getValue || window.GM.getValue)(key); const oldValue = await GM.getValue(key);
await (window.GM_setValue || window.GM.setValue)(key, val); await GM.setValue(key, val);
window.dispatchEvent( window.dispatchEvent(
new StorageEvent("storage", { new StorageEvent("storage", {
key, key,
@@ -31,7 +31,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 || window.GM.getValue)(key); const val = await GM.getValue(key);
return val; return val;
} }
return window.localStorage.getItem(key); return window.localStorage.getItem(key);
@@ -41,8 +41,8 @@ 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) {
const oldValue = await (window.GM_getValue || window.GM.getValue)(key); const oldValue = await GM.getValue(key);
await (window.GM_deleteValue || window.GM.deleteValue)(key); await GM.deleteValue(key);
window.dispatchEvent( window.dispatchEvent(
new StorageEvent("storage", { new StorageEvent("storage", {
key, key,

View File

@@ -16,12 +16,8 @@ import { Translator } from "./libs/translator";
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE) || document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE) ||
document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE2) document.location.href.includes(process.env.REACT_APP_OPTIONSPAGE2)
) { ) {
window.unsafeWindow.GM = window.GM; unsafeWindow.GM = GM;
window.unsafeWindow.GM_xmlhttpRequest = window.GM_xmlhttpRequest; unsafeWindow.APP_NAME = process.env.REACT_APP_NAME;
window.unsafeWindow.GM_setValue = window.GM_setValue;
window.unsafeWindow.GM_getValue = window.GM_getValue;
window.unsafeWindow.GM_deleteValue = window.GM_deleteValue;
window.unsafeWindow.APP_NAME = process.env.REACT_APP_NAME;
return; return;
} }