How can I Inject JavaScript code into a window object

60 Views Asked by At

I am trying to make a JavaScript injector for any website so you don't have to rely on bookmarklets and such. Every time I run my code, it tells me that my newWin.window.location.href = 'about:blank', and it tells me that my newWin.document.body.parentElement.innerHTML = '<head><body></body></head>'

Note: I am using alert(); instead of console.log(); beacuse DevTools is blocked

My Code:


let newWin = window.open('https://my_example.com');
try{
    var js_code = newWin.document.createElement('script');
    js_code.setAttribute('source.js',''); //Note that source.js has "alert('Hello World')" in it
    document.head.appendChild(js_code);
    alert(newWin.document.body.parentElement.innerHTML)    

} catch (error){
    alert(error);
}

I tried to set the window location manually but it stayed the same:

newWin.window.location.href = "https://my_example.com"

but to no avail.

I also tried to use:

document.write(newWin.document.body.parentElement.innerHTML + '<script>alert("Hello World")</script>
0

There are 0 best solutions below