aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins/inject-script-webpack-plugin.js
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-02-06 19:30:21 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-02-06 19:30:21 +0530
commitc57a9e3d9e304bd360437f8f732dbc0479376cd7 (patch)
treead9e31238aab26fd7a8ceb54d1424daa79a237c9 /config/webpack/plugins/inject-script-webpack-plugin.js
parent1a301791bb621fa01fa76e9403b23fe1f4a120af (diff)
downloadbanglar-bhumi-utils-c57a9e3d9e304bd360437f8f732dbc0479376cd7.tar.gz
banglar-bhumi-utils-c57a9e3d9e304bd360437f8f732dbc0479376cd7.zip
Fixed some bugs... Updated some code...
Diffstat (limited to 'config/webpack/plugins/inject-script-webpack-plugin.js')
-rw-r--r--config/webpack/plugins/inject-script-webpack-plugin.js28
1 files changed, 17 insertions, 11 deletions
diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js
index 63bab27..ca75213 100644
--- a/config/webpack/plugins/inject-script-webpack-plugin.js
+++ b/config/webpack/plugins/inject-script-webpack-plugin.js
@@ -1,10 +1,9 @@
const terser = require("terser");
-let count = 0;
-
class InjectScriptPlugin {
- constructor({ onload }) {
+ constructor({ onload, isFirst }) {
this.onload = !!onload;
+ this.isFirst = !!isFirst;
}
apply(compiler) {
@@ -20,30 +19,37 @@ class InjectScriptPlugin {
if (assetName.endsWith(".js")) {
const asset = assets[assetName];
let content = asset._source._children;
- let x = 2;
+ 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) {
- `
- if (count > 0) {
- content[x] += "script.innerHTML += `";
- } else {
- content[x] += "script.innerHTML = `";
+ 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[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;
- count++;
}
}
}