0

I'm looking into a way to change a native JS function body, while making it not possible to see that it has been changed. Let's take an example with document.hasFocus():

document.hasFocus = ()=>true;

This method works well to spoof focus, but it can be easily detected that it was modified:

document.hasFocus.toString() // -> "()=>true"

Is there any way, in which I can modify such a function while making it impossible to see it has been tampered with?

ijnkawakaze
  • 118
  • 1
  • 1
  • 8

1 Answers1

1

You can overwrite toString method in Function prototype, and do something like that:

// https://stackoverflow.com/questions/1833588/javascript-clone-a-function
Function.prototype.clone = function() {
    var that = this;
    var temp = function temporary() {
        return that.apply(this, arguments);
    };
    for (var key in this) {
        if (this.hasOwnProperty(key)) {
            temp[key] = this[key];
        }
    }
    return temp;
};

Function.prototype.__oldToString = Function.prototype.toString.clone();

function __toStringHooked() {
    if ((this.name == "")||(this.name == "hasFocus")) // on Firefox, hasFocus doesn't have any name
    {
        return eval+"" // this matches regexp
    } else {
        return this.__oldToString(); // we're returning default value
    }
}

Function.prototype.toString = __toStringHooked
document.hasFocus = () => true

The code above is from Th3B0r3dD3v3l0p3r's GitHub repo, you can check it if you want: https://github.com/Th3B0r3dD3v3l0p3r/focus-spoofer/