public
Last active

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/**********************************************************************************
*
* @author Jordan Boesch
* @version 1.2
* @link http://boedesign.com/blog/2010/02/13/hijacking-javascriptjquery-methods/
* @date Feb 20, 2010
*
* Hijack is a little snippet of code that allows you to hijack arguments passed to
* functions/methods. Not only can you alter arguments, but you call the old method
* from within the hijacked function (2nd param) and 'return false' if you want to call the
* function yourself.
*
* Be aware that this was more-so a proof of concept. I don't
* fully recommend hijacking and overwriting methods but it does have its uses.
* I thought I would attempt it by making it easy to do... It's still "eval" ;)
*
* USAGE:
*
* Here is how you could hijack the css method in jQuery:
*
----------------------------------------------------------------------------------
var hijackedjQueryCSS = new Hijack('jQuery.fn.css', function(user_args, old_method){
var args = user_args;
if(args[1] == 'dark-violet'){
args[1] = '#6B238E';
}
return args;
});
----------------------------------------------------------------------------------
*
* Now if you run this:
*
----------------------------------------------------------------------------------
$('body').css('color', 'dark-violet')
----------------------------------------------------------------------------------
*
* it will change the color to the hex value you set in your hijacked method.
*
* You can also restore the original method by calling destroy() like so:
*
*
----------------------------------------------------------------------------------
hijackedjQueryCSS.destroy();
----------------------------------------------------------------------------------
*
* Now when you run the code that sets the color to dark-violet, nothing will happen.
*
* If you're interested in over-writing the MooTools css method, then you would use
* this string: Element.prototype.setStyle as the first parameter.
*/
 
/**
* Allows you to alter with arguments before the method is actually called
* @param {Function} method A method/function to call after doing some tampering
* @param {Function} cb A callback that is passed the arguments for you to play with
* @constructor
*/
 
function Hijack(method_name, cb){
// A function needs to be passed or there will be hell to pay
if(Object.prototype.toString.call(cb) !== "[object Function]"){
throw new Error('An anonymous function must be passed as the second parameter!');
}
var self = this,
method_old = eval(method_name); // Keep track of the old one to call
// Make sure this is the first time we create an instance for this method
if(!Hijack._inArray(method_name, Hijack._methodInstances)){
// Store it, just in case we need to restore it for "destroy"
self._orig = { method_name: method_name, method_old: method_old };
Hijack._methodInstances.push(method_name);
// Evil, I know..
eval(method_name + " = function(){var args = Array.prototype.slice.call(arguments);if(a = cb.call(this, args, method_old)){return method_old.apply(this, a)}};");
}
// F off.
else {
throw new Error('You cannot create multiple instances of a specific method (yet), sorry!');
}
}
 
Hijack.prototype = {
/**
* Bring it back to the original method (before it was hijacked)
* @public
*/
destroy: function(){
var self = this;
eval(self._orig.method_name + " = function(){ return self._orig.method_old.apply(this, arguments); }");
}
}
 
/**
* Keep track of methods being hijacked, you can only hijack a method once!
* @private (but not really)
*/
Hijack._methodInstances = [];
 
/**
* Searches an array for a specific value
* @param {String} needle What you're searching for
* @param {Array} haystack What you're searching in
* @private (but not really)
*/
Hijack._inArray = function(needle, haystack){
for(var i = 0, length = haystack.length; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.