UserScript XHR Hook Framework

This script is intended to work with @require only. Once required this can be used to listen for XHR events using window.UserScript.OnXHR.addEventListener((event) => {});

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/440220/1019651/UserScript%20XHR%20Hook%20Framework.js

  1. // ==UserScript==
  2. // @namespace Xortrox/UserScripts/HookXHR
  3. // @name UserScript XHR Hook Framework
  4. // @version 0.1
  5. // @description This script is intended to work with @require only. Once required this can be used to listen for XHR events using window.UserScript.OnXHR.addEventListener((event) => {});
  6. // @author Xortrox
  7. // @match *
  8. // @esversion: 6
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. class HookXHR {
  13. constructor() {
  14. let rawSend = XMLHttpRequest.prototype.send;
  15.  
  16. XMLHttpRequest.prototype.send = function() {
  17. if (!this._hooked) {
  18. this._hooked = true;
  19.  
  20. this.addEventListener('readystatechange', function() {
  21. if (this.readyState === XMLHttpRequest.DONE) {
  22. setupHook(this);
  23. }
  24. }, false);
  25. }
  26. rawSend.apply(this, arguments);
  27. }
  28.  
  29. function setupHook(xhr) {
  30. if (window.elethorGeneralPurposeOnXHR) {
  31. const e = new Event('UserScriptXHR');
  32. e.xhr = xhr;
  33.  
  34. window.UserScript.OnXHR.dispatchEvent(e);
  35. }
  36. }
  37. }
  38. }
  39.  
  40. if (!window.UserScript) {
  41. window.UserScript = {};
  42. }
  43.  
  44. /**
  45. * Can be used to listen for XHR requests
  46. * the event parameter will contain an xhr field which is the XMLHttpRequest.send itself
  47. *
  48. * window.UserScript.OnXHR.addEventListener((event) => {
  49. * console.log(event.xhr);
  50. * });
  51. * */
  52. window.UserScript.OnXHR = new EventTarget();
  53. const hookXHR = new HookXHR();

QingJ © 2025

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