aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/utils/injected-code.js
diff options
context:
space:
mode:
authorArpit Chakladar <arpitchakladar+git@gmail.com>2026-07-13 23:32:26 +0530
committerGitHub <noreply@github.com>2026-07-13 23:32:26 +0530
commitce401d03e75bcbfd1cdfa963954e0807219fb80d (patch)
tree52a813c1705a952312c20c328b00fa845a3453f9 /config/webpack/utils/injected-code.js
parent98e7019c61ea0ffe344ef7ce809d38d62254a9fc (diff)
parentdfba7ff6fff7bde33b40a343a2318b35c429b40f (diff)
downloadbanglar-bhumi-utils-ce401d03e75bcbfd1cdfa963954e0807219fb80d.tar.gz
banglar-bhumi-utils-ce401d03e75bcbfd1cdfa963954e0807219fb80d.zip
Merge pull request #6 from arpitchakladar/adding-documentation-comments
Adding documentation comments
Diffstat (limited to 'config/webpack/utils/injected-code.js')
-rw-r--r--config/webpack/utils/injected-code.js22
1 files changed, 19 insertions, 3 deletions
diff --git a/config/webpack/utils/injected-code.js b/config/webpack/utils/injected-code.js
index a2884f6..f2e51c8 100644
--- a/config/webpack/utils/injected-code.js
+++ b/config/webpack/utils/injected-code.js
@@ -1,13 +1,29 @@
import crypto from "crypto";
-const getHash = url =>
+/**
+ * Computes an MD5 hash suffix (last 16 hex characters) for a given URL.
+ * Used to create unique attribute names for injected script data.
+ *
+ * @param {string} url - The URL to hash.
+ * @returns {string} A 16-character hex hash.
+ */
+const getHash = (url) =>
crypto
.createHash("md5")
.update(url)
.digest("hex")
.substring(16);
-export const getInjectedCode = code => {
+/**
+ * Transforms extension asset URL placeholders (`"$l{ url }l$"`) in the
+ * compiled JavaScript into `document.currentScript.getAttribute("data-<hash>")`
+ * lookups. The mapping of hashes to resolved `chrome.runtime.getURL()` calls
+ * is returned separately so the injector script can embed it as data attributes.
+ *
+ * @param {string} code - The compiled JavaScript bundle.
+ * @returns {[string, string]} A tuple of [transformedCode, extensionAssetsJSON].
+ */
+export function getInjectedCode(code) {
const extensionAssets = {};
let i = 0;
@@ -34,7 +50,7 @@ export const getInjectedCode = code => {
}
const extensionAssetsCode = Object.entries(extensionAssets).map(
- asset => `"${asset[0]}": chrome.runtime.getURL("${asset[1]}")`
+ (asset) => `"${asset[0]}": chrome.runtime.getURL("${asset[1]}")`
).join(",");
return [code, `{${extensionAssetsCode}}`];