aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/intercept-jquery-ajax.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/intercept-jquery-ajax.ts')
-rw-r--r--src/shared/intercept-jquery-ajax.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/shared/intercept-jquery-ajax.ts b/src/shared/intercept-jquery-ajax.ts
new file mode 100644
index 0000000..efcb555
--- /dev/null
+++ b/src/shared/intercept-jquery-ajax.ts
@@ -0,0 +1,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 });
+};