-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpatchExt.js
More file actions
190 lines (162 loc) · 7.47 KB
/
patchExt.js
File metadata and controls
190 lines (162 loc) · 7.47 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import "/libs/jszip.min.js"
function loadExtension(file){
let zip = new JSZip()
return zip.loadAsync(file)
}
async function processServiceWorker(ext, serviceWorkerScript, serviceWorkerPath) {
let importedScripts = []
// Get the directory of the service worker file
let serviceWorkerDir = serviceWorkerPath.includes('/')
? serviceWorkerPath.substring(0, serviceWorkerPath.lastIndexOf('/') + 1)
: ''
// Regular expression to match importScripts calls
// Matches: importScripts('script1.js', 'script2.js', ...)
// Handles single quotes, double quotes, and backticks
const importScriptsRegex = /importScripts\s*\(\s*((?:['"`][^'"`]*['"`]\s*,\s*)*['"`][^'"`]*['"`])\s*\)/g
// First pass: extract all script names
let match
while ((match = importScriptsRegex.exec(serviceWorkerScript)) !== null) {
// Extract script names from the importScripts call
let scriptNames = match[1]
.split(',')
.map(name => {
// Remove quotes and trim whitespace
let trimmed = name.trim()
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) ||
(trimmed.startsWith("'") && trimmed.endsWith("'")) ||
(trimmed.startsWith('`') && trimmed.endsWith('`'))) {
return trimmed.slice(1, -1)
}
return trimmed
})
.filter(name => name.length > 0)
// Add each script to the imported scripts array with proper path resolution
for (let scriptName of scriptNames) {
// Resolve relative paths based on service worker location
let resolvedPath = scriptName
if (!scriptName.startsWith('/') && !scriptName.startsWith('http')) {
// Relative path - prepend service worker directory
resolvedPath = serviceWorkerDir + scriptName
}
if (!importedScripts.includes(resolvedPath)) {
importedScripts.push(resolvedPath)
}
}
}
// Second pass: remove all importScripts calls
const removeImportScriptsRegex = /importScripts\s*\(\s*[^)]+\s*\)/g
let processedScript = serviceWorkerScript.replace(removeImportScriptsRegex, '')
return { processedScript, importedScripts }
}
async function patchManifest(ext, extId, store, needsOffscreenPolyfill = false){
let manifest = await ext.file('manifest.json').async('text').then(txt => JSON.parse(txt))
let randomId = (Math.random() + 1).toString(36).substring(2)
let newExtId = `${extId || randomId}@${store || ""}_CRXInstaller`
if(!manifest.theme) {
if(!manifest.background){
manifest.background = {
scripts: []
}
}
if(manifest.background?.service_worker){
manifest.background.service_worker = manifest.background.service_worker.replace(/^\/+/, "")
// Check if service worker file exists
let serviceWorkerFile = ext.file(manifest.background.service_worker)
if (serviceWorkerFile) {
try {
// Process service worker and handle importScripts
let serviceWorkerScript = await serviceWorkerFile.async('text')
let { processedScript, importedScripts } = await processServiceWorker(ext, serviceWorkerScript, manifest.background.service_worker)
// Update the service worker file with processed content
ext.file(manifest.background.service_worker, processedScript)
// Add imported scripts first, then the service worker script
// This ensures imported scripts are loaded before the service worker that calls them
manifest.background.scripts = [...importedScripts, manifest.background.service_worker]
} catch (error) {
console.warn('Failed to process service worker:', manifest.background.service_worker, error)
// Fallback: just convert service_worker to scripts without processing
manifest.background.scripts = [manifest.background.service_worker]
}
} else {
console.warn('Service worker file not found:', manifest.background.service_worker)
// Fallback: just convert service_worker to scripts without processing
manifest.background.scripts = [manifest.background.service_worker]
}
delete manifest.background.service_worker
}
// Add offscreen polyfill if needed (at the beginning)
if (needsOffscreenPolyfill) {
manifest.background.scripts.unshift("offscreenPolyfill.js")
}
manifest.background.scripts.push("uninstallHandler.js")
}
if(manifest.update_url) {
delete manifest.update_url
}
manifest.browser_specific_settings = {
"gecko": {
"id": newExtId
}
}
if(manifest.web_accessible_resources){
manifest.web_accessible_resources.forEach(res => {
if(res.extension_ids) res.extension_ids = [newExtId]
if(res.matches) {
res.matches = res.matches.filter(match => !match.startsWith('chrome-extension://'))
}
})
}
ext.file("manifest.json", JSON.stringify(manifest, null, "\t"))
return ext
}
async function checkOffscreenUsage(ext) {
// Check manifest.json for offscreen permissions
try {
let manifestFile = ext.file('manifest.json')
if (manifestFile) {
let manifest = await manifestFile.async('text').then(txt => JSON.parse(txt))
if (manifest.permissions && manifest.permissions.includes('offscreen')) {
return true
}
}
} catch (e) {
// Ignore manifest parsing errors
}
// Check all JavaScript files for chrome.offscreen usage
let files = Object.keys(ext.files)
for (let filename of files) {
if (filename.endsWith('.js') || filename.endsWith('.mjs')) {
try {
let file = ext.file(filename)
if (file) {
let content = await file.async('text')
if (content.includes('chrome.offscreen') || content.includes('offscreen')) {
return true
}
}
} catch (e) {
// Ignore file reading errors
}
}
}
return false
}
async function injectScripts(ext, needsOffscreenPolyfill = false){
let uninstallHandler = await fetch("/injects/uninstallHandler.js").then(res => res.arrayBuffer())
ext.file("uninstallHandler.js", uninstallHandler)
// Inject offscreen polyfill if needed
if (needsOffscreenPolyfill) {
let offscreenPolyfill = await fetch("/injects/offscreenPolyfill.js").then(res => res.arrayBuffer())
ext.file("offscreenPolyfill.js", offscreenPolyfill)
}
return ext
}
export async function patchExt(file, extId, store){
let ext = await loadExtension(file)
// Check if offscreen polyfill is needed
let needsOffscreenPolyfill = await checkOffscreenUsage(ext)
ext = await injectScripts(ext, needsOffscreenPolyfill)
ext = await patchManifest(ext, extId, store, needsOffscreenPolyfill)
return await ext.generateAsync({type: "arraybuffer"})
}
export { processServiceWorker }