I am new to Chrome extensions and I've been trying to make one.
The extension is supposed to override/remove a specific script tag from a webpage.
Let's say for example a page contains the following script tag:
<script>
function foo() {
// contents of function foo
}
foo();
</script>
What I want is to override/remove this function before it gets executed.
I wrote a simple content script for it but it seems that it executes too late. By the time it removes the script tag, the tag has already been executed. Content Script:
function removeScriptTag() {
const pageScripts = document.getElementsByTagName("script");
var element;
for (var i = 0; i < pageScripts.length; i++) {
element = pageScripts[i];
if ((element.innerHTML).includes("function foo")) {
element.parentElement.removeChild(element);
break;
}
}
}
removeScriptTag();
I tried adding "run_at": "document_start"
to the extension manifest file but that made the content script execute too early, before any script tag has loaded in the first place.
I suppose to override/remove the script tag, I will have to somehow detect when the script tag has loaded but now yet executed, the problem was I couldn't find any information on how to do that.