From a30b28d40e5bacdbbbcad3c9de8f1fe3e083119c Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Tue, 14 Feb 2023 09:46:34 +0530 Subject: Added ability to load static assets from extension's assets. --- src/scripts/optimize/home-page.ts | 35 +++++------------------------------ src/scripts/optimize/index.ts | 37 ++++++------------------------------- src/utils/observe-dom.ts | 2 +- src/utils/replace-attributes.ts | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 62 deletions(-) create mode 100644 src/utils/replace-attributes.ts (limited to 'src') diff --git a/src/scripts/optimize/home-page.ts b/src/scripts/optimize/home-page.ts index 2affea7..4c1f21f 100644 --- a/src/scripts/optimize/home-page.ts +++ b/src/scripts/optimize/home-page.ts @@ -1,32 +1,7 @@ -import observeDOM from "@/utils/observe-dom"; +import { replaceAttributes } from "@/utils/replace-attributes"; -const replace: (any[] | undefined)[] = [ - ["#slider > img", { src: "" }], - ["#slider > img.nivo-main-image", { src: "" }] -]; -let count = 0; +const bannerImageUrl = chrome.runtime.getURL("assets/banner.jpg"); -observeDOM(() => { - for (let i = 0; i < replace.length; i++) { - if (replace[i]) { - const element = document.querySelector(replace[i]![0]) as HTMLElement | null; - if (element) { - const attributes = replace[i]![1]; - if (attributes.innerHTML) { - element.innerHTML = attributes.innerHTML; - } - delete attributes.innerHTML; - for (const attribute in attributes) { - element.setAttribute(attribute, attributes[attribute]); - } - replace[i] = undefined; - count++; - if (count >= replace.length) { - return true; - } - } - } - } - - return false; -}); +replaceAttributes([ + ["#form_MainMenuPage > div > div:nth-child(13)", { innerHTML: `` }] +]); diff --git a/src/scripts/optimize/index.ts b/src/scripts/optimize/index.ts index 37ae12e..70cbac3 100644 --- a/src/scripts/optimize/index.ts +++ b/src/scripts/optimize/index.ts @@ -1,32 +1,7 @@ -import observeDOM from "@/utils/observe-dom"; +import { replaceAttributes } from "@/utils/replace-attributes"; -const replace: (any[] | undefined)[] = [ - ["head > script:nth-child(34)", { src: "" }], - ["head > script:nth-child(43)", { src: "" }] -]; -let count = 0; - -observeDOM(() => { - for (let i = 0; i < replace.length; i++) { - if (replace[i]) { - const element = document.querySelector(replace[i]![0]) as HTMLElement | null; - if (element) { - const attributes = replace[i]![1]; - if (attributes.innerHTML) { - element.innerHTML = attributes.innerHTML; - } - delete attributes.innerHTML; - for (const attribute in attributes) { - element.setAttribute(attribute, attributes[attribute]); - } - replace[i] = undefined; - count++; - if (count >= replace.length) { - return true; - } - } - } - } - - return false; -}); +replaceAttributes([ + ["head > script:nth-child(34)", { src: null }], + ["head > script:nth-child(36)", { src: null, innerHTML: "$.prototype.nivoSlider = () => {};" }], + ["head > script:nth-child(43)", { src: null }] +]); diff --git a/src/utils/observe-dom.ts b/src/utils/observe-dom.ts index e375326..30a07b4 100644 --- a/src/utils/observe-dom.ts +++ b/src/utils/observe-dom.ts @@ -1,4 +1,4 @@ -export default (callback: () => boolean) => { +export const observeDOM = (callback: () => boolean) => { const observer = new MutationObserver(() => { if (callback()) { observer.disconnect(); diff --git a/src/utils/replace-attributes.ts b/src/utils/replace-attributes.ts new file mode 100644 index 0000000..443c1d0 --- /dev/null +++ b/src/utils/replace-attributes.ts @@ -0,0 +1,34 @@ +import { observeDOM } from "@/utils/observe-dom"; + +export const replaceAttributes = (elements: any[]) => { + let count = 0; + + observeDOM(() => { + for (let i = 0; i < elements.length; i++) { + if (elements[i]) { + const element = document.querySelector(elements[i]![0]) as HTMLElement | null; + if (element) { + const attributes = elements[i]![1]; + if (attributes.innerHTML) { + element.innerHTML = attributes.innerHTML; + } + delete attributes.innerHTML; + for (const attribute in attributes) { + if (attributes[attribute] === null) { + element.removeAttribute(attribute); + } else { + element.setAttribute(attribute, attributes[attribute]); + } + } + elements[i] = undefined; + count++; + if (count >= elements.length) { + return true; + } + } + } + } + + return false; + }); +}; -- cgit v1.2.3