diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2024-03-12 18:39:36 +0530 |
|---|---|---|
| committer | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2024-03-12 18:39:36 +0530 |
| commit | 7ea487be4aaf8f3b3047e2a7b3c1da80744f258b (patch) | |
| tree | a4f91d979fe838aefe2ab8c4cecd9037b879e0aa | |
| parent | 95becf369822cbb74f4cefb5658f3ff97e164d6e (diff) | |
| download | banglar-bhumi-utils-7ea487be4aaf8f3b3047e2a7b3c1da80744f258b.tar.gz banglar-bhumi-utils-7ea487be4aaf8f3b3047e2a7b3c1da80744f258b.zip | |
Passing url of extension assets to the injected javascript code using data arguments on the script tag.
| -rw-r--r-- | config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js | 5 | ||||
| -rw-r--r-- | config/webpack/plugins/create-manifest-webpack-plugin/index.js | 10 | ||||
| -rw-r--r-- | config/webpack/plugins/create-rules-webpack-plugin.js | 10 | ||||
| -rw-r--r-- | config/webpack/plugins/inject-script-webpack-plugin.js | 9 | ||||
| -rw-r--r-- | config/webpack/utils/injected-code.js | 41 | ||||
| -rw-r--r-- | src/scripts/functionality/application-receipt.ts | 7 | ||||
| -rw-r--r-- | src/scripts/functionality/mutation-application.ts | 2 | ||||
| -rw-r--r-- | src/scripts/functionality/view-khatian/index.ts | 4 | ||||
| -rw-r--r-- | src/scripts/login.ts | 8 | ||||
| -rw-r--r-- | src/shared/script-injector.ts | 10 |
10 files changed, 94 insertions, 12 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 aaf256d..fd5a0a2 100644 --- a/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js +++ b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js @@ -23,7 +23,10 @@ class CreateInjectedSharedModulesPlugin { .map(sharedModule => `shared/${getFileNameHash(sharedModule, "shared")}.js`); if (injectedSharedScripts.length > 0) { - const injectionCode = `${injectedSharedScripts.map(injectedScript => `$${getFileNameHash("script-injector", "shared", true)}.injectScriptHead("shared/${path.basename(injectedScript)}");`).join("")}`; + const scriptInjectorModuleName = getFileNameHash("script-injector", "shared", true); + const injectionCode = injectedSharedScripts + .map(injectedScript => `$${scriptInjectorModuleName}.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 da62bea..b14cd13 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -98,7 +98,15 @@ class CreateManifestPlugin { compilation.emitAsset( "manifest.json", - new sources.RawSource(JSON.stringify(manifest, undefined, process.env.NODE_ENV === "production" ? undefined : "\t")) + new sources.RawSource( + JSON.stringify( + manifest, + undefined, + process.env.NODE_ENV === "production" + ? undefined + : "\t" + ) + ) ); } ); diff --git a/config/webpack/plugins/create-rules-webpack-plugin.js b/config/webpack/plugins/create-rules-webpack-plugin.js index 0dd05d7..6e31196 100644 --- a/config/webpack/plugins/create-rules-webpack-plugin.js +++ b/config/webpack/plugins/create-rules-webpack-plugin.js @@ -31,7 +31,15 @@ class CreateRulesPlugin { compilation.emitAsset( "rules.json", - new sources.RawSource(JSON.stringify(rules, undefined, process.env.NODE_ENV === "production" ? undefined : "\t")) + new sources.RawSource( + JSON.stringify( + rules, + undefined, + process.env.NODE_ENV === "production" + ? undefined + : "\t" + ) + ) ); } ); diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js index 599871b..665d24e 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -2,6 +2,7 @@ const { sources } = require("webpack"); const path = require("path"); const { getFileNameHash } = webpackRequire("utils/filename-hash"); +const { getInjectedCode } = webpackRequire("utils/injected-code"); class InjectScriptPlugin { constructor(insertedBefore = false) { @@ -19,8 +20,14 @@ class InjectScriptPlugin { async assets => { for (const assetName in assets) { if (/\.js$/.test(assetName)) { + const injectedCodeResponse = getInjectedCode(compilation.getAsset(assetName).source.source()) + const scriptInjectorModuleName = getFileNameHash("script-injector", "shared", true); + compilation.updateAsset( + assetName, + new sources.RawSource(injectedCodeResponse[0]) + ); assets["scripts/" + path.basename(assetName)] = new sources.RawSource( - `$${getFileNameHash("script-injector", "shared", true)}.injectScriptHead("scripts/injected/${path.basename(assetName)}");` + `$${scriptInjectorModuleName}.injectScriptHead("scripts/injected/${path.basename(assetName)}", ${injectedCodeResponse[1]});` ); } } diff --git a/config/webpack/utils/injected-code.js b/config/webpack/utils/injected-code.js new file mode 100644 index 0000000..a4f88ac --- /dev/null +++ b/config/webpack/utils/injected-code.js @@ -0,0 +1,41 @@ +const crypto = require("crypto"); + +const getHash = url => + crypto + .createHash("md5") + .update(url) + .digest("hex") + .substring(16); + +module.exports.getInjectedCode = code => { + const extensionAssets = {}; + let i = 0; + + while (true) { + i = code.indexOf("\"$l{", i); + + if (i < 0) { + break; + } + + const start = i; + const end = code.indexOf("}l$\"", i); + + if (end <= 0) { + break; + } + + const url = code.substring(start + 4, end).trim(); + const urlHash = getHash(url); + code = code.substring(0, start) + `document.currentScript.getAttribute("data-${urlHash}")` + code.substring(end + 4); + extensionAssets[urlHash] = url; + + i = end + 4; + } + + const extensionAssetsCode = Object.entries(extensionAssets).map( + asset => `"${asset[0]}": chrome.runtime.getURL("${asset[1]}")` + ).join(","); + + return [code, `{${extensionAssetsCode}}`]; +}; diff --git a/src/scripts/functionality/application-receipt.ts b/src/scripts/functionality/application-receipt.ts index 704d865..a86405f 100644 --- a/src/scripts/functionality/application-receipt.ts +++ b/src/scripts/functionality/application-receipt.ts @@ -12,9 +12,12 @@ if (captchaElement.length > 0) { const captchaValueElement = $("#captchaText"); const captchaFieldElement = $("[name=txtDrawText]"); - setInterval(() => { + const captcha = () => { captchaFieldElement.val("FFFFFF"); captchaValueElement.val("FFFFFF"); captchaElement.hide(); - }, 1000); + }; + + captcha(); + setInterval(captcha, 1000); } diff --git a/src/scripts/functionality/mutation-application.ts b/src/scripts/functionality/mutation-application.ts index c212823..96aec8a 100644 --- a/src/scripts/functionality/mutation-application.ts +++ b/src/scripts/functionality/mutation-application.ts @@ -1,7 +1,7 @@ import { PDFDocument, StandardFonts } from "pdf-lib"; import { observeDOM } from "@/shared/observe-dom"; -const sanghaFacilitationCentreBannerUrl = chrome.runtime.getURL("/assets/sangha-facilitation-centre-banner.jpg"); +const sanghaFacilitationCentreBannerUrl = "$l{ /assets/sangha-facilitation-centre-banner.jpg }l$"; document.addEventListener("DOMContentLoaded", () => { observeDOM(() => { diff --git a/src/scripts/functionality/view-khatian/index.ts b/src/scripts/functionality/view-khatian/index.ts index 1ca37c5..312c7c1 100644 --- a/src/scripts/functionality/view-khatian/index.ts +++ b/src/scripts/functionality/view-khatian/index.ts @@ -4,7 +4,9 @@ import getDownloadInformationPDFPageContent from "@/scripts/functionality/view-k declare function load(): void; -const sanghaFacilitationCentreBannerUrl = chrome.runtime.getURL("/assets/sangha-facilitation-centre-banner.jpg"); +const sanghaFacilitationCentreBannerUrl = "$l{ /assets/sangha-facilitation-centre-banner.jpg }l$"; +// document.currentScript.getAttribute("data-banner-image-url"); +//("/assets/sangha-facilitation-centre-banner.jpg"); document.addEventListener("DOMContentLoaded", () => { const submitButtonElement = $("#khbutton"); diff --git a/src/scripts/login.ts b/src/scripts/login.ts index 7bc6118..a09eb0a 100644 --- a/src/scripts/login.ts +++ b/src/scripts/login.ts @@ -88,7 +88,7 @@ document.addEventListener("DOMContentLoaded", () => { window.localStorage.removeItem("loginData"); }); - setInterval(() => { + const login = () => { let loginData = window.localStorage.getItem("loginData"); loginData = loginData ? JSON.parse(atob(loginData)) : null; @@ -104,5 +104,9 @@ document.addEventListener("DOMContentLoaded", () => { "json" ); } - }, 1000 * 60 * 5); + + }; + + login(); + setInterval(login, 1000 * 60 * 5); }); diff --git a/src/shared/script-injector.ts b/src/shared/script-injector.ts index 7abc510..a2139c7 100644 --- a/src/shared/script-injector.ts +++ b/src/shared/script-injector.ts @@ -1,6 +1,12 @@ -export const injectScriptHead = (src: string) => { +type DataType = { + [key: string]: string +}; + +export const injectScriptHead = (src: string, data: DataType = {}) => { const s = document.createElement("script") as HTMLScriptElement; s.src = chrome.runtime.getURL(src); - // s.onload = () => s.remove(); + for (const i in data) { + s.setAttribute(`data-${i}`, data[i]); + } (document.head || document.documentElement).append(s); }; |
