wwmoraes / Native

Published:

Updated:

License: MIT; https://opensource.org/licenses/MIT

Native Overrides

This library allows any script to override native functions pretty much like OOP languages do. It also supports base/super call, which allows the override function to act like a proxy, i.e., you'll still get the native function result.

How to override

Simple Override

It's pretty simple to override a function:

Function.override('aObject.nativeFunction', function(){
    doSomething();
});

You can do this multiple times: any subsequent call will override the last override function.

Override with native function call (a.k.a. base/super call)

If you need to use the native function inside your override, use the base object:

Function.override('aObject.nativeFunction', function(){
    doSomething();
    base.nativeFunction.apply(this);
});

Override with original function arguments

If you need to use the original function arguments, or any arguments you receive, you can use the vanilla's arguments's object:

Function.override('aObject.nativeFunction', function(){
    doSomething.apply(this, arguments);
    base.nativeFunction.apply(this, arguments);
});

Restoring native function

You can restore the native function any time by calling:

Function.restore('aObject.nativeFunction');

This will rollback the function to it's vanilla state, even though the function got overridden multiple times.

Userscripts Using This Library

Rating: 0