-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveInlineScript.cjs
More file actions
49 lines (43 loc) · 1.56 KB
/
removeInlineScript.cjs
File metadata and controls
49 lines (43 loc) · 1.56 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
// code from https://github.com/tiodot/my-chrome-extension/blob/main/removeInlineScript.cjs
// this saved me a major headache
const glob = require("tiny-glob");
const path = require('path');
const fs = require('fs');
function hash(value) {
let hash = 5381;
let i = value.length;
if (typeof value === "string") {
while (i) hash = (hash * 33) ^ value.charCodeAt(--i);
} else {
while (i) hash = (hash * 33) ^ value[--i];
}
return (hash >>> 0).toString(36);
}
async function removeInlineScript(directory) {
console.log("Removing Inline Scripts");
const scriptRegx = /<script>([\s\S]+)<\/script>/;
const files = await glob("**/*.{html}", {
cwd: directory,
dot: true,
aboslute: true,
filesOnly: true,
});
files
.map((f) => path.join(directory, f))
.forEach((file) => {
console.log(`edit file: ${file}`);
const f = fs.readFileSync(file, { encoding: 'utf-8' });
const script = f.match(scriptRegx)
if (script && script[1]) {
const inlineContent = script[1]
.replace('__sveltekit', 'const __sveltekit')
.replace('document.currentScript.parentElement', 'document.body.firstElementChild');
const fn = `/script-${hash(inlineContent)}.js`;
const newHtml = f.replace(scriptRegx, `<script type="module" src="${fn}"></script>`);
fs.writeFileSync(file, newHtml);
fs.writeFileSync(`${directory}${fn}`, inlineContent);
console.log(`Inline script extracted and saved at: ${directory}${fn}`);
}
});
}
removeInlineScript(path.resolve(__dirname, 'build'));