aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/intercept-jquery-ajax.ts
blob: efcb5556a7c3e2b50f6ef531d4293391f635dda0 (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 const interceptPost = (url: string, callback: InterceptJqueryAjaxCallback) => {
	postIntercepts.push({ url, callback });
};

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