3

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.

Community
  • 1
  • 1
WSCodeN
  • 33
  • 4

3 Answers3

1

Less desired but you can try the below code in your content script:

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();
Aefits
  • 3,399
  • 6
  • 28
  • 46
  • This actually works well for the scenario I have given. Check with the last comment I have written for @Kaiido to know more about the issue I am facing. – WSCodeN Oct 31 '18 at 06:04
  • Ok, why do you want to override so many functions of the web page. Can you explain your goal? – Aefits Oct 31 '18 at 06:09
  • not so many functions, just a single function that exists between other functions. It's that when I delete an item, it would give me a notification if I am sure that I want to remove it. I just want to override that one. – WSCodeN Oct 31 '18 at 08:51
0

If you can inject code in the page, then you just need to overwrite window#confirm() method:

(()=>{
  const c = window.confirm;
  window.confirm = (...args) => {
   c.apply(window, args);
   return true;
  }
 })();

console.log(confirm('works?'));

Note that overwritting default methods like this is not recommended, but neither is using window.confirm()...

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • Thanks for your answer, but how can I do the actual "injection"? – WSCodeN Oct 31 '18 at 05:06
  • https://stackoverflow.com/questions/9515704/insert-code-into-the-page-context-using-a-content-script – Kaiido Oct 31 '18 at 05:10
  • I have mixed and matched your answer with @elegant-user solution, and it worked pretty well with the scenario I have given. However it doesn't seem to work on a web application that has multiple functions and scripts going on for it. Could it be because their scripts run before the injected one? – WSCodeN Oct 31 '18 at 06:03
  • Actually nevermind, turns out I had a type with writing the script's name! Thank you for your help! – WSCodeN Oct 31 '18 at 09:08
  • Thanks @wOxxOm, that's better indeed. That was still "working" because `["string"].toString() === "string"`, but obviously that was wrong to write it like that... – Kaiido Feb 23 '21 at 06:24
0

Put the below function code in your content.js file and call the function.When you call this function After that no confirmation box will be shown/appear.

content.js below is the function code

const overrideConfirmBox=()=>{
    let myScript=document.createElement("script");
    myScript.innerHTML=`window.confirm=(...args)=>{ return true; }`;
    document.body.appendChild(myScript);
}

content.js call the above function

overrideConfirmBox()
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29