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) => {});

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/440220/1019651/UserScript%20XHR%20Hook%20Framework.js

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @namespace    Xortrox/UserScripts/HookXHR
// @name         UserScript XHR Hook Framework
// @version      0.1
// @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) => {});
// @author       Xortrox
// @match        *
// @esversion:   6
// @license MIT
// ==/UserScript==

class HookXHR {
	constructor() {
		let rawSend = XMLHttpRequest.prototype.send;

		XMLHttpRequest.prototype.send = function() {
			if (!this._hooked) {
				this._hooked = true;

				this.addEventListener('readystatechange', function() {
					if (this.readyState === XMLHttpRequest.DONE) {
						setupHook(this);
					}
				}, false);
			}
			rawSend.apply(this, arguments);
		}

		function setupHook(xhr) {
			if (window.elethorGeneralPurposeOnXHR) {
				const e = new Event('UserScriptXHR');
				e.xhr = xhr;

				window.UserScript.OnXHR.dispatchEvent(e);
			}
		}
		
	}
}

if (!window.UserScript) {
	window.UserScript = {};
}

/** 
 * Can be used to listen for XHR requests
 * the event parameter will contain an xhr field which is the XMLHttpRequest.send itself
 * 
 * window.UserScript.OnXHR.addEventListener((event) => {
 * 		console.log(event.xhr);
 * });
 * */
window.UserScript.OnXHR = new EventTarget();
const hookXHR = new HookXHR();