blob: 7e1f54eb143f5dc1aea0ae052f07282626fb475b (
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
type InterceptJqueryAjaxCallback = (args: any) => void;
interface InterceptJqueryEntry {
url: string;
callback: InterceptJqueryAjaxCallback;
};
const postIntercepts: InterceptJqueryEntry[] = [];
const getIntercepts: InterceptJqueryEntry[] = [];
/**
* On DOMContentLoaded, proxies jQuery's `$.post` and `$.get` so that
* registered interceptors are called before the real request.
*/
document.addEventListener("DOMContentLoaded", () => {
const proxiedPost = $.post;
const proxiedGet = $.get;
/** Proxied `$.post` that invokes registered interceptors before the real call. */
$.post = function() {
for (const { url, callback } of postIntercepts) {
if (arguments[0].endsWith(url)) {
callback(arguments);
}
}
return proxiedPost.apply(this, Array.from(arguments) as any);
};
/** Proxied `$.get` that invokes registered interceptors before the real call. */
$.get = function() {
for (const { url, callback } of getIntercepts) {
if (arguments[0].endsWith(url)) {
callback(arguments);
}
}
return proxiedGet.apply(this, Array.from(arguments) as any);
};
});
/**
* Registers a callback that fires whenever a jQuery POST request matches
* the given URL suffix.
*
* @param url - The URL suffix to match (checked via `endsWith`).
* @param callback - Receives the original `arguments` from `$.post`.
*/
export function interceptPost(url: string, callback: InterceptJqueryAjaxCallback) {
postIntercepts.push({ url, callback });
};
/**
* Registers a callback that fires whenever a jQuery GET request matches
* the given URL suffix.
*
* @param url - The URL suffix to match (checked via `endsWith`).
* @param callback - Receives the original `arguments` from `$.get`.
*/
export function interceptGet(url: string, callback: InterceptJqueryAjaxCallback) {
getIntercepts.push({ url, callback });
};
|