From f4a94bed13a8595ec8a15e3ea8315aeb5aecc744 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Sat, 9 Mar 2024 08:57:59 +0530 Subject: A lot of changes :) --- src/shared/intercept-jquery-ajax.ts | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/shared/intercept-jquery-ajax.ts (limited to 'src/shared/intercept-jquery-ajax.ts') 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 }); +}; -- cgit v1.2.3