Skip to content

Commit 6cd49ea

Browse files
authored
Merge pull request #408 from WhiteSevs/fix-gm-api
✨ feat: 修复GM.info、GM.cookie & GM_cookie新增配置partitionKey.topLevelSite
2 parents 01f1036 + 131b31f commit 6cd49ea

4 files changed

Lines changed: 77 additions & 17 deletions

File tree

src/app/service/content/gm_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ export default class GMApi {
130130
return {
131131
downloadMode: "browser",
132132
// isIncognito
133+
// relaxedCsp
134+
// sandboxMode
133135
scriptWillUpdate: true,
134136
scriptHandler: "ScriptCat",
135137
scriptUpdateURL: script.downloadUrl,

src/app/service/content/utils.ts

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,76 @@ export function createContext(scriptRes: ScriptRunResouce, GMInfo: any, envPrefi
6868
valueUpdate: GMApi.prototype.valueUpdate,
6969
emitEvent: GMApi.prototype.emitEvent,
7070
EE: new EventEmitter(),
71-
GM: { Info: GMInfo },
71+
GM: { info: GMInfo },
7272
GM_info: GMInfo,
7373
window: {},
7474
};
7575
if (scriptRes.metadata.grant) {
76+
const GM_cookie = function (action: string) {
77+
let default_details: GMTypes.CookieDetails = {
78+
url: window.location.href,
79+
};
80+
return (
81+
details: GMTypes.CookieDetails,
82+
done: (cookie: GMTypes.Cookie[] | any, error: any | undefined) => void
83+
) => {
84+
let queryDetails = { ...default_details, ...details };
85+
return context["GM_cookie"](action, queryDetails, done);
86+
};
87+
};
7688
scriptRes.metadata.grant.forEach((val) => {
7789
const api = GMContext.apis.get(val);
7890
if (!api) {
7991
return;
8092
}
8193
if (/^(GM|window)\./.test(val)) {
8294
const [n, t] = val.split(".");
83-
(<{ [key: string]: any }>context[n])[t] = api.api.bind(context);
95+
if (t === "cookie") {
96+
context[n][t] = {
97+
list(details: GMTypes.CookieDetails = {}) {
98+
return new Promise((resolve, reject) => {
99+
let fn = GM_cookie("list");
100+
fn(details, function (cookie, error) {
101+
if (error) {
102+
reject(error);
103+
} else {
104+
resolve(cookie);
105+
}
106+
});
107+
});
108+
},
109+
delete(details: GMTypes.CookieDetails) {
110+
return new Promise((resolve, reject) => {
111+
let fn = GM_cookie("delete");
112+
fn(details, function (cookie, error) {
113+
if (error) {
114+
reject(error);
115+
} else {
116+
resolve(cookie);
117+
}
118+
});
119+
});
120+
},
121+
set(details: GMTypes.CookieDetails) {
122+
return new Promise((resolve, reject) => {
123+
let fn = GM_cookie("set");
124+
fn(details, function (cookie, error) {
125+
if (error) {
126+
reject(error);
127+
} else {
128+
resolve(cookie);
129+
}
130+
});
131+
});
132+
},
133+
};
134+
} else {
135+
(<{ [key: string]: any }>context[n])[t] = api.api.bind(context);
136+
}
84137
} else if (val === "GM_cookie") {
85138
// 特殊处理GM_cookie.list之类
86139
context[val] = api.api.bind(context);
87140

88-
const GM_cookie = function (action: string) {
89-
return (
90-
details: GMTypes.CookieDetails,
91-
done: (cookie: GMTypes.Cookie[] | any, error: any | undefined) => void
92-
) => {
93-
return context[val](action, details, done);
94-
};
95-
};
96141
context[val].list = GM_cookie("list");
97142
context[val].delete = GM_cookie("delete");
98143
context[val].set = GM_cookie("set");

src/app/service/service_worker/gm_api.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,9 @@ export default class GMApi {
137137
let flag = false;
138138
if (request.script.metadata.connect) {
139139
const { connect } = request.script.metadata;
140-
for (let i = 0; i < connect.length; i += 1) {
141-
if (url.hostname.endsWith(connect[i])) {
142-
flag = true;
143-
break;
144-
}
145-
}
140+
flag =
141+
connect.indexOf("*") !== -1 ||
142+
connect.findIndex((connectHostName) => url.hostname.endsWith(connectHostName)) !== -1;
146143
}
147144
if (!flag) {
148145
return Promise.reject(new Error("hostname must be in the definition of connect"));
@@ -177,6 +174,13 @@ export default class GMApi {
177174
if (!detail.url && !detail.domain) {
178175
throw new Error("there must be one of url or domain");
179176
}
177+
if (typeof detail.partitionKey !== "object" || detail.partitionKey == null) {
178+
detail.partitionKey = {};
179+
}
180+
if (typeof detail.partitionKey.topLevelSite !== "string") {
181+
// string | undefined
182+
detail.partitionKey.topLevelSite = undefined;
183+
}
180184
// 处理tab的storeid
181185
let tabId = sender.getExtMessageSender().tabId;
182186
let storeId: string | undefined;
@@ -189,15 +193,17 @@ export default class GMApi {
189193
}
190194
switch (param[0]) {
191195
case "list": {
192-
return chrome.cookies.getAll({
196+
let cookies = await chrome.cookies.getAll({
193197
domain: detail.domain,
194198
name: detail.name,
195199
path: detail.path,
196200
secure: detail.secure,
197201
session: detail.session,
198202
url: detail.url,
199203
storeId: storeId,
204+
partitionKey: detail.partitionKey,
200205
});
206+
return cookies;
201207
}
202208
case "delete": {
203209
if (!detail.url || !detail.name) {
@@ -207,6 +213,7 @@ export default class GMApi {
207213
name: detail.name,
208214
url: detail.url,
209215
storeId: storeId,
216+
partitionKey: detail.partitionKey,
210217
});
211218
break;
212219
}
@@ -224,6 +231,7 @@ export default class GMApi {
224231
httpOnly: detail.httpOnly,
225232
secure: detail.secure,
226233
storeId: storeId,
234+
partitionKey: detail.partitionKey,
227235
});
228236
break;
229237
}

src/types/scriptcat.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ declare namespace GMTypes {
294294
[key: string]: string | boolean | number | undefined;
295295
};
296296

297+
interface CookieDetailsPartitionKeyType {
298+
topLevelSite?: string;
299+
}
300+
297301
interface CookieDetails {
298302
url?: string;
299303
name?: string;
@@ -304,6 +308,7 @@ declare namespace GMTypes {
304308
session?: boolean;
305309
httpOnly?: boolean;
306310
expirationDate?: number;
311+
partitionKey?: CookieDetailsPartitionKeyType;
307312
}
308313

309314
interface Cookie {

0 commit comments

Comments
 (0)