From 34ad953c953a395ba3e51c8125034a6bb65b2a79 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Mon, 13 Feb 2023 22:49:42 +0530 Subject: Fixed not being able to import modules in scripts. --- src/scripts.json | 4 +-- src/scripts/auto-login.ts | 58 ++++++++++++++++++++------------------- src/scripts/autofill-otps.ts | 36 ++++++++++++------------ src/scripts/optimize/home-page.ts | 10 ++++--- src/scripts/optimize/index.ts | 10 ++++--- src/utils/observe-dom.ts | 8 ++++++ 6 files changed, 70 insertions(+), 56 deletions(-) create mode 100644 src/utils/observe-dom.ts (limited to 'src') diff --git a/src/scripts.json b/src/scripts.json index 6980407..ba6f73f 100644 --- a/src/scripts.json +++ b/src/scripts.json @@ -4,9 +4,7 @@ "optimize/home-page.ts": "Home.action" }, "injected": { - "stop-blocking.js": "*" - }, - "injected_and_loaded": { + "stop-blocking.js": "*", "autofill-otps.ts": "*", "auto-login.ts": "*" }, diff --git a/src/scripts/auto-login.ts b/src/scripts/auto-login.ts index 1d2b379..ae1682b 100644 --- a/src/scripts/auto-login.ts +++ b/src/scripts/auto-login.ts @@ -1,32 +1,34 @@ -const proxiedPost = $.post; +document.addEventListener("DOMContentLoaded", () => { + const proxiedPost = $.post; -let loginData: any | null = null; + let loginData: any | null = null; -$.post = function() { - if (arguments[0].endsWith("login.action")) { - const callback = arguments[2]; - arguments[2] = (data: any) => { - if (!data.exception && data.checkmsg === "success") { - loginData = arguments[1]; - } - callback(data); - }; - } + $.post = function() { + if (arguments[0].endsWith("login.action")) { + const callback = arguments[2]; + arguments[2] = (data: any) => { + if (!data.exception && data.checkmsg === "success") { + loginData = arguments[1]; + } + callback(data); + }; + } - return proxiedPost.apply(this, Array.from(arguments) as any); -}; + return proxiedPost.apply(this, Array.from(arguments) as any); + }; -setInterval(() => { - if (loginData) { - $.post( - "/BanglarBhumi/login.action", - loginData, - data => { - if (data.exception || data.checkmsg !== "success") { - loginData = null; - } - }, - "json" - ); - } -}, 1000 * 60 * 5); + setInterval(() => { + if (loginData) { + $.post( + "/BanglarBhumi/login.action", + loginData, + data => { + if (data.exception || data.checkmsg !== "success") { + loginData = null; + } + }, + "json" + ); + } + }, 1000 * 60 * 5); +}); diff --git a/src/scripts/autofill-otps.ts b/src/scripts/autofill-otps.ts index f2c5650..140504c 100644 --- a/src/scripts/autofill-otps.ts +++ b/src/scripts/autofill-otps.ts @@ -1,20 +1,22 @@ -const proxiedPost = $.post; +document.addEventListener("DOMContentLoaded", () => { + const proxiedPost = $.post; -$.post = function() { - let element: HTMLInputElement | null = null; - if (arguments[0].endsWith("MISPetitionSmsSend.action")) { - element = document.querySelector("input[name=mobile_Code]") as HTMLInputElement; - } else if (arguments[0].endsWith("MISPetitionEmailSend.action")) { - element = document.querySelector("input[name=txtemail_Code]") as HTMLInputElement; - } + $.post = function() { + let element: HTMLInputElement | null = null; + if (arguments[0].endsWith("MISPetitionSmsSend.action")) { + element = document.querySelector("input[name=mobile_Code]") as HTMLInputElement; + } else if (arguments[0].endsWith("MISPetitionEmailSend.action")) { + element = document.querySelector("input[name=txtemail_Code]") as HTMLInputElement; + } - if (element) { - const callback = arguments[2]; - arguments[2] = (data: any) => { - element!.value = data?.messageShow || ""; - callback(data); - }; - } + if (element) { + const callback = arguments[2]; + arguments[2] = (data: any) => { + element!.value = data?.messageShow || ""; + callback(data); + }; + } - return proxiedPost.apply(this, Array.from(arguments) as any); -}; + return proxiedPost.apply(this, Array.from(arguments) as any); + }; +}); diff --git a/src/scripts/optimize/home-page.ts b/src/scripts/optimize/home-page.ts index 5ca4c2e..2affea7 100644 --- a/src/scripts/optimize/home-page.ts +++ b/src/scripts/optimize/home-page.ts @@ -1,10 +1,12 @@ +import observeDOM from "@/utils/observe-dom"; + const replace: (any[] | undefined)[] = [ ["#slider > img", { src: "" }], ["#slider > img.nivo-main-image", { src: "" }] ]; let count = 0; -const observer = new MutationObserver(() => { +observeDOM(() => { for (let i = 0; i < replace.length; i++) { if (replace[i]) { const element = document.querySelector(replace[i]![0]) as HTMLElement | null; @@ -20,11 +22,11 @@ const observer = new MutationObserver(() => { replace[i] = undefined; count++; if (count >= replace.length) { - observer.disconnect(); - break; + return true; } } } } + + return false; }); -observer.observe(document, {childList: true, subtree: true}); diff --git a/src/scripts/optimize/index.ts b/src/scripts/optimize/index.ts index 8b63c53..37ae12e 100644 --- a/src/scripts/optimize/index.ts +++ b/src/scripts/optimize/index.ts @@ -1,10 +1,12 @@ +import observeDOM from "@/utils/observe-dom"; + const replace: (any[] | undefined)[] = [ ["head > script:nth-child(34)", { src: "" }], ["head > script:nth-child(43)", { src: "" }] ]; let count = 0; -const observer = new MutationObserver(() => { +observeDOM(() => { for (let i = 0; i < replace.length; i++) { if (replace[i]) { const element = document.querySelector(replace[i]![0]) as HTMLElement | null; @@ -20,11 +22,11 @@ const observer = new MutationObserver(() => { replace[i] = undefined; count++; if (count >= replace.length) { - observer.disconnect(); - break; + return true; } } } } + + return false; }); -observer.observe(document, {childList: true, subtree: true}); diff --git a/src/utils/observe-dom.ts b/src/utils/observe-dom.ts new file mode 100644 index 0000000..e375326 --- /dev/null +++ b/src/utils/observe-dom.ts @@ -0,0 +1,8 @@ +export default (callback: () => boolean) => { + const observer = new MutationObserver(() => { + if (callback()) { + observer.disconnect(); + } + }); + observer.observe(document, {childList: true, subtree: true}); +}; -- cgit v1.2.3