Skip to content

Commit b9268e7

Browse files
authored
🐛 修复 无法通过GitHub链接安装脚本 (#877)
* 修复 无法通过GitHub链接安装脚本 * Update script.ts * 修复 无法通过GitHub链接安装脚本 * updateDynamicRules -> updateSessionRules
1 parent 6bcb93c commit b9268e7

File tree

1 file changed

+126
-28
lines changed

1 file changed

+126
-28
lines changed

src/app/service/service_worker/script.ts

Lines changed: 126 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -162,39 +162,123 @@ export class ScriptService {
162162
}
163163
);
164164
// 兼容 chrome 内核 < 128 处理
165-
const condition: chrome.declarativeNetRequest.RuleCondition = {
166-
regexFilter: "^([^#]+?)\\.user(\\.bg|\\.sub)?\\.js((\\?).*|$)",
167-
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
168-
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod],
169-
};
170165
const browserType = getBrowserType();
171-
if (browserType.chrome && browserType.chromeVersion >= 128) {
172-
condition.excludedResponseHeaders = [
173-
{
174-
header: "Content-Type",
175-
values: ["text/html"],
166+
const addResponseHeaders = browserType.chrome && browserType.chromeVersion >= 128;
167+
// Chrome 84+
168+
const conditions: chrome.declarativeNetRequest.RuleCondition[] = [
169+
{
170+
regexFilter: "^([^?#]+?\\.user(\\.bg|\\.sub)?\\.js)", // Chrome 84+
171+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME], // Chrome 84+
172+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
173+
isUrlFilterCaseSensitive: false, // Chrome 84+
174+
excludedRequestDomains: ["github.com", "gitlab.com", "gitea.com", "bitbucket.org"], // Chrome 101+
175+
},
176+
{
177+
regexFilter: "^(.+?\\.user(\\.bg|\\.sub)?\\.js&response-content-type=application%2Foctet-stream)",
178+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
179+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
180+
isUrlFilterCaseSensitive: false,
181+
requestDomains: ["githubusercontent.com"], // Chrome 101+
182+
},
183+
{
184+
regexFilter:
185+
"^(https?:\\/\\/github.com\\/[^\\s/?#]+\\/[^\\s/?#]+\\/releases/[^\\s/?#]+/download/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)",
186+
// https://github.com/<user>/<repo>/releases/latest/download/file.user.js
187+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
188+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
189+
isUrlFilterCaseSensitive: false,
190+
requestDomains: ["github.com"], // Chrome 101+
191+
},
192+
{
193+
regexFilter:
194+
"^(https?:\\/\\/gitlab\\.com\\/[^\\s/?#]+\\/[^\\s/?#]+\\/-\\/raw\\/[a-z0-9_/.-]+\\/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)",
195+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
196+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
197+
isUrlFilterCaseSensitive: false,
198+
requestDomains: ["gitlab.com"], // Chrome 101+
199+
},
200+
{
201+
regexFilter: "^(https?:\\/\\/github\\.com\\/[^\\/]+\\/[^\\/]+\\/releases\\/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)",
202+
// https://github.com/<user>/<repo>/releases/latest/download/file.user.js
203+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
204+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
205+
isUrlFilterCaseSensitive: false,
206+
requestDomains: ["github.com"], // Chrome 101+
207+
},
208+
{
209+
regexFilter: "^(https?://github.com/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z]+/[^?#]+?.user(\\.bg|\\.sub)?.js)",
210+
// https://github.com/<user>/<repo>/raw/refs/heads/main/.../file.user.js
211+
// https://github.com/<user>/<repo>/raw/<branch>/.../file.user.js
212+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
213+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
214+
isUrlFilterCaseSensitive: false,
215+
requestDomains: ["github.com"], // Chrome 101+
216+
},
217+
{
218+
regexFilter:
219+
"^(https?://gitlab\\.com/[^\\s/?#]+/[^\\s/?#]+/-/raw/[a-z0-9_/.-]+/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)",
220+
// https://gitlab.com/<user>/<repo>/-/raw/<branch>/.../file.user.js
221+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
222+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod],
223+
isUrlFilterCaseSensitive: false,
224+
requestDomains: ["gitlab.com"], // Chrome 101+
225+
},
226+
{
227+
regexFilter:
228+
"^(https?://gitea\\.com/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z0-9_/.-]+/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)",
229+
// https://gitea.com/<user>/<repo>/raw/<branch>/.../file.user.js
230+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
231+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod],
232+
isUrlFilterCaseSensitive: false,
233+
requestDomains: ["gitea.com"], // Chrome 101+
234+
},
235+
{
236+
regexFilter:
237+
"^(https?://bitbucket\\.org/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z0-9_/.-]+/[^?#]+?\\.user(\\.bg|\\.sub)?\\.js)",
238+
// https://bitbucket.org/<user>/<repo>/raw/<branch>/.../file.user.js
239+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
240+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod],
241+
isUrlFilterCaseSensitive: false,
242+
requestDomains: ["bitbucket.org"], // Chrome 101+
243+
},
244+
];
245+
const rules = conditions.map((condition, idx) => {
246+
Object.assign(condition, {
247+
excludedTabIds: [chrome.tabs.TAB_ID_NONE],
248+
});
249+
if (addResponseHeaders) {
250+
Object.assign(condition, {
251+
responseHeaders: [
252+
{
253+
header: "Content-Type",
254+
values: [
255+
"text/javascript*",
256+
"application/javascript*",
257+
"text/html*",
258+
"text/plain*",
259+
"application/octet-stream*",
260+
"application/force-download*",
261+
],
262+
},
263+
],
264+
});
265+
}
266+
return {
267+
id: 1000 + idx,
268+
priority: 1,
269+
action: {
270+
type: "redirect" as chrome.declarativeNetRequest.RuleActionType,
271+
redirect: {
272+
regexSubstitution: `${DocumentationSite}${localePath}/docs/script_installation/#url=\\1`,
273+
},
176274
},
177-
];
178-
} else {
179-
condition.excludedRequestDomains = ["github.com"];
180-
}
275+
condition: condition,
276+
} as chrome.declarativeNetRequest.Rule;
277+
});
181278
// 重定向到脚本安装页
182279
chrome.declarativeNetRequest.updateDynamicRules(
183280
{
184-
removeRuleIds: [1, 2],
185-
addRules: [
186-
{
187-
id: 1,
188-
priority: 1,
189-
action: {
190-
type: "redirect" as chrome.declarativeNetRequest.RuleActionType,
191-
redirect: {
192-
regexSubstitution: `${DocumentationSite}${localePath}/docs/script_installation/#url=\\0`,
193-
},
194-
},
195-
condition: condition,
196-
},
197-
],
281+
removeRuleIds: [1],
198282
},
199283
() => {
200284
if (chrome.runtime.lastError) {
@@ -205,6 +289,20 @@ export class ScriptService {
205289
}
206290
}
207291
);
292+
chrome.declarativeNetRequest.updateSessionRules(
293+
{
294+
removeRuleIds: [...rules.map((rule) => rule.id)],
295+
addRules: rules,
296+
},
297+
() => {
298+
if (chrome.runtime.lastError) {
299+
console.error(
300+
"chrome.runtime.lastError in chrome.declarativeNetRequest.updateSessionRules:",
301+
chrome.runtime.lastError
302+
);
303+
}
304+
}
305+
);
208306
}
209307

210308
public async openInstallPageByUrl(url: string, source: InstallSource): Promise<{ success: boolean; msg: string }> {

0 commit comments

Comments
 (0)