Can I change the document method of javascript: hasFocus() to return 'true' all the time?

868 Views Asked by At

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.

1

There are 1 best solutions below

6
On

Try this

document.__proto__.hasFocus = function() {return true}