Search code examples
javascriptwindowgmail

JavaScript accessing multiple sites through console


var gmaill = window.open("https://mail.google.com/mail/u/0/#sent", "_blank");
setTimeout(function() {
alert(gmaill.document.getElementById(":113").innerHTML == "Test");
}, 3000);

I am using setInterval to look at a website, and when a condition is met, I am opening a different tab to gmail and sending an email. Unfortunately, I cannot access different tabs that are on different websites through Chrome's console (i.e. I run it from twitter to wait for a tweet and open a new tab for gmail and email someone the link to the tweet). It either doesn't return anything, or there is a security error. Is there any way to get around this? Could I make an extension and have it get around it?


Solution

  • What you are attempting is called Cross site scripting. Your caller is in one domain and actual script is in other. For security reasons most of the browsers follow same origin policy. Meaning to access script or DOM in other window both the windows must be on same domain.

    https://en.wikipedia.org/wiki/Same-origin_policy

    https://en.wikipedia.org/wiki/Cross-site_scripting

    I have tried it earlier. Programmatically it is impossible to do. Not sure if you can achieve using some kind of hacking at browser level