diff options
Diffstat (limited to 'src/shared')
| -rw-r--r-- | src/shared/intercept-jquery-ajax.ts | 41 | ||||
| -rw-r--r-- | src/shared/script-injector.ts | 6 |
2 files changed, 47 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 }); +}; diff --git a/src/shared/script-injector.ts b/src/shared/script-injector.ts new file mode 100644 index 0000000..7abc510 --- /dev/null +++ b/src/shared/script-injector.ts @@ -0,0 +1,6 @@ +export const injectScriptHead = (src: string) => { + const s = document.createElement("script") as HTMLScriptElement; + s.src = chrome.runtime.getURL(src); + // s.onload = () => s.remove(); + (document.head || document.documentElement).append(s); +}; |
