mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-12 22:47:18 +03:00
Fix wait for element not working on embed, causing segments not to load
Fix #1497
This commit is contained in:
@@ -130,8 +130,9 @@ const playerButtons: Record<string, {button: HTMLButtonElement, image: HTMLImage
|
|||||||
// Direct Links after the config is loaded
|
// Direct Links after the config is loaded
|
||||||
utils.wait(() => Config.config !== null, 1000, 1).then(() => videoIDChange(getYouTubeVideoID(document)));
|
utils.wait(() => Config.config !== null, 1000, 1).then(() => videoIDChange(getYouTubeVideoID(document)));
|
||||||
// wait for hover preview to appear, and refresh attachments if ever found
|
// wait for hover preview to appear, and refresh attachments if ever found
|
||||||
utils.waitForElement(".ytp-inline-preview-ui").then(() => refreshVideoAttachments())
|
utils.waitForElement(".ytp-inline-preview-ui").then(() => refreshVideoAttachments());
|
||||||
utils.waitForElement("a.ytp-title-link[data-sessionlink='feature=player-title']").then(() => videoIDChange(getYouTubeVideoID(document)).then())
|
utils.waitForElement("a.ytp-title-link[data-sessionlink='feature=player-title']")
|
||||||
|
.then(() => videoIDChange(getYouTubeVideoID(document)));
|
||||||
addPageListeners();
|
addPageListeners();
|
||||||
addHotkeyListener();
|
addHotkeyListener();
|
||||||
|
|
||||||
@@ -1223,7 +1224,7 @@ function getYouTubeVideoID(document: Document, url?: string): string | boolean {
|
|||||||
// clips should never skip, going from clip to full video has no indications.
|
// clips should never skip, going from clip to full video has no indications.
|
||||||
if (url.includes("youtube.com/clip/")) return false;
|
if (url.includes("youtube.com/clip/")) return false;
|
||||||
// skip to document and don't hide if on /embed/
|
// skip to document and don't hide if on /embed/
|
||||||
if (url.includes("/embed/") && url.includes("youtube.com")) return getYouTubeVideoIDFromDocument(false);
|
if (url.includes("/embed/") && url.includes("youtube.com")) return getYouTubeVideoIDFromDocument(false, PageType.Embed);
|
||||||
// skip to URL if matches youtube watch or invidious or matches youtube pattern
|
// skip to URL if matches youtube watch or invidious or matches youtube pattern
|
||||||
if ((!url.includes("youtube.com")) || url.includes("/watch") || url.includes("/shorts/") || url.includes("playlist")) return getYouTubeVideoIDFromURL(url);
|
if ((!url.includes("youtube.com")) || url.includes("/watch") || url.includes("/shorts/") || url.includes("playlist")) return getYouTubeVideoIDFromURL(url);
|
||||||
// skip to document if matches pattern
|
// skip to document if matches pattern
|
||||||
@@ -1233,8 +1234,10 @@ function getYouTubeVideoID(document: Document, url?: string): string | boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getYouTubeVideoIDFromDocument(hideIcon = true, pageHint = PageType.Watch): string | boolean {
|
function getYouTubeVideoIDFromDocument(hideIcon = true, pageHint = PageType.Watch): string | boolean {
|
||||||
|
const selector = "a.ytp-title-link[data-sessionlink='feature=player-title']";
|
||||||
// get ID from document (channel trailer / embedded playlist)
|
// get ID from document (channel trailer / embedded playlist)
|
||||||
const element = video?.parentElement?.parentElement?.querySelector("a.ytp-title-link[data-sessionlink='feature=player-title']");
|
const element = pageHint === PageType.Embed ? document.querySelector(selector)
|
||||||
|
: video?.parentElement?.parentElement?.querySelector(selector);
|
||||||
const videoURL = element?.getAttribute("href");
|
const videoURL = element?.getAttribute("href");
|
||||||
if (videoURL) {
|
if (videoURL) {
|
||||||
onInvidious = hideIcon;
|
onInvidious = hideIcon;
|
||||||
|
|||||||
@@ -244,7 +244,8 @@ export enum PageType {
|
|||||||
Watch = "watch",
|
Watch = "watch",
|
||||||
Search = "search",
|
Search = "search",
|
||||||
Browse = "browse",
|
Browse = "browse",
|
||||||
Channel = "channel"
|
Channel = "channel",
|
||||||
|
Embed = "embed"
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ButtonListener {
|
export interface ButtonListener {
|
||||||
|
|||||||
21
src/utils.ts
21
src/utils.ts
@@ -65,7 +65,7 @@ export default class Utils {
|
|||||||
|
|
||||||
private setupWaitingMutationListener(): void {
|
private setupWaitingMutationListener(): void {
|
||||||
if (!this.waitingMutationObserver) {
|
if (!this.waitingMutationObserver) {
|
||||||
this.waitingMutationObserver = new MutationObserver(() => {
|
const checkForObjects = () => {
|
||||||
const foundSelectors = [];
|
const foundSelectors = [];
|
||||||
for (const { selector, visibleCheck, callback } of this.waitingElements) {
|
for (const { selector, visibleCheck, callback } of this.waitingElements) {
|
||||||
const element = this.getElement(selector, visibleCheck);
|
const element = this.getElement(selector, visibleCheck);
|
||||||
@@ -78,16 +78,23 @@ export default class Utils {
|
|||||||
this.waitingElements = this.waitingElements.filter((element) => !foundSelectors.includes(element.selector));
|
this.waitingElements = this.waitingElements.filter((element) => !foundSelectors.includes(element.selector));
|
||||||
|
|
||||||
if (this.waitingElements.length === 0) {
|
if (this.waitingElements.length === 0) {
|
||||||
this.waitingMutationObserver.disconnect();
|
this.waitingMutationObserver?.disconnect();
|
||||||
this.waitingMutationObserver = null;
|
this.waitingMutationObserver = null;
|
||||||
this.creatingWaitingMutationObserver = false;
|
this.creatingWaitingMutationObserver = false;
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
this.waitingMutationObserver.observe(document.body, {
|
// Do an initial check over all objects
|
||||||
childList: true,
|
checkForObjects();
|
||||||
subtree: true
|
|
||||||
});
|
if (this.waitingElements.length > 0) {
|
||||||
|
this.waitingMutationObserver = new MutationObserver(checkForObjects);
|
||||||
|
|
||||||
|
this.waitingMutationObserver.observe(document.body, {
|
||||||
|
childList: true,
|
||||||
|
subtree: true
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user