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
23 changes: 23 additions & 0 deletions example/gm_download.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* - 自定义 header
* - 进度回调 / 完成回调
*/

// 1. 下载网络资源

GM_download({
url: "https://scriptcat.org/api/v2/open/crx-download/ndcooeababalnlpkfedmmbbbgkljhpjf",
name: "scriptcat.crx",
Expand All @@ -35,3 +38,23 @@ GM_download({
console.log("load", data);
},
});

// 2. 下载 Blob 资源
// 参考: https://github.com/Tampermonkey/tampermonkey/issues/2591

const pngData = new Uint8Array([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00,
0x0a, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0x00,
0x00, 0x03, 0x01, 0x01, 0x00, 0xae, 0xb4, 0xfa, 0x77, 0x00, 0x00, 0x00,
0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
]);

const testImageUrl = URL.createObjectURL(new Blob([pngData], { type: 'image/png' }));

GM_download({
url: testImageUrl,
name: 'test/test.png', // 储存在 test 资料夹内
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

示例里 name: 'test/test.png' 并注释“储存在 test 资料夹内”,但当前实现会在后台通过 cleanFileName 把 / 替换成 -(因此实际文件名会变成 test-test.png,不会创建子目录)。建议要么改成不含路径分隔符的文件名,要么更新注释/示例以符合实际行为。

Suggested change
name: 'test/test.png', // 储存在 test 资料夹内
name: 'test-test.png', // 使用固定文件名(不会创建子目录)

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI 有时候聪明过了

conflictAction: 'overwrite', // 每次都使用固定的档案名
Comment on lines +55 to +59
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里用 URL.createObjectURL 生成了 blob URL,但示例里没有在下载完成/失败后调用 URL.revokeObjectURL 释放,可能导致页面侧内存与 blob 注册表条目泄漏。建议在 onload/onerror/ontimeout 回调里(或 finally)revoke,并注意不要过早 revoke 以免下载未开始。

Suggested change
GM_download({
url: testImageUrl,
name: 'test/test.png', // 储存在 test 资料夹内
conflictAction: 'overwrite', // 每次都使用固定的档案名
function revokeTestImageUrl() {
URL.revokeObjectURL(testImageUrl);
}
GM_download({
url: testImageUrl,
name: 'test/test.png', // 储存在 test 资料夹内
conflictAction: 'overwrite', // 每次都使用固定的档案名
onload() {
revokeTestImageUrl();
},
onerror() {
revokeTestImageUrl();
},
ontimeout() {
revokeTestImageUrl();
},

Copilot uses AI. Check for mistakes.
});
2 changes: 2 additions & 0 deletions src/app/service/content/gm_api/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ export default class GMApi extends GM_Base {
name: details.name,
headers: details.headers,
saveAs: details.saveAs,
conflictAction: details.conflictAction,
timeout: details.timeout,
cookie: details.cookie,
anonymous: details.anonymous,
Expand Down Expand Up @@ -938,6 +939,7 @@ export default class GMApi extends GM_Base {
name: details.name,
headers: details.headers,
saveAs: details.saveAs,
conflictAction: details.conflictAction,
timeout: details.timeout,
cookie: details.cookie,
anonymous: details.anonymous,
Expand Down
Loading