JavaScript API for easy hooking and managing native APIs.
Hooking or function overriding is a well known technique in programming.
It's usefull for debugging your code, extending plugins and doing things that seem impossible( for example overriding events mechanisms in JS, to gain full control over events ).
There are couple of problems according to hooking, which are`
- Saving original functions in some container(if you'r the hooker, you need to use original functions).
- Hooked functions can easily be spotted.
- It's easy to break things around because hooked methods are different than originals.
Here is a complete hooking API for JavaScript which solves all the problems mentioned above.
This API uses error prone hooking techniques using which hooked functions cannot be spotted.
It has local container for original functions which is well organized.
Also this API solves vendor prefixing problems.
Dependencies.
This package depends on vendor-prefixes API.
You can install this using bower.
bower install hooking-api
Or load dependencies manually.
To use with NodeJS run.
npm install hooking-api.
API reference.
Hooking API!!
Natives.hook( ...path, [options], generator, [...gen_args] );
Loads and hooks given functions.
Path is the list of functions that needs to be hooked. Every item is a string path to it's function like 'Array.prototype.slice'.
Also i've added a support for getter/setter functions too!!.
To point to a getter of some property, you need to write 'HTMLElement.prototype.onclick > get'.
Or '... > set' to point to a setter function.
You can give options to control original functions loading and hooking.
Here are all available options.
- bindToParent. This is used to bind loaded functions to its parents.
- from. If you need to load multiple functions from the same source, use this.
For example if you need to load addEventListener, removeEventListener, dispatchEvent functions from EventTarget.prototype, you can pass 'EventTarget.prototype' to from arg. - ignoreHookeds. Pass false, if you want to hook methods even if they're already hooked. By default it's true.
- rehook. Works only with ignoreHookeds flag set to false. Pass false if you need to hook already hooked methods. By default it's true, which means that hook call will rewrite all your pervious hooks of a partuclar function.
Generator is the hooker function, which hooks needed functions based on the original or the current version( it's not an optional argument ).
Generator arguments(gen_args) are additional arguments to pass to generator function.
Usage.
// Hooking addEventListener and removeEventListener methods of EventTarget.Natives; // After hooking those methods you can try to bind some event to some element, // message that prints hooked function will be printed for every addEventListener or removeEventListener call, let's try.document;console;
Here is the shorter way to do the same as above.
Natives;
You can hook a function, and get the original one of that function like this.
Natives; // You have the original setTimeout function here. :)// This is the container for loaded functions.console;
As I mentioned above, there is a new feature, which allows to access getter/setter accessor methods.
Let's try to hook HTML element's onclick setter.
Natives; // After this call you will see a new line from the hooked function in your console.document { console;} // You can access this function's original version from here.// NOTE: you must write the path without any space character.console;
ONLY IN WEB: Accually you cant use this setTimeout function directly, it will throw an error with text 'Illegal invocation'.
try Natives$; catch err console; // But theres a way to call this setTimeout.Natives$setTimeout;
This is because setTimeout function must be called on window only, and that's why there is an option called bindToParent.
Try to pass option object {bindToParent: true} when hooking, and try to call setTimeout again.
Natives; // Now this will work, because it's bound to window.Natives$;
Accually there is a simpler way to bind already loaded function to it's parent, see in the section Helpier methods.
ignoreHookeds flag prevents doing multiple hooks on the same functions.
For example, if you try to hook the method HTMLElement.prototype.addEventListener, and then hook the HTMLDocument.prototype.addEventListener you will accually hooked that method twice, because in modern browsers HTMLDocument and HTMLElement classes inherit addEventListener from EventTarget class and those methods are the same.
Here is demo.
// Using this flags to create a situation described above.Natives; // This call will cause 2 same lines('Called addEventListener method!!!') in your console.document;
So ignoreHookeds flag prevents situations like this.
Let's try to hook a function multiple times and then rewrite all changes we've done.
Natives; // Rehooking already hooked method.Natives; // window.setTimeout call will print this two lines in your console.// 'setTimeout first hook call'// 'setTimeout second hook call'// This is because the second hooker's original argument was the already hooked version.window;
Hooked functions keep original's name, arguments count and toString declaration.
To get sure that hooked function is the same as original, try this.
Natives;console;console;console;
This proves that it's impossible to differentiate the original function from the hooked one, so this makes safe to hook any function from anywhere.
The only differnce that remains is that errors that have been thrown from original functions will have different backtraces.
Backtraces will have more entries because of hooks.
Natives.restore( ...path, [options] );
Restores hooked functions to original ones.
Natives; // This will print 'setTimeout has been called' to your console, because it's hooked.window; // Restoring original function.Natives; // So this will not print anything anymore.window;
Hooking functions directly.
Natives.hook( function, [options], generator, [...gen_args] );
You can pass function as argument to Natives.hook, instead of paths.
If there is no need to fake hooked functions properties, give fake: false option, it will improve performance.
If you want to save hooked pair into this API's local container, pass save option.
Make sure that this will not become a garbage, or delete it when it will.
Examples of usage of saveHooked, removeHooked, originalOf, hookedOf, hook methods.
{ console;}; // Hooking DoSomething function.var hooked = Natives; // Checking are this functions different.// Will print 3 trues.console;console;console; // Try this one with gecko.if 'toSource' in Functionprototype console; // Hooking DoSomething again, but this time without faking it.var hooked = Natives; // Checking are this functions different.// This time this will print 3 falses.console;console;console; // Now tryinig to hook and save function in the local container of this API.var hooked = Natives; // 2 trues.console;console;
Natives.saveHooked( hooked, original );
If you hooked a method for your own, add it to this API's container.
Be carefull, this will save given functions in the local container and garbage collection wont collect functions that have been removed, so you need to delete this methods manually.
Natives.removeHooked( function );
This removes hooked and original pair of functions from local containers.
Example of usage of saveHooked and removeHooked methods
{ console;}; // Now tryinig to hook and save function in the local container of this API.var hooked = Natives; Natives; // 2 falses. because we removed all info for this functions from container.console;console; // It's possible to save into container manually too.var hooked = Natives;Natives; // Again 2 trues.console;console;Natives;
Natives.originalOf( function, [include_bound] );
Give a function and it will return original version of it.
Will return bound version if include_bound is true and original version has been bound to parent.
Natives.hookedOf( function );
Give a function and it will return hooked version of it.
Natives.findProto( object, method );
Finds a prototype object which has own given method.
Example`
var target = document proto = Natives; // Will print true.console;
Helper methods.
Natives.load( ...path, [options] );
Loads functions original versions into container.
Options possible flags.
- bindToParent
- from
We've discussed both of this flags above.
Usage example.
// Will find and save setTimeout and setInterval native versions.Natives;Natives; // After loading those methods will be available here.// Natives.$ is the container for all loaded functions.console;console;
Using with options.
As mentioned above, here is a technique to reload wanted function with bindToParent.
// Lets load setTimeout method.Natives; // But this will throw an error with text 'Illegal invocation'.// It's because setTimeout is only allowed to be called from window object.// Already discussed this above.try Natives$; catch err console; // So you can pass bindToParent flag here too.Natives; // This will work now.Natives$;
Fast loading functions from the same source.
// You can load functions from the same source, so instead of writing this.Natives; // You can pass from option to loader, and get multiple functions from the same source. // It will look like this.Natives;
Functions with paths can be accessed like this.
console; // Or like this.console;
But this one will return the real(maybe hooked) version.
It's because EventTarget.prototype is the real prototype of EventTarget interface, so in order to get the original functions, you need to access them using methods mentioned above.
It's a little complicated, but you will understand it after doing few trys.
console;
Lets hook addEventListener to check it.
Natives; // This will print false.console;
Natives.need( ...path, [options] );
Same as load, but returns loaded functions.
Will return an object which maps given function names to functions.
Functions that does not exist, will not be included into this object.
In this example resulted object will map setTimeout and setInterval to it's functions, but doAnything will not be included.
console;
If you give 'names' flag, function will return only filtered list, which contains loaded function names only.
This one will result to [ 'setTimeout', 'setInterval' ];
console;
If you pass 'first' flag only first matched object will be returned.
In this case it will be setTimeout function.
console;
Flags can be combined.
This will return first existed method's name.
So this will print 'setTimeout'.
console;
Natives.translate( path, [...additional_variants], [options] );
Translates vendor prefixed versions of functions to original, keeps all into container and groups that changes, so it will be easy to hook all of them.
Path is the function's path that needs to be translated.
Additional variants. There are cases where function haves additional variants as "vendor prefixed versions", which have different name.
Available options to pass with options argument.
- getAll. Pass this flag and translate will return all verions of functions that are available on this system.
- prefixType( 'JS', 'JSClass', 'const' ). Give prefixing style with this option.
- bindToParent. Same as for loader.
- from. Same as for loader.
Returns first available version found.
NOTE: Translation functionality is not available in NodeJS, because there are no vendor prefixes there.
Try this with webkit.
Natives;
In webkit there are 3 available functions for cancelling requested animation frame.
- cancelAnimationFrame
- webkitCancelAnimationFrame
- webkitCancelRequestAnimationFrame
As we can see third version is not a vendor prefixed version of cancelAnimationFrame. It's a prefixed version of cancelRequestAnimationFrame, which does the same thing as cancelAnimationFrame, so this is considered as an additional version of those functions.
You can pass this additional variants too.
Try to delete cancelAnimationFrame default function, to see what happenes in case of default version is not available.
ATTENTION: We've passed getAll flag, this will force translator to return all found versions in the system.
delete windowcancelAnimationFrame;console; // This will print webkitCancelAnimationFrame method.// As we can see, API translated vendor prefixed version to original one for easier use.console;
If you need to convert JavaScript classes such as MutationObserver, use vendorPrefix flag to change default prefixing type.
This will try following versions.
MutationObserver, WebkitMutationObserver, MozMutationObserver, MsMutationObserver, OMutationObserver.
Natives;
If you need to convert JavaScript constants such as CSSRule.KEYFRAMES_RULE, use vendorPrefix flag to change default prefixing type.
This will try the following versions
KEYFRAMES_RULE, WEBKIT_KEYFRAMES_RULE, MOZ_KEYFRAMES_RULE, MS_KEYFRAMES_RULE, O_KEYFRAMES_RULE.
Natives;
Translating constants are not usefull for hooking, but it's an additional feature that makes vendor prefixed versions easier to organize.
If there is vendor prefix versions available for the same function, first translate it to parse all known translations, than hook it.
It will override all variants of available functions.
// As mentioned above, cancelAnimationFrame in webkit is available in 3 forms` // cancelAnimationFrame, webkitCancelAnimationFrame and webkitCancelRequestAnimationFrame.// So we will load all versions of this method.Natives;
Now we hook cancelAnimationFrame.
Natives;
After this call, you will see following three lines in your console(if you'r using webkit)
'Hooking cancelAnimationFrame'
'Hooking webkitCancelAnimationFrame'
'Hooking webkitCancelRequestAnimationFrame'
This means that all versions of cancelAnimationFrame will be hooked.
But you must call hook after the translate to be able to do this.
And that's all. Please submit bug report if you find something.
Browser support
Firefox | Chrome | IE | Opera | Safari |
---|---|---|---|---|
7 | 5 | 9 | 12 | 5.1 |