aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/utils/injected-code.js
blob: f2e51c8900778a0a83cfcea0745602ef5d5bd0de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import crypto from "crypto";

/**
 * 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);

/**
 * 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;

	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}}`];
};