diff options
author | Ajay <[email protected]> | 2022-01-05 15:13:42 -0500 |
---|---|---|
committer | Ajay <[email protected]> | 2022-01-05 15:13:42 -0500 |
commit | 040bce263855a1ff68cecb694f8da903e3df55ec (patch) | |
tree | 015fa75fabe0411dcca4a5352fccee03afd68095 /src/render | |
parent | 388b9179ac788812546c5021f367ede55d4917fa (diff) | |
download | SponsorBlock-040bce263855a1ff68cecb694f8da903e3df55ec.tar.gz SponsorBlock-040bce263855a1ff68cecb694f8da903e3df55ec.zip |
Make category pill work on invidious and mobile youtube
Diffstat (limited to 'src/render')
-rw-r--r-- | src/render/CategoryPill.tsx | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/render/CategoryPill.tsx b/src/render/CategoryPill.tsx index 6c6695e2..4fe846d0 100644 --- a/src/render/CategoryPill.tsx +++ b/src/render/CategoryPill.tsx @@ -9,15 +9,19 @@ export class CategoryPill { ref: React.RefObject<CategoryPillComponent>; unsavedState: CategoryPillState; + + mutationObserver?: MutationObserver; constructor() { this.ref = React.createRef(); } - async attachToPage(): Promise<void> { - // TODO: Mobile and invidious - const referenceNode = await GenericUtils.wait(() => document.querySelector(".ytd-video-primary-info-renderer.title") as HTMLElement); - + async attachToPage(onMobileYouTube: boolean): Promise<void> { + const referenceNode = + await GenericUtils.wait(() => + // YouTube, Mobile YouTube, Invidious + document.querySelector(".ytd-video-primary-info-renderer.title, .slim-video-information-title, #player-container + .h-box > h1") as HTMLElement); + if (referenceNode && !referenceNode.contains(this.container)) { this.container = document.createElement('span'); this.container.id = "categoryPill"; @@ -26,6 +30,10 @@ export class CategoryPill { referenceNode.prepend(this.container); referenceNode.style.display = "flex"; + if (this.ref.current) { + this.unsavedState = this.ref.current.state; + } + ReactDOM.render( <CategoryPillComponent ref={this.ref} />, this.container @@ -35,6 +43,19 @@ export class CategoryPill { this.ref.current?.setState(this.unsavedState); this.unsavedState = null; } + + if (onMobileYouTube) { + if (this.mutationObserver) { + this.mutationObserver.disconnect(); + } + + this.mutationObserver = new MutationObserver(() => this.attachToPage(onMobileYouTube)); + + this.mutationObserver.observe(referenceNode, { + childList: true, + subtree: true + }); + } } } |