blob: ca752132d0c10461f88f627cfef16432e94cbd00 (
plain)
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
|
const terser = require("terser");
class InjectScriptPlugin {
constructor({ onload, isFirst }) {
this.onload = !!onload;
this.isFirst = !!isFirst;
}
apply(compiler) {
compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => {
compilation.hooks.processAssets.tapPromise(
{
name: "InjectScriptPlugin",
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
additionalAssets: true
},
async assets => {
for (const assetName in assets) {
if (assetName.endsWith(".js")) {
const asset = assets[assetName];
let content = asset._source._children;
let x = -1;
for (let i = 0; i < content.length; i++) {
if (typeof content[i] === "string" && content[i].indexOf("__webpack_exports__") >= 0) {
x = i;
break;
}
}
if (x == -1) {
throw Error("Failed to inject script.");
}
content[x] += `
const observer = new MutationObserver(mutations => {
const script = document.querySelector("head > script:nth-child(54)");
if (script) {
script.innerHTML
`;
if (!this.isFirst) {
content[x] += "+";
}
content[x] += "= `";
if (this.onload) {
content[x] += "document.addEventListener(\"DOMContentLoaded\", () => {";
content[content.length - 3] += "});";
}
content[content.length - 3] += `\`;
observer.disconnect();
}
});
observer.observe(document, {childList: true, subtree: true});
`;
content[x + 1] = (await terser.minify(content[x + 1]._source._source._valueAsString.replaceAll("`", "\\`"))).code;
}
}
}
);
});
}
}
module.exports = InjectScriptPlugin;
|