fix: GM_download 补回 conflictAction#1250
Conversation
|
一直漏的 |
There was a problem hiding this comment.
Pull request overview
该 PR 主要用于修复内容脚本侧 GM_download 参数透传缺失的问题,把 conflictAction 补回消息传递链路,使后台 chrome.downloads.download 能按期望处理重名文件策略;同时更新示例脚本补充 Blob 下载场景。
Changes:
- 在 content 侧
GM_download的两处转发参数中补回conflictAction - 更新
example/gm_download.js,新增 Blob 资源下载示例并展示conflictAction: 'overwrite'
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/app/service/content/gm_api/gm_api.ts | GM_download 转发参数补充 conflictAction,对齐 SW 侧下载选项支持 |
| example/gm_download.js | 增加 Blob 下载示例与 conflictAction 使用示例 |
|
|
||
| GM_download({ | ||
| url: testImageUrl, | ||
| name: 'test/test.png', // 储存在 test 资料夹内 |
There was a problem hiding this comment.
示例里 name: 'test/test.png' 并注释“储存在 test 资料夹内”,但当前实现会在后台通过 cleanFileName 把 / 替换成 -(因此实际文件名会变成 test-test.png,不会创建子目录)。建议要么改成不含路径分隔符的文件名,要么更新注释/示例以符合实际行为。
| name: 'test/test.png', // 储存在 test 资料夹内 | |
| name: 'test-test.png', // 使用固定文件名(不会创建子目录) |
| url: url as string, | ||
| name: details.name, | ||
| headers: details.headers, | ||
| saveAs: details.saveAs, | ||
| conflictAction: details.conflictAction, | ||
| timeout: details.timeout, |
There was a problem hiding this comment.
这次补回了 conflictAction 的透传,但目前看不到对应的自动化测试来防止后续回归(例如校验 content 侧发送的参数、以及 SW 侧最终传给 chrome.downloads.download 的 options 含 conflictAction)。建议补一个用例覆盖 conflictAction=overwrite/uniquify 等至少一个分支。
|
|
||
| GM_download({ | ||
| url: testImageUrl, | ||
| name: 'test/test.png', // 储存在 test 资料夹内 | ||
| conflictAction: 'overwrite', // 每次都使用固定的档案名 |
There was a problem hiding this comment.
这里用 URL.createObjectURL 生成了 blob URL,但示例里没有在下载完成/失败后调用 URL.revokeObjectURL 释放,可能导致页面侧内存与 blob 注册表条目泄漏。建议在 onload/onerror/ontimeout 回调里(或 finally)revoke,并注意不要过早 revoke 以免下载未开始。
| 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(); | |
| }, |
一直漏了还是之前改漏了?不清楚
另外加了新例子
注:新例子使用的是
test/test.png. 目前 1.2 未包含上次 PR #1203 的修改