Hook.js

Javascript function hook

当前为 2022-04-09 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/443030/1037826/Hookjs.js

  1. // ==UserScript==
  2. // @name Hook.js
  3. // @namespace https://github.com/invobzvr
  4. // @version 0.1
  5. // @description Javascript function hook
  6. // @author invobzvr
  7. // @homepageURL https://github.com/invobzvr/invotoys.js/tree/main/hook.js
  8. // @supportURL https://github.com/invobzvr/invotoys.js/issues
  9. // @license GPL-3.0
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. const ORI = {};
  14.  
  15. Function.prototype.hook = function (params) {
  16. typeof params === 'function' && (params = {
  17. scope: window,
  18. before: params,
  19. });
  20. !params.scope && (params.scope = window);
  21. const that = this,
  22. name = that.name;
  23. ORI[name] = that;
  24. params.scope[name] = function () {
  25. if (typeof params.before === 'function') {
  26. const ret = params.before.apply(this, arguments);
  27. if (ret !== undefined) {
  28. return ret;
  29. }
  30. }
  31. let val = that.apply(this, arguments);
  32. if (typeof params.after === 'function') {
  33. const ret = params.after.apply(this, [val, arguments]);
  34. if (ret !== undefined) {
  35. val = ret;
  36. }
  37. }
  38. return val;
  39. }
  40. }
  41. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址