-
Notifications
You must be signed in to change notification settings - Fork 108
/
fix-csp.js
88 lines (76 loc) · 2.4 KB
/
fix-csp.js
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
import fs from 'fs';
import path from 'path';
function updateManifest(distFolder) {
// Define the path to the manifest.json file
const manifestPath = path.join(distFolder, 'manifest.json');
// Find the content.js-YYY file in the dist/assets directory
const assetsFolder = path.join(distFolder, 'assets');
let contentJsFile = null;
// Check if the manifest.json file exists
if (!fs.existsSync(manifestPath)) {
console.log(`Error: manifest.json not found in ${distFolder}.`);
return;
}
// Check if the assets folder exists
if (!fs.existsSync(assetsFolder)) {
console.log(`Error: assets folder not found in ${distFolder}.`);
return;
}
// Iterate over files in the assets folder to find the content.js file
const files = fs.readdirSync(assetsFolder);
for (const filename of files) {
if (
!filename.startsWith('content.js-loader') &&
filename.startsWith('content.js-') &&
filename.endsWith('.js')
) {
contentJsFile = filename;
break;
}
}
// If the content.js file was not found, return with an error message
if (contentJsFile === null) {
console.log('Error: content.js-YYY file not found in the assets folder.');
return;
}
// Read the manifest.json file
let manifestData;
try {
const data = fs.readFileSync(manifestPath, 'utf-8');
manifestData = JSON.parse(data);
} catch (e) {
console.log(`Error reading manifest.json: ${e.message}`);
return;
}
// Update the content_scripts entry in the manifest
let updated = false;
if (manifestData.content_scripts) {
for (const script of manifestData.content_scripts) {
if (script.js) {
// Find any entry matching the pattern content.js-loader-*.js
for (let i = 0; i < script.js.length; i++) {
if (/assets\/content\.js-loader-.*\.js/.test(script.js[i])) {
script.js[i] = `assets/${contentJsFile}`;
updated = true;
break;
}
}
}
}
}
if (!updated) {
console.log('No matching content.js-loader-XXX.js entry found in manifest.json.');
return;
}
// Write the updated manifest.json file back to the file system
try {
fs.writeFileSync(manifestPath, JSON.stringify(manifestData, null, 2));
} catch (e) {
console.log(`Error writing to manifest.json: ${e.message}`);
return;
}
console.log(`Updated manifest.json to use ${contentJsFile} in content_scripts.`);
}
// Run the script
const distFolder = './dist'; // Replace this path with the path to your 'dist' folder
updateManifest(distFolder);