[ js.hook.js ]

javascript钩子; 劫持方法/伪造参数/篡改结果/还原劫持

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

Author
v1
Daily installs
0
Total installs
744
Ratings
7 0 0
Version
0.0.4
Created
2015-11-22
Updated
2016-10-31
Size
12.6 KB
License
N/A
Applies to
All sites
/*
 *
 *  [ js.hook.js ]
 *
 *  javascript钩子
 *
 *  * 劫持方法
 *  * 伪造参数
 *  * 篡改结果
 *  * 还原劫持
 *
 *  * 2016-10-31
 *  * vc1
 *
 */




hook('window.tool.calc').fakeArg('这个计算器坏了', -1).fakeRst('<(ˉ^ˉ)> 告诉你坏了');







window.hook()

传入需要hook的方法

  1. window下的方法可以直接使用hook(alert)的形式
  2. 对象中要劫持的方法使用字符串表示hook('window.object.fn')

.fakeArg()

修改传入原方法的参数

  1. 直接替换 .fakeArg(参数1, 参数2, ..) 原方法接收到的参数将以此为准

  2. 通过方法修改

    .fakeArg(function(原参数1, 原参数2, ..){
        /* 注:只修改上面的形参没啥大用 */
        arguments[0] = 'test';
        return arguments;
    })
    
    • 如果没有返回值会维持原来的参数不变
    • 若要返回多个参数,使用数组 return [a, b, c]
    • 返回空数组将清空所有参数值

.fakeRst()

修改原方法返回的参数

  1. 直接替换 .fakeRst(参数1) 众所周知js中只能返回一个值

  2. 通过方法修改

    .fakeRst(function(原返回值){
        /* 原方法返回值将以此为准 */
        return '修改之后实际返回的值'
    })
    

.fake()

直接替换掉被hook的方法

``` 
.fake(function(args, data){
    /*
    args: 传入的arguments
    data: {
        fn_real,
        fn_name,
        fn_object_name,
        fn_object,
        get fn_puppet(),
        get fakeArgFn(),
        get fakeRstFn()
    }
    */
})
```

.off()

关闭劫持

  1. off('arg') 停止fakeArg的劫持
  2. off('rst') 停止fakeRst的劫持
  3. off() 停止所有劫持,还原被劫持的方法




// 效果测试


/*



window.tool = {
    calc: function(msg, n) {
        console.warn('calc收到参数:' + msg + ', ' + n);
        var r = n * n;
        console.warn('calc结果:' + r);
        return r;
    }
}



console.clear();
console.info('一个计算器:');

console.group('原始方法:\ntool.calc');
console.log(tool.calc);
console.info('设置参数:' + '专注于计算平方的计算器' + ', ' + 42);
console.info('接收到的结果:' + tool.calc('专注于计算平方的计算器', 42));
console.groupEnd();
console.log('\n');


console.group("劫持后:\nhook('window.tool.calc').fakeArg('这个计算器坏了', -1).fakeRst(function(right){\n" +
"    console.info('fakeRst:计算器结果返回:' + right);\n " +
"    return '<(ˉ^ˉ)> 告诉你坏了'\n" +
"}")
hook('window.tool.calc').fakeArg('这个计算器坏了', -1).fakeRstFn(function(right){
    console.info('fakeRst:计算器返回的结果:' + right);
    return '<(ˉ^ˉ)> 告诉你坏了'
});
console.log(tool.calc);
console.info('设置参数:' + '专注于计算平方的计算器' + ', ' + 42);
console.info('接收到的结果:' + tool.calc('专注于计算平方的计算器', 42));
console.groupEnd();
console.log('\n');


console.group("还原后:\nhook('window.tool.calc').off();");
hook('window.tool.calc').off();
console.log(tool.calc);
console.info('设置参数:' + '专注于计算平方的计算器' + ', ' + 42);
console.info('接收到的结果:' + tool.calc('专注于计算平方的计算器', 42));
console.groupEnd();



*/



/*



function print(msg){
    document.write((msg||'<br>') + '<br>');
}

window.tool = {
    calc: function(msg, n) {
        print('calc收到参数:' + msg + ', ' + n);
        var r = n * n;
        print('calc结果:' + r);
        return r;
    }
}


print('一个计算器:');

print('原始方法:\ntool.calc');
print(tool.calc);
print('设置参数:' + '专注于计算平方的计算器' + ', ' + 42);
print('接收到的结果:' + tool.calc('专注于计算平方的计算器', 42));
print();
print('\n');


print("劫持后:\nhook('window.tool.calc').fakeArg('这个计算器坏了', -1).fakeRst(function(right){\n" +
"    print('fakeRst:计算器结果返回:' + right);\n " +
"    return '<(ˉ^ˉ)> 告诉你坏了'\n" +
"}");
hook('window.tool.calc').fakeArg('这个计算器坏了', -1).fakeRstFn(function(right){
    print('fakeRst:计算器返回的结果:' + right);
    return '<(ˉ^ˉ)> 告诉你坏了'
});
print(tool.calc);
print('设置参数:' + '专注于计算平方的计算器' + ', ' + 42);
print('接收到的结果:' + tool.calc('专注于计算平方的计算器', 42));
print();
print('\n');


print("还原后:\nhook('window.tool.calc').off();");
hook('window.tool.calc').off();
print(tool.calc);
print('设置参数:' + '专注于计算平方的计算器' + ', ' + 42);
print('接收到的结果:' + tool.calc('专注于计算平方的计算器', 42));
print();



*/







兼容ie8