function someFunc (arg) { alert(arg)}
function injectFunc(original, injection, inject_after) {
if(typeof original === "string") original = window[original];
return function() {
if(!inject_after) injection.apply(this, arguments);
var result = original.apply(this, arguments);
if(inject_after) injection.apply(this, arguments);
return result;
}
}
// insert before
someFunc = injectFunc(someFunc, function(arg) {alert("Before" + arg)});
// or with string func name
someFunc = injectFunc('someFunc', function(arg) {alert("Before" + arg)});
// and after
someFunc = injectFunc(someFunc, function(arg) {alert("After" + arg)}, true);
// and with olny func name
window['someFunc'] = injectFunc('someFunc', function(arg) {alert("After" + arg)}, true);
すべての注入は関数に保存されます