aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/intercept-jquery-ajax.ts
blob: a77dd441153e75eeeb6f54fe8971acc1cdbf9c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
type InterceptJqueryAjaxCallback = (args: any) => void;

interface InterceptJqueryEntry {
	url: string;
	callback: InterceptJqueryAjaxCallback;
};

const postIntercepts: InterceptJqueryEntry[] = [];
const getIntercepts: InterceptJqueryEntry[] = [];

document.addEventListener("DOMContentLoaded", () => {
	const proxiedPost = $.post;
	const proxiedGet = $.get;

	$.post = function() {
		for (const { url, callback } of postIntercepts) {
			if (arguments[0].endsWith(url)) {
				callback(arguments);
			}
		}

		return proxiedPost.apply(this, Array.from(arguments) as any);
	};
	$.get = function() {
		for (const { url, callback } of getIntercepts) {
			if (arguments[0].endsWith(url)) {
				callback(arguments);
			}
		}

		return proxiedGet.apply(this, Array.from(arguments) as any);
	};
});

export function interceptPost(url: string, callback: InterceptJqueryAjaxCallback) {
	postIntercepts.push({ url, callback });
};

export function interceptGet(url: string, callback: InterceptJqueryAjaxCallback) {
	getIntercepts.push({ url, callback });
};