This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcontent-script-injection.ts
More file actions
103 lines (92 loc) · 3.16 KB
/
Copy pathcontent-script-injection.ts
File metadata and controls
103 lines (92 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { consoleError, consoleLog } from '@/shared/utils/console-log';
/*
* This content script is injected programmatically because
* MAIN world injection does not work properly via manifest
* https://bugs.chromium.org/p/chromium/issues/detail?id=634381
*/
class ContentScriptInjectionService {
private registeredScripts = new Set<string>();
init = async () => {
// Register message bridge first and wait a bit to ensure it's loaded
await this.registerMessageBridge();
// Small delay to ensure message bridge is loaded and has injected UUIDs
await new Promise((resolve) => setTimeout(resolve, 50));
// Then register the scripts that run in MAIN world
await this.registerPageProvider();
await this.registerFlowScript();
};
private registerMessageBridge = async () => {
try {
await chrome.scripting.registerContentScripts([
{
id: 'messageBridge',
matches: ['file://*/*', 'http://*/*', 'https://*/*'],
js: ['message-bridge.js'],
runAt: 'document_start',
world: 'ISOLATED',
allFrames: true,
},
]);
this.registeredScripts.add('messageBridge');
consoleLog('Successfully registered message bridge content script');
} catch (err) {
consoleError(`Failed to register message bridge content script: ${err}`);
}
};
private registerPageProvider = async () => {
try {
await chrome.scripting.registerContentScripts([
{
id: 'pageProvider',
matches: ['file://*/*', 'http://*/*', 'https://*/*'],
js: ['pageProvider.js'],
runAt: 'document_start',
world: 'MAIN',
allFrames: true,
},
]);
this.registeredScripts.add('pageProvider');
consoleLog('Successfully registered pageProvider content script');
} catch (err) {
consoleError(`Failed to register pageProvider content script: ${err}`);
}
};
private registerFlowScript = async () => {
try {
await chrome.scripting.registerContentScripts([
{
id: 'flowScript',
matches: ['file://*/*', 'http://*/*', 'https://*/*'],
js: ['script.js'],
runAt: 'document_start',
world: 'MAIN',
allFrames: true,
},
]);
this.registeredScripts.add('flowScript');
consoleLog('Successfully registered flow script content script');
} catch (err) {
consoleError(`Failed to register flow script content script: ${err}`);
}
};
// Method to unregister scripts if needed
unregisterAllScripts = async () => {
try {
const registeredIds = Array.from(this.registeredScripts);
if (registeredIds.length > 0) {
await chrome.scripting.unregisterContentScripts({
ids: registeredIds,
});
this.registeredScripts.clear();
consoleLog('Successfully unregistered all content scripts');
}
} catch (err) {
consoleError(`Failed to unregister content scripts: ${err}`);
}
};
// Method to check if scripts are registered
getRegisteredScripts = () => {
return Array.from(this.registeredScripts);
};
}
export default new ContentScriptInjectionService();