6

I am trying to fool this specific code:

<!DOCTYPE html>
<html>
<body>

<p>Click anywhere in the document (the right frame) to get focus. If you click outside the document, it will lose focus.</p>

<p id="demo"></p>

<script>
setInterval("myFunction()", 1);

function myFunction() {
    var x = document.getElementById("demo");
    if (document.hasFocus()) {
        x.innerHTML = "The document has focus.";
    } else {
        x.innerHTML = "The document DOES NOT have focus.";
    }
}
</script>

</body>
</html>

So even when I am not foreground application in Chrome it will return True. Solutions I thought of:

  1. using selenium somehow ?

  2. compiling new hasFocus() method in chrome JS engine or chromium ? that always return True.

Sion C
  • 439
  • 2
  • 14

1 Answers1

8

Try this

document.__proto__.hasFocus = function() {return true}
M1X
  • 4,971
  • 10
  • 61
  • 123
  • 1
    Should I do it each time I enter a new website(selenium) or it override the function completely? – Sion C Apr 25 '18 at 16:00
  • 1
    This will override the current document method, so you have to run this again if you need it in another website (document). – M1X Apr 25 '18 at 16:02
  • so basically I do: browser.get('www.google.com') browser.execute_script('document.__proto__.hasFocus = function() {return true}) And it seems ok, but can't I first change the function before enetering the website, so it will change the function not only for this specific object. ? – Sion C Apr 25 '18 at 16:04
  • 1
    Sorry I have no knowledge regarding Selenium or stuff like that. But as soon as you put that code in any of your javascript files it will override the hasFocus() method of that document. – M1X Apr 25 '18 at 16:10
  • 1
    Unless you can edit the website itself, you will need to run this after you load each page using Selenium. – JeffC Apr 25 '18 at 16:13
  • It's technically possible to inject some JavaScript with Chrome/Selenium just before the page starts loading by calling [Page.addScriptToEvaluateOnNewDocument](https://chromedevtools.github.io/devtools-protocol/tot/Page#method-addScriptToEvaluateOnNewDocument) via the devtool API. Though you'll have to implement the command. For an example have a look at https://stackoverflow.com/questions/45199076/take-full-page-screen-shot-in-chrome-with-selenium/45201692#45201692 – Florent B. Apr 25 '18 at 16:31