HOOK

HOOK API

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/40863/266172/HOOK.js

  1. // ==UserScript==
  2. // @name HOOK
  3. // @namespace http://www.infosec-wiki.com/
  4. // @version 1.3
  5. // @description HOOK API
  6. // @author http://www.infosec-wiki.com/
  7. // @match *
  8. // @run-at document-start
  9.  
  10. function Hooks() {
  11. return {
  12. initEnv:function () {
  13. Function.prototype.hook = function (realFunc,hookFunc, run, context,funcName) {
  14. var _context = null; //函數上下文
  15. var _funcName = null; //函數名
  16.  
  17. _context = context || window;
  18. //_context = context || unsafeWindow;
  19.  
  20. _funcName = funcName || getFuncName(this);
  21. _context[realFunc] = this;
  22.  
  23. if(_context[_funcName].prototype && _context[_funcName].prototype.isHooked)
  24. {
  25. console.log("Already has been hooked,unhook first");
  26. return false;
  27. }
  28.  
  29. function getFuncName (fn) {
  30. // 獲取函數名稱
  31. var strFunc = fn.toString();
  32. var _regex = /function\s+(\w+)\s*\(/;
  33. var patten = strFunc.match(_regex);
  34. if (patten) {
  35. return patten[1];
  36. };
  37. return '';
  38. }
  39.  
  40. try {
  41. if (run) {
  42. eval('_context[_funcName] = function ' + _funcName + '(){\n' +
  43. 'var args = Array.prototype.slice.call(arguments,0);\n' +
  44. 'var obj = this;\n' +
  45. 'hookFunc.apply(obj,args)\n' +
  46. 'return _context[realFunc].apply(obj,args);\n' +
  47. '};');
  48. }else {
  49. eval('_context[_funcName] = function ' + _funcName + '(){\n' +
  50. 'var args = Array.prototype.slice.call(arguments,0);\n' +
  51. 'var obj = this;\n' +
  52. 'hookFunc.apply(obj,args)\n' +
  53. '};');
  54. }
  55. _context[_funcName].prototype.isHooked = true;
  56. return true;
  57. }catch (e)
  58. {
  59. console.log("Hook failed,check the params.");
  60. return false;
  61. }
  62. };
  63. Function.prototype.unhook = function (realFunc,funcName,context) {
  64. var _context = null;
  65. var _funcName = null;
  66. _context = context || window;
  67. _funcName = funcName;
  68. if (!_context[_funcName].prototype.isHooked)
  69. {
  70. console.log("No function is hooked on");
  71. return false;
  72. }
  73. _context[_funcName] = _context[realFunc];
  74. delete _context[realFunc];
  75. return true;
  76. };
  77. },
  78. cleanEnv:function () {
  79. if(Function.prototype.hasOwnProperty("hook"))
  80. {
  81. delete Function.prototype.hook;
  82. }
  83. if(Function.prototype.hasOwnProperty("unhook"))
  84. {
  85. delete Function.prototype.unhook;
  86. }
  87. return true;
  88. }
  89. };
  90. }