aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils.ts
diff options
context:
space:
mode:
authorAjay <[email protected]>2023-02-13 02:31:25 -0500
committerAjay <[email protected]>2023-02-13 02:31:25 -0500
commit5859c33ce89ef142e44b1acebfacb964919073ff (patch)
tree1713c1c1af7b3781d106daa5eda380045848196b /src/utils.ts
parentf4d80d88438ff31bc067ff480fd94112d972718d (diff)
downloadSponsorBlock-5859c33ce89ef142e44b1acebfacb964919073ff.tar.gz
SponsorBlock-5859c33ce89ef142e44b1acebfacb964919073ff.zip
Move video handing and config to shared library
Diffstat (limited to 'src/utils.ts')
-rw-r--r--src/utils.ts75
1 files changed, 1 insertions, 74 deletions
diff --git a/src/utils.ts b/src/utils.ts
index 8d8b4b16..1cdbf73f 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -2,9 +2,9 @@ import Config, { VideoDownvotes } from "./config";
import { CategorySelection, SponsorTime, FetchResponse, BackgroundScriptContainer, Registration, HashedValue, VideoID, SponsorHideType } from "./types";
import * as CompileConfig from "../config.json";
-import { findValidElement, findValidElementFromSelector } from "./utils/pageUtils";
import { waitFor } from "@ajayyy/maze-utils";
import { isSafari } from "./utils/configUtils";
+import { findValidElementFromSelector } from "@ajayyy/maze-utils/lib/dom";
export default class Utils {
@@ -23,11 +23,6 @@ export default class Utils {
"shared.css"
];
- /* Used for waitForElement */
- creatingWaitingMutationObserver = false;
- waitingMutationObserver: MutationObserver = null;
- waitingElements: { selector: string; visibleCheck: boolean; callback: (element: Element) => void }[] = [];
-
constructor(backgroundScriptContainer: BackgroundScriptContainer = null) {
this.backgroundScriptContainer = backgroundScriptContainer;
}
@@ -36,74 +31,6 @@ export default class Utils {
return waitFor(condition, timeout, check);
}
- /* Uses a mutation observer to wait asynchronously */
- async waitForElement(selector: string, visibleCheck = false): Promise<Element> {
- return await new Promise((resolve) => {
- const initialElement = this.getElement(selector, visibleCheck);
- if (initialElement) {
- resolve(initialElement);
- return;
- }
-
- this.waitingElements.push({
- selector,
- visibleCheck,
- callback: resolve
- });
-
- if (!this.creatingWaitingMutationObserver) {
- this.creatingWaitingMutationObserver = true;
-
- if (document.body) {
- this.setupWaitingMutationListener();
- } else {
- window.addEventListener("DOMContentLoaded", () => {
- this.setupWaitingMutationListener();
- });
- }
- }
- });
- }
-
- private setupWaitingMutationListener(): void {
- if (!this.waitingMutationObserver) {
- const checkForObjects = () => {
- const foundSelectors = [];
- for (const { selector, visibleCheck, callback } of this.waitingElements) {
- const element = this.getElement(selector, visibleCheck);
- if (element) {
- callback(element);
- foundSelectors.push(selector);
- }
- }
-
- this.waitingElements = this.waitingElements.filter((element) => !foundSelectors.includes(element.selector));
-
- if (this.waitingElements.length === 0) {
- this.waitingMutationObserver?.disconnect();
- this.waitingMutationObserver = null;
- this.creatingWaitingMutationObserver = false;
- }
- };
-
- // Do an initial check over all objects
- checkForObjects();
-
- if (this.waitingElements.length > 0) {
- this.waitingMutationObserver = new MutationObserver(checkForObjects);
-
- this.waitingMutationObserver.observe(document.body, {
- childList: true,
- subtree: true
- });
- }
- }
- }
-
- private getElement(selector: string, visibleCheck: boolean) {
- return visibleCheck ? findValidElement(document.querySelectorAll(selector)) : document.querySelector(selector);
- }
-
containsPermission(permissions: chrome.permissions.Permissions): Promise<boolean> {
return new Promise((resolve) => {
chrome.permissions.contains(permissions, resolve)