To simplify the example, let us say I have the following web page:
<html>
<input type="button" value="click me!" onclick="Confirmation()">
</html>
<script>
function Confirmation() {
if (window.confirm("Are you sure you want to proceed?")) {
alert("proceeded!");
}
}
</script>
What I am trying to do is inject my chrome extension in a way that it would either override the confirmation() function,
or would change it to the following:
<script>
function Confirmation() {
if (true) {
alert("proceeded!");
}
}
</script>
how could I achieve that?
Edit:
This is the solution that worked with me:
@elegant-user 's function
var jsCodeInjection = '' + function Confirmation() {
if (true) {
alert("proceeded cs!");
}
} + '';
var script = document.createElement('script');
script.textContent = jsCodeInjection;
(document.body||document.documentElement).appendChild(script);
script.remove();
If you want a cleaner way of implementing it, then:
@Kaiido 's referral for how to inject a seperate .js file for a cleaner implementation:
Insert code into the page context using a content script
If the script element has multiple functions, and you're trying to override one(or some) of them,
then you have to copy all the functions in a separate js file, and adjust the functions that you want to adjust.