From feac7acb91a78b117241abdd0a5d80f778e15f06 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Fri, 24 Mar 2023 11:36:54 +0530 Subject: Overriding the javascript on the page instead of locally replacing it. --- .../create-manifest-webpack-plugin/index.js | 2 +- .../plugins/inject-script-webpack-plugin.js | 41 ++++++++++++++++++++++ .../get-injection-code.js | 18 ---------- .../plugins/inject-script-webpack-plugin/index.js | 30 ---------------- 4 files changed, 42 insertions(+), 49 deletions(-) create mode 100644 config/webpack/plugins/inject-script-webpack-plugin.js delete mode 100644 config/webpack/plugins/inject-script-webpack-plugin/get-injection-code.js delete mode 100644 config/webpack/plugins/inject-script-webpack-plugin/index.js (limited to 'config/webpack/plugins') diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index f0d9084..6984955 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -29,7 +29,7 @@ class CreateManifestPlugin { manifest.content_scripts.push({ matches, - js: [`scripts/${getFileNameHash(scriptPath)}/${scriptType}.js`], + js: [`scripts/${getFileNameHash(scriptPath)}-${scriptType}.js`], run_at: getScriptRuntimeFromType(scriptType) }); } diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js new file mode 100644 index 0000000..8d72ea2 --- /dev/null +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -0,0 +1,41 @@ +const { sources } = require("webpack"); +const terser = require("terser"); + +const { getInjectionCode } = webpackRequire("utils/get-injection-code"); + +class InjectScriptPlugin { + apply(compiler) { + compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => { + compilation.hooks.processAssets.tapPromise( + { + name: "InjectScriptPlugin", + stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE, + additionalAssets: true + }, + async assets => { + for (const assetName in assets) { + let injected = false; + let injected_before = false; + + if (assetName.endsWith("injected.js")) + injected = true; + + else if (assetName.endsWith("injected-before.js")) { + injected = true; + injected_before = true; + } + + if (injected) { + compilation.updateAsset( + assetName, + new sources.RawSource(getInjectionCode(compilation.getAsset(assetName).source.source(), injected_before)) + ); + } + } + } + ); + }); + } +} + +module.exports = InjectScriptPlugin; diff --git a/config/webpack/plugins/inject-script-webpack-plugin/get-injection-code.js b/config/webpack/plugins/inject-script-webpack-plugin/get-injection-code.js deleted file mode 100644 index 2993c44..0000000 --- a/config/webpack/plugins/inject-script-webpack-plugin/get-injection-code.js +++ /dev/null @@ -1,18 +0,0 @@ -module.exports.getInjectionCode = code => { - const escapeSequences = [["\b", "\\b"], - ["\f", "\\f"], - ["\n", "\\n"], - ["\r", "\\r"], - ["\t", "\\t"], - ["\v", "\\v"] - ]; - let finalCode = code.replaceAll("`","\\`"); - - for (const escapeSequence of escapeSequences) { - finalCode = finalCode - .replaceAll(escapeSequence[1], "\\" + escapeSequence[1]) - .replaceAll(escapeSequence[0], escapeSequence[1]); - } - - return `(()=>{const o=new MutationObserver(()=>{let t=document.querySelector("head > script:nth-child(54)");if(t){let e=\`(()=>{${finalCode}})();\`;null==t.getAttribute("data-replaced")?(t.innerHTML=e,t.setAttribute("data-replaced","")):t.innerHTML+=e,o.disconnect()}});o.observe(document,{childList:!0,subtree:!0});})();`; -}; diff --git a/config/webpack/plugins/inject-script-webpack-plugin/index.js b/config/webpack/plugins/inject-script-webpack-plugin/index.js deleted file mode 100644 index a0ee1f7..0000000 --- a/config/webpack/plugins/inject-script-webpack-plugin/index.js +++ /dev/null @@ -1,30 +0,0 @@ -const { sources } = require("webpack"); -const terser = require("terser"); - -const { getInjectionCode } = webpackRequire("plugins/inject-script-webpack-plugin/get-injection-code"); - -class InjectScriptPlugin { - apply(compiler) { - compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => { - compilation.hooks.processAssets.tapPromise( - { - name: "InjectScriptPlugin", - stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE, - additionalAssets: true - }, - async assets => { - for (const assetName in assets) { - if (assetName.endsWith("injected.js")) { - compilation.updateAsset( - assetName, - new sources.RawSource(getInjectionCode(compilation.getAsset(assetName).source.source())) - ); - } - } - } - ); - }); - } -} - -module.exports = InjectScriptPlugin; -- cgit v1.2.3