diff --git a/src/app/service/content/gm_api.ts b/src/app/service/content/gm_api.ts index 87c631479..ce02139d0 100644 --- a/src/app/service/content/gm_api.ts +++ b/src/app/service/content/gm_api.ts @@ -130,6 +130,8 @@ export default class GMApi { return { downloadMode: "browser", // isIncognito + // relaxedCsp + // sandboxMode scriptWillUpdate: true, scriptHandler: "ScriptCat", scriptUpdateURL: script.downloadUrl, diff --git a/src/app/service/content/utils.ts b/src/app/service/content/utils.ts index 030bed26e..e2fc1d499 100644 --- a/src/app/service/content/utils.ts +++ b/src/app/service/content/utils.ts @@ -68,11 +68,23 @@ export function createContext(scriptRes: ScriptRunResouce, GMInfo: any, envPrefi valueUpdate: GMApi.prototype.valueUpdate, emitEvent: GMApi.prototype.emitEvent, EE: new EventEmitter(), - GM: { Info: GMInfo }, + GM: { info: GMInfo }, GM_info: GMInfo, window: {}, }; if (scriptRes.metadata.grant) { + const GM_cookie = function (action: string) { + let default_details: GMTypes.CookieDetails = { + url: window.location.href, + }; + return ( + details: GMTypes.CookieDetails, + done: (cookie: GMTypes.Cookie[] | any, error: any | undefined) => void + ) => { + let queryDetails = { ...default_details, ...details }; + return context["GM_cookie"](action, queryDetails, done); + }; + }; scriptRes.metadata.grant.forEach((val) => { const api = GMContext.apis.get(val); if (!api) { @@ -80,19 +92,52 @@ export function createContext(scriptRes: ScriptRunResouce, GMInfo: any, envPrefi } if (/^(GM|window)\./.test(val)) { const [n, t] = val.split("."); - (<{ [key: string]: any }>context[n])[t] = api.api.bind(context); + if (t === "cookie") { + context[n][t] = { + list(details: GMTypes.CookieDetails = {}) { + return new Promise((resolve, reject) => { + let fn = GM_cookie("list"); + fn(details, function (cookie, error) { + if (error) { + reject(error); + } else { + resolve(cookie); + } + }); + }); + }, + delete(details: GMTypes.CookieDetails) { + return new Promise((resolve, reject) => { + let fn = GM_cookie("delete"); + fn(details, function (cookie, error) { + if (error) { + reject(error); + } else { + resolve(cookie); + } + }); + }); + }, + set(details: GMTypes.CookieDetails) { + return new Promise((resolve, reject) => { + let fn = GM_cookie("set"); + fn(details, function (cookie, error) { + if (error) { + reject(error); + } else { + resolve(cookie); + } + }); + }); + }, + }; + } else { + (<{ [key: string]: any }>context[n])[t] = api.api.bind(context); + } } else if (val === "GM_cookie") { // 特殊处理GM_cookie.list之类 context[val] = api.api.bind(context); - const GM_cookie = function (action: string) { - return ( - details: GMTypes.CookieDetails, - done: (cookie: GMTypes.Cookie[] | any, error: any | undefined) => void - ) => { - return context[val](action, details, done); - }; - }; context[val].list = GM_cookie("list"); context[val].delete = GM_cookie("delete"); context[val].set = GM_cookie("set"); diff --git a/src/app/service/service_worker/gm_api.ts b/src/app/service/service_worker/gm_api.ts index f8025aae2..845c7c687 100644 --- a/src/app/service/service_worker/gm_api.ts +++ b/src/app/service/service_worker/gm_api.ts @@ -137,12 +137,9 @@ export default class GMApi { let flag = false; if (request.script.metadata.connect) { const { connect } = request.script.metadata; - for (let i = 0; i < connect.length; i += 1) { - if (url.hostname.endsWith(connect[i])) { - flag = true; - break; - } - } + flag = + connect.indexOf("*") !== -1 || + connect.findIndex((connectHostName) => url.hostname.endsWith(connectHostName)) !== -1; } if (!flag) { return Promise.reject(new Error("hostname must be in the definition of connect")); @@ -177,6 +174,13 @@ export default class GMApi { if (!detail.url && !detail.domain) { throw new Error("there must be one of url or domain"); } + if (typeof detail.partitionKey !== "object" || detail.partitionKey == null) { + detail.partitionKey = {}; + } + if (typeof detail.partitionKey.topLevelSite !== "string") { + // string | undefined + detail.partitionKey.topLevelSite = undefined; + } // 处理tab的storeid let tabId = sender.getExtMessageSender().tabId; let storeId: string | undefined; @@ -189,7 +193,7 @@ export default class GMApi { } switch (param[0]) { case "list": { - return chrome.cookies.getAll({ + let cookies = await chrome.cookies.getAll({ domain: detail.domain, name: detail.name, path: detail.path, @@ -197,7 +201,9 @@ export default class GMApi { session: detail.session, url: detail.url, storeId: storeId, + partitionKey: detail.partitionKey, }); + return cookies; } case "delete": { if (!detail.url || !detail.name) { @@ -207,6 +213,7 @@ export default class GMApi { name: detail.name, url: detail.url, storeId: storeId, + partitionKey: detail.partitionKey, }); break; } @@ -224,6 +231,7 @@ export default class GMApi { httpOnly: detail.httpOnly, secure: detail.secure, storeId: storeId, + partitionKey: detail.partitionKey, }); break; } diff --git a/src/types/scriptcat.d.ts b/src/types/scriptcat.d.ts index b5158ef7d..60f1ad370 100644 --- a/src/types/scriptcat.d.ts +++ b/src/types/scriptcat.d.ts @@ -294,6 +294,10 @@ declare namespace GMTypes { [key: string]: string | boolean | number | undefined; }; + interface CookieDetailsPartitionKeyType { + topLevelSite?: string; + } + interface CookieDetails { url?: string; name?: string; @@ -304,6 +308,7 @@ declare namespace GMTypes { session?: boolean; httpOnly?: boolean; expirationDate?: number; + partitionKey?: CookieDetailsPartitionKeyType; } interface Cookie {