fix: 修复缺少站点访问权限时跨域请求异常的问题#1477
Open
cyfung1031 wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
该 PR 旨在修复当浏览器未授予扩展对目标站点的“站点访问权限”时,脚本发起 GM.xmlHttpRequest / GM_xmlhttpRequest 跨域请求可能出现非预期异常的问题。整体思路是在原有 @connect / CORS 确认链路之外,增加对扩展“站点访问权限(Site access)”的检测与引导授权,从而避免在权限被浏览器侧拦截时产生不可控错误。
Changes:
- 在 Service Worker 侧为 XHR 跨域请求增加
chrome.permissions.contains检测,并在缺少站点访问权限时返回新的确认类型extension-site-access - 在确认页面中增加
chrome.permissions.request({ origins })的用户手势授权流程,并为extension-site-access场景简化按钮 UI - 为多语言资源补充“需要站点访问权限”的标题/描述/内容文案
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/pages/confirm/App.tsx | 确认页新增站点访问权限请求逻辑,并对 extension-site-access 场景简化按钮 |
| src/app/service/service_worker/permission_verify.ts | 扩展 ConfirmParam,增加 extensionSiteAccessOrigins 字段用于确认页触发权限申请 |
| src/app/service/service_worker/gm_api/gm_api.ts | XHR 权限确认前新增站点访问权限检测,必要时返回 extension-site-access 确认 |
| src/locales/zh-TW/translation.json | 新增站点访问权限相关文案(繁中) |
| src/locales/zh-CN/translation.json | 新增站点访问权限相关文案(简中) |
| src/locales/vi-VN/translation.json | 新增站点访问权限相关文案(越南语) |
| src/locales/ru-RU/translation.json | 新增站点访问权限相关文案(俄语) |
| src/locales/ja-JP/translation.json | 新增站点访问权限相关文案(日语) |
| src/locales/en-US/translation.json | 新增站点访问权限相关文案(英语) |
| src/locales/de-DE/translation.json | 新增站点访问权限相关文案(德语) |
Comment on lines
+48
to
+62
| if (allow && confirm?.extensionSiteAccessOrigins?.length) { | ||
| const granted = await chrome.permissions.request({ | ||
| origins: confirm.extensionSiteAccessOrigins, | ||
| }); | ||
| if (!granted) { | ||
| await permissionClient | ||
| .confirm(uuid, { | ||
| allow: false, | ||
| type, | ||
| }) | ||
| .catch(() => {}); | ||
| window.close(); | ||
| return; | ||
| } | ||
| } |
Comment on lines
+810
to
+817
| const originPattern = `${url.origin}/*`; | ||
| let hasOriginPermission = false; | ||
| try { | ||
| hasOriginPermission = await chrome.permissions.contains({ origins: [originPattern] }); | ||
| } catch (e) { | ||
| console.warn(e); | ||
| } | ||
| const extensionSiteAccessOrigins = hasOriginPermission ? undefined : [originPattern]; |
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist / 检查清单
Description / 描述
Close #1476
修复在浏览器未授予 ScriptCat 对目标站点的访问权限时,脚本发起跨域请求可能出现非预期错误的问题。
本次改动会在处理 GM.xmlHttpRequest / 跨域请求权限确认时,先检查扩展是否已拥有目标 origin 的站点访问权限。如果尚未授权,则在确认页面引导用户通过浏览器权限弹窗授予对应站点访问权限,授权成功后再继续执行请求;如果用户拒绝授权,则本次请求会被拒绝。
主要改动
chrome.permissions.contains的检查。extension-site-access确认类型。chrome.permissions.request,通过用户手势请求站点访问权限。@connect权限判断逻辑,并在已允许 CORS 但缺少站点访问权限时继续请求浏览器站点权限。测试
已验证以下场景:
Screenshots / 截图
DENY
ALLOW
m501a.user.js
m501b.user.js
server.zip