aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/utils/get-injection-code.js
blob: 0a57ef98c56f8715d7a7c5259bf17bcee6c121c4 (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
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})})();`;
};