aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins/inject-script-webpack-plugin/get-injection-code.js
blob: 2993c44bc7a4efa6b3ae828cf202e70764a90e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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});})();`;
};