Skip to content

Commit 2254fd1

Browse files
committed
🐛 修复首次打开浏览器加载脚本的问题
1 parent 223da30 commit 2254fd1

1 file changed

Lines changed: 69 additions & 39 deletions

File tree

src/app/service/service_worker/runtime.ts

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ export class RuntimeService {
164164
});
165165
}
166166

167+
getMessageFlag() {
168+
return Cache.getInstance().get("scriptInjectMessageFlag");
169+
}
170+
167171
// 给指定tab发送消息
168172
sendMessageToTab(to: ExtMessageSender, action: string, data: any) {
169173
if (to.tabId === -1) {
@@ -286,40 +290,57 @@ export class RuntimeService {
286290
}
287291

288292
// 注册inject.js
289-
registerInjectScript() {
290-
chrome.userScripts.getScripts({ ids: ["scriptcat-inject"] }).then((res) => {
291-
if (res.length == 0) {
292-
chrome.userScripts.configureWorld({
293-
csp: "script-src 'self' 'unsafe-inline' 'unsafe-eval' *",
294-
messaging: true,
293+
async registerInjectScript() {
294+
// 如果没设置过, 则更新messageFlag
295+
let messageFlag = await this.getMessageFlag();
296+
if (!messageFlag) {
297+
messageFlag = await this.messageFlag();
298+
const injectJs = await fetch("inject.js").then((res) => res.text());
299+
// 替换ScriptFlag
300+
const code = `(function (MessageFlag) {\n${injectJs}\n})('${messageFlag}')`;
301+
chrome.userScripts.configureWorld({
302+
csp: "script-src 'self' 'unsafe-inline' 'unsafe-eval' *",
303+
messaging: true,
304+
});
305+
const scripts: chrome.userScripts.RegisteredUserScript[] = [
306+
{
307+
id: "scriptcat-inject",
308+
js: [{ code }],
309+
matches: ["<all_urls>"],
310+
allFrames: true,
311+
world: "MAIN",
312+
runAt: "document_start",
313+
},
314+
// 注册content
315+
{
316+
id: "scriptcat-content",
317+
js: [{ file: "src/content.js" }],
318+
matches: ["<all_urls>"],
319+
allFrames: true,
320+
runAt: "document_start",
321+
world: "USER_SCRIPT",
322+
},
323+
];
324+
try {
325+
// 如果使用getScripts来判断, 会出现找不到的问题
326+
// 另外如果使用
327+
await chrome.userScripts.register(scripts);
328+
} catch (e: any) {
329+
LoggerCore.logger().error("register inject.js error", {
330+
error: e,
295331
});
296-
fetch("inject.js")
297-
.then((res) => res.text())
298-
.then(async (injectJs) => {
299-
// 替换ScriptFlag
300-
const code = `(function (MessageFlag) {\n${injectJs}\n})('${await this.messageFlag()}')`;
301-
chrome.userScripts.register([
302-
{
303-
id: "scriptcat-inject",
304-
js: [{ code }],
305-
matches: ["<all_urls>"],
306-
allFrames: true,
307-
world: "MAIN",
308-
runAt: "document_start",
309-
},
310-
// 注册content
311-
{
312-
id: "scriptcat-content",
313-
js: [{ file: "src/content.js" }],
314-
matches: ["<all_urls>"],
315-
allFrames: true,
316-
runAt: "document_start",
317-
world: "USER_SCRIPT",
318-
},
319-
]);
332+
if (e.message?.indexOf("Duplicate script ID") !== -1) {
333+
// 如果是重复注册, 则更新
334+
chrome.userScripts.update(scripts, () => {
335+
if (chrome.runtime.lastError) {
336+
LoggerCore.logger().error("update inject.js error", {
337+
error: chrome.runtime.lastError,
338+
});
339+
}
320340
});
341+
}
321342
}
322-
});
343+
}
323344
}
324345

325346
loadingScript: Promise<void> | null | undefined;
@@ -473,18 +494,27 @@ export class RuntimeService {
473494
if (scriptRes.metadata["run-at"]) {
474495
registerScript.runAt = getRunAt(scriptRes.metadata["run-at"]);
475496
}
476-
if (await Cache.getInstance().get("registryScript:" + script.uuid)) {
477-
await chrome.userScripts.update([registerScript]);
497+
const res = await chrome.userScripts.getScripts({ ids: [script.uuid] });
498+
const logger = LoggerCore.logger({
499+
name: script.name,
500+
registerMatch: {
501+
matches: registerScript.matches,
502+
excludeMatches: registerScript.excludeMatches,
503+
},
504+
});
505+
if (res.length > 0) {
506+
await chrome.userScripts.update([registerScript], () => {
507+
if (chrome.runtime.lastError) {
508+
logger.error("update registerScript error", {
509+
error: chrome.runtime.lastError,
510+
});
511+
}
512+
});
478513
} else {
479514
await chrome.userScripts.register([registerScript], () => {
480515
if (chrome.runtime.lastError) {
481-
LoggerCore.logger().error("registerScript error", {
516+
logger.error("registerScript error", {
482517
error: chrome.runtime.lastError,
483-
name: script.name,
484-
registerMatch: {
485-
matches: registerScript.matches,
486-
excludeMatches: registerScript.excludeMatches,
487-
},
488518
});
489519
}
490520
});

0 commit comments

Comments
 (0)