-
Notifications
You must be signed in to change notification settings - Fork 298
修复 无法通过GitHub链接安装脚本 #877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
修复 无法通过GitHub链接安装脚本 #877
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8bce443
修复 无法通过GitHub链接安装脚本
cyfung1031 b0e4f9f
Update script.ts
cyfung1031 65c3e8c
修复 无法通过GitHub链接安装脚本
cyfung1031 1a7bd4c
updateDynamicRules -> updateSessionRules
cyfung1031 954159a
Merge remote-tracking branch 'upstream/main' into pr-github-link
cyfung1031 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -162,39 +162,123 @@ export class ScriptService { | |
| } | ||
| ); | ||
| // 兼容 chrome 内核 < 128 处理 | ||
| const condition: chrome.declarativeNetRequest.RuleCondition = { | ||
| regexFilter: "^([^#]+?)\\.user(\\.bg|\\.sub)?\\.js((\\?).*|$)", | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], | ||
| }; | ||
| const browserType = getBrowserType(); | ||
| if (browserType.chrome && browserType.chromeVersion >= 128) { | ||
| condition.excludedResponseHeaders = [ | ||
| { | ||
| header: "Content-Type", | ||
| values: ["text/html"], | ||
| const addResponseHeaders = browserType.chrome && browserType.chromeVersion >= 128; | ||
| // Chrome 84+ | ||
| const conditions: chrome.declarativeNetRequest.RuleCondition[] = [ | ||
| { | ||
| regexFilter: "^([^?#]+?\\.user(\\.bg|\\.sub)?\\.js)", // Chrome 84+ | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], // Chrome 84+ | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+ | ||
| isUrlFilterCaseSensitive: false, // Chrome 84+ | ||
| excludedRequestDomains: ["github.com", "gitlab.com", "gitea.com", "bitbucket.org"], // Chrome 101+ | ||
| }, | ||
| { | ||
| regexFilter: "^(.+?\\.user(\\.bg|\\.sub)?\\.js&response-content-type=application%2Foctet-stream)", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这种应该是可以不匹配的(不过无所谓,可以少点规则) |
||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+ | ||
| isUrlFilterCaseSensitive: false, | ||
| requestDomains: ["githubusercontent.com"], // Chrome 101+ | ||
| }, | ||
| { | ||
| regexFilter: | ||
| "^(https?:\\/\\/github.com\\/[^\\s/?#]+\\/[^\\s/?#]+\\/releases/[^\\s/?#]+/download/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)", | ||
| // https://github.com/<user>/<repo>/releases/latest/download/file.user.js | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+ | ||
| isUrlFilterCaseSensitive: false, | ||
| requestDomains: ["github.com"], // Chrome 101+ | ||
| }, | ||
| { | ||
| regexFilter: | ||
| "^(https?:\\/\\/gitlab\\.com\\/[^\\s/?#]+\\/[^\\s/?#]+\\/-\\/raw\\/[a-z0-9_/.-]+\\/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)", | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+ | ||
| isUrlFilterCaseSensitive: false, | ||
| requestDomains: ["gitlab.com"], // Chrome 101+ | ||
| }, | ||
| { | ||
| regexFilter: "^(https?:\\/\\/github\\.com\\/[^\\/]+\\/[^\\/]+\\/releases\\/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)", | ||
| // https://github.com/<user>/<repo>/releases/latest/download/file.user.js | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+ | ||
| isUrlFilterCaseSensitive: false, | ||
| requestDomains: ["github.com"], // Chrome 101+ | ||
| }, | ||
| { | ||
| regexFilter: "^(https?://github.com/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z]+/[^?#]+?.user(\\.bg|\\.sub)?.js)", | ||
| // https://github.com/<user>/<repo>/raw/refs/heads/main/.../file.user.js | ||
| // https://github.com/<user>/<repo>/raw/<branch>/.../file.user.js | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+ | ||
| isUrlFilterCaseSensitive: false, | ||
| requestDomains: ["github.com"], // Chrome 101+ | ||
| }, | ||
| { | ||
| regexFilter: | ||
| "^(https?://gitlab\\.com/[^\\s/?#]+/[^\\s/?#]+/-/raw/[a-z0-9_/.-]+/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)", | ||
| // https://gitlab.com/<user>/<repo>/-/raw/<branch>/.../file.user.js | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], | ||
| isUrlFilterCaseSensitive: false, | ||
| requestDomains: ["gitlab.com"], // Chrome 101+ | ||
| }, | ||
| { | ||
| regexFilter: | ||
| "^(https?://gitea\\.com/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z0-9_/.-]+/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)", | ||
| // https://gitea.com/<user>/<repo>/raw/<branch>/.../file.user.js | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], | ||
| isUrlFilterCaseSensitive: false, | ||
| requestDomains: ["gitea.com"], // Chrome 101+ | ||
| }, | ||
| { | ||
| regexFilter: | ||
| "^(https?://bitbucket\\.org/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z0-9_/.-]+/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)", | ||
| // https://bitbucket.org/<user>/<repo>/raw/<branch>/.../file.user.js | ||
| resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], | ||
| requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], | ||
| isUrlFilterCaseSensitive: false, | ||
| requestDomains: ["bitbucket.org"], // Chrome 101+ | ||
| }, | ||
| ]; | ||
| const rules = conditions.map((condition, idx) => { | ||
| Object.assign(condition, { | ||
| excludedTabIds: [chrome.tabs.TAB_ID_NONE], | ||
| }); | ||
| if (addResponseHeaders) { | ||
| Object.assign(condition, { | ||
| responseHeaders: [ | ||
| { | ||
| header: "Content-Type", | ||
| values: [ | ||
| "text/javascript*", | ||
| "application/javascript*", | ||
| "text/html*", | ||
| "text/plain*", | ||
| "application/octet-stream*", | ||
| "application/force-download*", | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
| return { | ||
| id: 1000 + idx, | ||
| priority: 1, | ||
| action: { | ||
| type: "redirect" as chrome.declarativeNetRequest.RuleActionType, | ||
| redirect: { | ||
| regexSubstitution: `${DocumentationSite}${localePath}/docs/script_installation/#url=\\1`, | ||
| }, | ||
| }, | ||
| ]; | ||
| } else { | ||
| condition.excludedRequestDomains = ["github.com"]; | ||
| } | ||
| condition: condition, | ||
| } as chrome.declarativeNetRequest.Rule; | ||
| }); | ||
| // 重定向到脚本安装页 | ||
| chrome.declarativeNetRequest.updateDynamicRules( | ||
| { | ||
| removeRuleIds: [1, 2], | ||
| addRules: [ | ||
| { | ||
| id: 1, | ||
| priority: 1, | ||
| action: { | ||
| type: "redirect" as chrome.declarativeNetRequest.RuleActionType, | ||
| redirect: { | ||
| regexSubstitution: `${DocumentationSite}${localePath}/docs/script_installation/#url=\\0`, | ||
| }, | ||
| }, | ||
| condition: condition, | ||
| }, | ||
| ], | ||
| removeRuleIds: [1], | ||
| }, | ||
| () => { | ||
| if (chrome.runtime.lastError) { | ||
|
|
@@ -205,6 +289,20 @@ export class ScriptService { | |
| } | ||
| } | ||
| ); | ||
| chrome.declarativeNetRequest.updateSessionRules( | ||
| { | ||
| removeRuleIds: [...rules.map((rule) => rule.id)], | ||
| addRules: rules, | ||
| }, | ||
| () => { | ||
| if (chrome.runtime.lastError) { | ||
| console.error( | ||
| "chrome.runtime.lastError in chrome.declarativeNetRequest.updateSessionRules:", | ||
| chrome.runtime.lastError | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public async openInstallPageByUrl(url: string, source: InstallSource): Promise<{ success: boolean; msg: string }> { | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
responseHeaders与excludedResponseHeaders 都需要 >= 128 的版本,是不打算兼容旧版本的浏览器了么?(虽然我也想,恶心的兼容性) https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest?hl=zh-cn#:~:text=main_frame%20%E8%B5%84%E6%BA%90%E7%B1%BB%E5%9E%8B%E3%80%82-,responseHeaders,-HeaderInfo%5B%5D%C2%A0%E5%8F%AF
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest?hl=en
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不使用 excludedResponseHeaders
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你似乎看错了,这个是 RuleAction 的:https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest?hl=en#type-RuleAction
RuleCondition的是
https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest?hl=en#type-RuleCondition