| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1209,6 +1209,53 @@ return { value1, value2, value3, values1,values2, allValues1, allValues2, value4 | |||
| 1209 | 1209 | }); | |
| 1210 | 1210 | }); | |
| 1211 | 1211 | ||
| 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 | + | ||
| 1212 | 1259 | describe("@grant CAT.agent.conversation", () => { | |
| 1213 | 1260 | it("CAT.agent.conversation 应该在沙盒中可访问", async () => { | |
| 1214 | 1261 | const script = Object.assign({}, scriptRes) as ScriptLoadInfo; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -993,6 +993,15 @@ export default class GMApi extends GM_Base { | |||
| 993 | 993 | }; | |
| 994 | 994 | const handle = async () => { | |
| 995 | 995 | 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 | + } | ||
| 996 | 1005 | const downloadMode = details.downloadMode || "native"; // native = sc_default; browser = chrome api | |
| 997 | 1006 | details.url = url; | |
| 998 | 1007 | if (downloadMode === "browser" || url.startsWith("blob:")) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments