FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

🐛 修复 GM_download 传入空 url 时未触发 onerror 的兼容性问题 (#1619) · scriptscat/scriptcat@125d58b · GitHub

Commit 125d58b

Browse files
authored
🐛 修复 GM_download 传入空 url 时未触发 onerror 的兼容性问题 (#1619)
new URL("", location.href) 不会抛错而是解析为当前页面地址,导致空 url 被当作有效地址发起下载,与 TM 触发 onerror 的行为不一致。
1 parent a4bc9b6 commit 125d58b

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎src/app/service/content/gm_api/gm_api.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,53 @@ return { value1, value2, value3, values1,values2, allValues1, allValues2, value4
12091209
});
12101210
});
12111211

1212+
describe("@grant GM_download", () => {
1213+
it("空 url 应触发 onerror 而不是发起下载(GM_download)", async () => {
1214+
const script = Object.assign({}, scriptRes) as ScriptLoadInfo;
1215+
script.metadata.grant = ["GM_download"];
1216+
const exec = new ExecScript(script, {
1217+
envPrefix: "scripting",
1218+
message: undefined as any,
1219+
contentMsg: undefined as any,
1220+
code: nilFn,
1221+
envInfo,
1222+
});
1223+
script.code = `return new Promise((resolve) => {
1224+
let onloadCalled = false;
1225+
GM_download({
1226+
url: "",
1227+
name: "empty-url-test.bin",
1228+
onload: () => { onloadCalled = true; },
1229+
onerror: (e) => resolve({ onloadCalled, error: e && e.error }),
1230+
});
1231+
setTimeout(() => resolve({ onloadCalled, error: "TIMEOUT" }), 100);
1232+
})`;
1233+
exec.scriptFunc = compileScript(compileScriptCode(script));
1234+
const ret = await exec.exec();
1235+
expect(ret.onloadCalled).toEqual(false);
1236+
expect(ret.error).toEqual("unknown");
1237+
});
1238+
1239+
it("空 url 应 reject(GM.download)", async () => {
1240+
const script = Object.assign({}, scriptRes) as ScriptLoadInfo;
1241+
script.metadata.grant = ["GM.download"];
1242+
const exec = new ExecScript(script, {
1243+
envPrefix: "scripting",
1244+
message: undefined as any,
1245+
contentMsg: undefined as any,
1246+
code: nilFn,
1247+
envInfo,
1248+
});
1249+
script.code = `return GM.download({ url: "", name: "empty-url-test.bin" }).then(
1250+
() => ({ resolved: true }),
1251+
() => ({ resolved: false })
1252+
)`;
1253+
exec.scriptFunc = compileScript(compileScriptCode(script));
1254+
const ret = await exec.exec();
1255+
expect(ret.resolved).toEqual(false);
1256+
});
1257+
});
1258+
12121259
describe("@grant CAT.agent.conversation", () => {
12131260
it("CAT.agent.conversation 应该在沙盒中可访问", async () => {
12141261
const script = Object.assign({}, scriptRes) as ScriptLoadInfo;

‎src/app/service/content/gm_api/gm_api.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,15 @@ export default class GMApi extends GM_Base {
993993
};
994994
const handle = async () => {
995995
const url = await urlPromiseLike;
996+
if (!url) {
997+
// TM 对空 url 会同步报错/触发 onerror,而非发起请求;
998+
// new URL("", base) 不会抛错而是解析为当前页面地址,因此需在此显式拦截,避免误下载当前页面。
999+
if (!aborted) {
1000+
details.onerror?.(makeCallbackParam({ error: "unknown" }) as GMTypes.DownloadError);
1001+
retPromiseReject?.(new Error("GM_download: url is empty"));
1002+
}
1003+
return;
1004+
}
9961005
const downloadMode = details.downloadMode || "native"; // native = sc_default; browser = chrome api
9971006
details.url = url;
9981007
if (downloadMode === "browser" || url.startsWith("blob:")) {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL