aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2024-03-09 08:57:59 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2024-03-09 08:57:59 +0530
commitf4a94bed13a8595ec8a15e3ea8315aeb5aecc744 (patch)
treee563f1a74a657348f13999c125f5ca12d200f8e6 /config/webpack
parent435b16e29080f49e1ec373f5b0f552559ca04686 (diff)
downloadbanglar-bhumi-utils-f4a94bed13a8595ec8a15e3ea8315aeb5aecc744.tar.gz
banglar-bhumi-utils-f4a94bed13a8595ec8a15e3ea8315aeb5aecc744.zip
A lot of changes :)
Diffstat (limited to 'config/webpack')
-rw-r--r--config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js4
-rw-r--r--config/webpack/plugins/create-manifest-webpack-plugin/index.js14
-rw-r--r--config/webpack/plugins/inject-script-webpack-plugin.js16
-rw-r--r--config/webpack/utils/filename-hash.js15
-rw-r--r--config/webpack/utils/get-injection-code.js33
-rw-r--r--config/webpack/utils/script-runtime.js2
6 files changed, 30 insertions, 54 deletions
diff --git a/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
index 88959c5..aaf256d 100644
--- a/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
+++ b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
@@ -20,10 +20,10 @@ class CreateInjectedSharedModulesPlugin {
assets => {
const injectedSharedScripts = this.sortedSharedModules
.filter(sharedModule => this.injectedSharedModulesImportedCount[sharedModule] > 0)
- .map(sharedModule => `"/shared/${getFileNameHash(sharedModule, "shared")}.js"`);
+ .map(sharedModule => `shared/${getFileNameHash(sharedModule, "shared")}.js`);
if (injectedSharedScripts.length > 0) {
- const injectionCode = `(async()=>{let e=(await Promise.all([${injectedSharedScripts.join(", ")}].map(async e=>(await fetch(chrome.runtime.getURL(e))).text()))).join(""),t=new MutationObserver(()=>{let n=document.querySelector("head > script:nth-child(54)");n&&(n.innerHTML=e+n.innerHTML,t.disconnect())});t.observe(document,{childList:!0,subtree:!0})})();`;
+ const injectionCode = `${injectedSharedScripts.map(injectedScript => `$${getFileNameHash("script-injector", "shared", true)}.injectScriptHead("shared/${path.basename(injectedScript)}");`).join("")}`;
compilation.emitAsset(
`shared/${getFileNameHash("injected-shared-modules", "shared")}.js`,
new sources.RawSource(injectionCode)
diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
index 91ddf3e..da62bea 100644
--- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js
+++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
@@ -23,6 +23,7 @@ class CreateManifestPlugin {
assets => {
manifest.version = require(path.resolve(ROOT_DIR, "package.json")).version;
manifest.content_scripts = [];
+ let resources = [];
const arrangedScripts = {};
@@ -33,12 +34,15 @@ class CreateManifestPlugin {
const scriptRuntime = getScriptRuntimeFromType(scriptType);
if (typeof arrangedScripts[scriptPath][scriptRuntime] === "undefined") {
- arrangedScripts[scriptPath][scriptRuntime] = [];
- }
+ arrangedScripts[scriptPath][scriptRuntime] = [];
+ }
- arrangedScripts[scriptPath][scriptRuntime].push(`scripts/${getFileNameHash(scriptType, scriptPath)}.js`);
+ arrangedScripts[scriptPath][scriptRuntime].push(`scripts/${getFileNameHash(scriptType, scriptPath)}.js`);
+ if (scriptType.startsWith("injected")) {
+ resources.push(`scripts/injected/${getFileNameHash(scriptType, scriptPath)}.js`);
}
}
+ }
manifest.content_scripts.push({
matches: [`*://banglarbhumi.gov.in/BanglarBhumi/*`],
@@ -66,8 +70,6 @@ class CreateManifestPlugin {
}
}
- let resources = [];
-
for (const assetName in assets) {
if (/^assets\/.*\.(jpg|png|gif)$/.test(assetName)) {
resources.push(assetName);
@@ -83,6 +85,8 @@ class CreateManifestPlugin {
manifest.content_scripts[0].js.unshift(`shared/${getFileNameHash("injected-shared-modules", "shared")}.js`);
}
+ manifest.content_scripts[0].js.unshift(`shared/${getFileNameHash("script-injector", "shared")}.js`);
+
if (resources.length > 0) {
manifest.web_accessible_resources = [
{
diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js
index 0c42dfd..599871b 100644
--- a/config/webpack/plugins/inject-script-webpack-plugin.js
+++ b/config/webpack/plugins/inject-script-webpack-plugin.js
@@ -1,6 +1,7 @@
const { sources } = require("webpack");
+const path = require("path");
-const { getInjectionCode } = webpackRequire("utils/get-injection-code");
+const { getFileNameHash } = webpackRequire("utils/filename-hash");
class InjectScriptPlugin {
constructor(insertedBefore = false) {
@@ -12,19 +13,14 @@ class InjectScriptPlugin {
compilation.hooks.processAssets.tapPromise(
{
name: "InjectScriptPlugin",
- stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE
+ stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
+ additionalAssets: true
},
async assets => {
for (const assetName in assets) {
if (/\.js$/.test(assetName)) {
- compilation.updateAsset(
- assetName,
- new sources.RawSource(
- getInjectionCode(
- compilation.getAsset(assetName).source.source(),
- this.insertedBefore
- )
- )
+ assets["scripts/" + path.basename(assetName)] = new sources.RawSource(
+ `$${getFileNameHash("script-injector", "shared", true)}.injectScriptHead("scripts/injected/${path.basename(assetName)}");`
);
}
}
diff --git a/config/webpack/utils/filename-hash.js b/config/webpack/utils/filename-hash.js
index bd66647..e0d8a6d 100644
--- a/config/webpack/utils/filename-hash.js
+++ b/config/webpack/utils/filename-hash.js
@@ -1,10 +1,19 @@
const crypto = require("crypto");
-module.exports.getFileNameHash = (fileName, prefix) => {
- const hash = crypto
+module.exports.getFileNameHash = (fileName, prefix, justHash = false) => {
+ let hash = crypto
.createHash("md5")
.update(`${prefix}-${fileName}`)
.digest("hex");
- return production ? hash : `${fileName}-${hash.substring(16)}`;
+ if (production) {
+ return hash;
+ } else {
+ hash = hash.substring(16);
+ if (justHash) {
+ return hash;
+ } else {
+ return `${fileName}-${hash}`;
+ }
+ }
};
diff --git a/config/webpack/utils/get-injection-code.js b/config/webpack/utils/get-injection-code.js
deleted file mode 100644
index 0a57ef9..0000000
--- a/config/webpack/utils/get-injection-code.js
+++ /dev/null
@@ -1,33 +0,0 @@
-const insertAtIndex = (str, substring, index) => str.slice(0, index) + substring + str.slice(index);
-
-module.exports.getInjectionCode = (code, front) => {
- let finalCode = code
- .replaceAll("\\", "\\\\")
- .replaceAll("`","\\`")
- .replaceAll("${", "\\${");
-
- let i = 0;
-
- while (true) {
- i = finalCode.indexOf("chrome.runtime", i);
-
- if (i < 0) {
- break;
- }
-
- finalCode = insertAtIndex(finalCode, "\"${", i);
- i = finalCode.indexOf(")", i) + 1;
-
- if (i <= 0) {
- break;
- }
-
- finalCode = insertAtIndex(finalCode, "}\"", i);
- }
-
- const concatenationCode = front
- ? "t.innerHTML=i+t.innerHTML"
- : "t.innerHTML+=i";
-
- return `(()=>{let e=new MutationObserver(()=>{let t=document.querySelector("head > script:nth-child(54)");if(t){let i=\`${finalCode}\`;${concatenationCode},e.disconnect()}});e.observe(document,{childList:!0,subtree:!0})})();`;
-};
diff --git a/config/webpack/utils/script-runtime.js b/config/webpack/utils/script-runtime.js
index f424168..a89d4b8 100644
--- a/config/webpack/utils/script-runtime.js
+++ b/config/webpack/utils/script-runtime.js
@@ -1,7 +1,7 @@
module.exports.getScriptRuntimeFromType = scriptType => {
switch (scriptType) {
case "before":
- case "injected":
+ case "injected-after":
case "injected-before":
return "document_start";