Skip to content

Commit 32ded8d

Browse files
committed
1 parent d9c2956 commit 32ded8d

File tree

1 file changed

+97
-35
lines changed

1 file changed

+97
-35
lines changed

src/app/service/service_worker/script.ts

Lines changed: 97 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import Logger from "@App/app/logger/logger";
55
import LoggerCore from "@App/app/logger/core";
66
import { cacheInstance } from "@App/app/cache";
77
import { CACHE_KEY_SCRIPT_INFO } from "@App/app/cache_key";
8-
import { checkSilenceUpdate, getStorageName, openInCurrentTab, stringMatching } from "@App/pkg/utils/utils";
8+
import {
9+
checkSilenceUpdate,
10+
getBrowserType,
11+
getStorageName,
12+
openInCurrentTab,
13+
stringMatching,
14+
} from "@App/pkg/utils/utils";
915
import { ltever } from "@App/pkg/utils/semver";
1016
import type {
1117
SCMetadata,
@@ -88,11 +94,12 @@ export class ScriptService {
8894
return undefined;
8995
}
9096
// 判断是否有url参数
91-
if (!reqUrl.hash.includes("url=")) {
97+
const idx = reqUrl.hash.indexOf("url=");
98+
if (idx < 0) {
9299
return undefined;
93100
}
94101
// 获取url参数
95-
targetUrl = reqUrl.hash.split("url=")[1];
102+
targetUrl = reqUrl.hash.substring(idx + 4);
96103
}
97104
// 读取脚本url内容, 进行安装
98105
const logger = this.logger.with({ url: targetUrl });
@@ -148,44 +155,99 @@ export class ScriptService {
148155
],
149156
}
150157
);
158+
151159
// 兼容 chrome 内核 < 128 处理
152-
const condition: chrome.declarativeNetRequest.RuleCondition = {
153-
regexFilter: "^[^#]+\\.user(\\.bg|\\.sub)?\\.js(\\?.*?)?$",
154-
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
155-
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod],
156-
responseHeaders: [
157-
{
158-
header: "Content-Type",
159-
values: [
160-
"text/javascript*",
161-
"application/javascript*",
162-
"text/html*",
163-
"text/plain*",
164-
"application/octet-stream*",
165-
"application/force-download*",
160+
const browserType = getBrowserType();
161+
const addResponseHeaders = browserType.chrome && browserType.chromeVersion >= 128;
162+
const conditions: chrome.declarativeNetRequest.RuleCondition[] = [
163+
{
164+
regexFilter: "^[^?#]+\\.user(\\.bg|\\.sub)?\\.js([?#][^./\\s#?]*?)*?$",
165+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
166+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
167+
isUrlFilterCaseSensitive: false,
168+
excludedRequestDomains: ["github.com", "gitlab.com", "gitea.com", "bitbucket.org"],
169+
},
170+
{
171+
regexFilter:
172+
"^https?://github.com/[^\\s/?#]+/[^\\s/?#]+/releases/([^\\s.?#]+/|)[^.?#]+.user(\\.bg|\\.sub)?.js([?#][^./\\s#?]*?)*?$",
173+
// https://github.com/<user>/<repo>/releases/latest/download/file.user.js
174+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
175+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
176+
isUrlFilterCaseSensitive: false,
177+
},
178+
{
179+
regexFilter:
180+
"^https?://github.com/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z]+/([^\\s.?#]+/|)[^.?#]+.user(\\.bg|\\.sub)?.js([?#][^./\\s#?]*?)*?$",
181+
// https://github.com/<user>/<repo>/raw/refs/heads/main/.../file.user.js
182+
// https://github.com/<user>/<repo>/raw/<branch>/.../file.user.js
183+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
184+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod], // Chrome 91+
185+
isUrlFilterCaseSensitive: false,
186+
},
187+
{
188+
regexFilter:
189+
"^https?://gitlab\\.com/[^\\s/?#]+/[^\\s/?#]+/-/raw/[a-z0-9_/.-]+/([^\\s.?#]+/|)[^.?#]+\\.user(\\.bg|\\.sub)?\\.js([?#][^./\\s#?]*?)*?$",
190+
// https://gitlab.com/<user>/<repo>/-/raw/<branch>/.../file.user.js
191+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
192+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod],
193+
isUrlFilterCaseSensitive: false,
194+
},
195+
{
196+
regexFilter:
197+
"^https?://gitea\\.com/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z0-9_/.-]+/([^\\s.?#]+/|)[^.?#]+\\.user(\\.bg|\\.sub)?\\.js([?#][^./\\s#?]*?)*?$",
198+
// https://gitea.com/<user>/<repo>/raw/<branch>/.../file.user.js
199+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
200+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod],
201+
isUrlFilterCaseSensitive: false,
202+
},
203+
{
204+
regexFilter:
205+
"^https?://bitbucket\\.org/[^\\s/?#]+/[^\\s/?#]+/raw/[a-z0-9_/.-]+/([^\\s.?#]+/|)[^.?#]+\\.user(\\.bg|\\.sub)?\\.js([?#][^./\\s#?]*?)*?$",
206+
// https://bitbucket.org/<user>/<repo>/raw/<branch>/.../file.user.js
207+
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
208+
requestMethods: ["get" as chrome.declarativeNetRequest.RequestMethod],
209+
isUrlFilterCaseSensitive: false,
210+
},
211+
];
212+
const installPageURL = chrome.runtime.getURL("src/install.html");
213+
const rules = conditions.map((condition, idx) => {
214+
Object.assign(condition, {
215+
excludedTabIds: [chrome.tabs.TAB_ID_NONE],
216+
});
217+
if (addResponseHeaders) {
218+
Object.assign(condition, {
219+
responseHeaders: [
220+
{
221+
header: "Content-Type",
222+
values: [
223+
"text/javascript*",
224+
"application/javascript*",
225+
"text/html*",
226+
"text/plain*",
227+
"application/octet-stream*",
228+
"application/force-download*",
229+
],
230+
},
166231
],
232+
});
233+
}
234+
return {
235+
id: 1000 + idx,
236+
priority: 1,
237+
action: {
238+
type: "redirect" as chrome.declarativeNetRequest.RuleActionType,
239+
redirect: {
240+
regexSubstitution: `${installPageURL}?url=\\0`,
241+
},
167242
},
168-
],
169-
isUrlFilterCaseSensitive: true,
170-
};
171-
const installPageURL = chrome.runtime.getURL("src/install.html");
243+
condition: condition,
244+
} as chrome.declarativeNetRequest.Rule;
245+
});
172246
// 重定向到脚本安装页
173247
chrome.declarativeNetRequest.updateDynamicRules(
174248
{
175-
removeRuleIds: [1],
176-
addRules: [
177-
{
178-
id: 1,
179-
priority: 1,
180-
action: {
181-
type: "redirect" as chrome.declarativeNetRequest.RuleActionType,
182-
redirect: {
183-
regexSubstitution: `${installPageURL}?url=\\0`,
184-
},
185-
},
186-
condition: condition,
187-
},
188-
],
249+
removeRuleIds: [1, ...rules.map((rule) => rule.id)],
250+
addRules: rules,
189251
},
190252
() => {
191253
if (chrome.runtime.lastError) {

0 commit comments

Comments
 (0)