Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/service/content/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export default class GMApi {
return {
downloadMode: "browser",
// isIncognito
// relaxedCsp
// sandboxMode
scriptWillUpdate: true,
scriptHandler: "ScriptCat",
scriptUpdateURL: script.downloadUrl,
Expand Down
65 changes: 55 additions & 10 deletions src/app/service/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,76 @@ 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) {
return;
}
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");
Expand Down
22 changes: 15 additions & 7 deletions src/app/service/service_worker/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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;
Expand All @@ -189,15 +193,17 @@ 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,
secure: detail.secure,
session: detail.session,
url: detail.url,
storeId: storeId,
partitionKey: detail.partitionKey,
});
return cookies;
}
case "delete": {
if (!detail.url || !detail.name) {
Expand All @@ -207,6 +213,7 @@ export default class GMApi {
name: detail.name,
url: detail.url,
storeId: storeId,
partitionKey: detail.partitionKey,
});
break;
}
Expand All @@ -224,6 +231,7 @@ export default class GMApi {
httpOnly: detail.httpOnly,
secure: detail.secure,
storeId: storeId,
partitionKey: detail.partitionKey,
});
break;
}
Expand Down
5 changes: 5 additions & 0 deletions src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ declare namespace GMTypes {
[key: string]: string | boolean | number | undefined;
};

interface CookieDetailsPartitionKeyType {
topLevelSite?: string;
}

interface CookieDetails {
url?: string;
name?: string;
Expand All @@ -304,6 +308,7 @@ declare namespace GMTypes {
session?: boolean;
httpOnly?: boolean;
expirationDate?: number;
partitionKey?: CookieDetailsPartitionKeyType;
}

interface Cookie {
Expand Down