mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-11 05:57:07 +03:00
rebase document script out of videoInfo #1312
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
ToggleSkippable,
|
||||
VideoID,
|
||||
VideoInfo,
|
||||
PageType
|
||||
} from "./types";
|
||||
import Utils from "./utils";
|
||||
import PreviewBar, { PreviewBarSegment } from "./js-components/previewBar";
|
||||
@@ -55,6 +56,10 @@ let activeSkipKeybindElement: ToggleSkippable = null;
|
||||
|
||||
// JSON video info
|
||||
let videoInfo: VideoInfo = null;
|
||||
// Page Type - browse/watch etc...
|
||||
let pageType: PageType;
|
||||
// if video is live or premiere
|
||||
let isLivePremiere: boolean
|
||||
// The channel this video is about
|
||||
let channelIDInfo: ChannelIDInfo;
|
||||
// Locked Categories in this tab, like: ["sponsor","intro","outro"]
|
||||
@@ -90,7 +95,7 @@ let onInvidious: boolean;
|
||||
let onMobileYouTube: boolean;
|
||||
|
||||
//the video id of the last preview bar update
|
||||
let lastPreviewBarUpdate;
|
||||
let lastPreviewBarUpdate: VideoID;
|
||||
|
||||
// Is the video currently being switched
|
||||
let switchingVideos = null;
|
||||
@@ -334,6 +339,7 @@ function resetValues() {
|
||||
sponsorSkipped = [];
|
||||
|
||||
videoInfo = null;
|
||||
pageType = null;
|
||||
channelWhitelisted = false;
|
||||
channelIDInfo = {
|
||||
status: ChannelIDStatus.Fetching,
|
||||
@@ -1157,6 +1163,8 @@ function startSkipScheduleCheckingForStartSponsors() {
|
||||
|
||||
function getYouTubeVideoID(document: Document, url?: string): string | boolean {
|
||||
url ||= document.URL;
|
||||
// pageType shortcut
|
||||
if (pageType === PageType.Channel) return getYouTubeVideoIDFromDocument()
|
||||
// clips should never skip, going from clip to full video has no indications.
|
||||
if (url.includes("youtube.com/clip/")) return false;
|
||||
// skip to document and don't hide if on /embed/
|
||||
@@ -1164,17 +1172,19 @@ function getYouTubeVideoID(document: Document, url?: string): string | boolean {
|
||||
// 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);
|
||||
// skip to document if matches pattern
|
||||
if (url.includes("/channel/") || url.includes("/user/") || url.includes("/c/")) return getYouTubeVideoIDFromDocument();
|
||||
if (url.includes("/channel/") || url.includes("/user/") || url.includes("/c/")) return getYouTubeVideoIDFromDocument(true, PageType.Channel);
|
||||
// not sure, try URL then document
|
||||
return getYouTubeVideoIDFromURL(url) || getYouTubeVideoIDFromDocument(false);
|
||||
}
|
||||
|
||||
function getYouTubeVideoIDFromDocument(hideIcon = true): string | boolean {
|
||||
function getYouTubeVideoIDFromDocument(hideIcon = true, pageHint = PageType.Watch): string | boolean {
|
||||
// get ID from document (channel trailer / embedded playlist)
|
||||
const element = video?.parentElement?.parentElement?.querySelector("a.ytp-title-link[data-sessionlink='feature=player-title']");
|
||||
const videoURL = element?.getAttribute("href");
|
||||
if (videoURL) {
|
||||
onInvidious = hideIcon;
|
||||
// if href found, hint was correct
|
||||
pageType = pageHint;
|
||||
return getYouTubeVideoIDFromURL(videoURL);
|
||||
} else {
|
||||
return false;
|
||||
@@ -1731,7 +1741,7 @@ function updateEditButtonsOnPlayer(): void {
|
||||
// Don't try to update the buttons if we aren't on a YouTube video page
|
||||
if (!sponsorVideoID || onMobileYouTube) return;
|
||||
|
||||
const buttonsEnabled = !Config.config.hideVideoPlayerControls && !onInvidious;
|
||||
const buttonsEnabled = !(Config.config.hideVideoPlayerControls || onInvidious);
|
||||
|
||||
let creatingSegment = false;
|
||||
let submitButtonVisible = false;
|
||||
@@ -2069,7 +2079,7 @@ function submitSponsorTimes() {
|
||||
//called after all the checks have been made that it's okay to do so
|
||||
async function sendSubmitMessage() {
|
||||
// Block if submitting on a running livestream or premiere
|
||||
if (isVisible(document.querySelector(".ytp-live-badge"))) {
|
||||
if (isLivePremiere || isVisible(document.querySelector(".ytp-live-badge"))) {
|
||||
alert(chrome.i18n.getMessage("liveOrPremiere"));
|
||||
return;
|
||||
}
|
||||
@@ -2182,6 +2192,33 @@ function getSegmentsMessage(sponsorTimes: SponsorTime[]): string {
|
||||
return sponsorTimesMessage;
|
||||
}
|
||||
|
||||
function windowListenerHandler(event: MessageEvent): void {
|
||||
const data = event.data;
|
||||
const dataType = data.type
|
||||
if (data.source !== "sponsorblock") return;
|
||||
if (dataType === "navigation") {
|
||||
sponsorVideoID = data.videoID;
|
||||
pageType = data.pageType
|
||||
/* for use with category-specific
|
||||
channelIDInfo = {
|
||||
id: data.channelID,
|
||||
name: data.channelTitle,
|
||||
status: 1
|
||||
}
|
||||
*/
|
||||
} else if (dataType === "ad") {
|
||||
// update isAdPlaying
|
||||
if(isAdPlaying != data.playing) {
|
||||
isAdPlaying = data.playing
|
||||
updatePreviewBar();
|
||||
updateVisibilityOfPlayerControlsButton();
|
||||
}
|
||||
} else if (dataType === "data") {
|
||||
sponsorVideoID = data.videoID;
|
||||
isLivePremiere = data.isLive || data.isPremiere
|
||||
}
|
||||
}
|
||||
|
||||
function updateActiveSegment(currentTime: number): void {
|
||||
previewBar?.updateChapterText(sponsorTimes, sponsorTimesSubmitting, currentTime);
|
||||
chrome.runtime.sendMessage({
|
||||
@@ -2226,8 +2263,13 @@ function addPageListeners(): void {
|
||||
refreshVideoAttachments();
|
||||
}
|
||||
};
|
||||
|
||||
// inject into document
|
||||
const docScript = document.createElement("script");
|
||||
docScript.src = chrome.runtime.getURL("js/document.js");
|
||||
(document.head || document.documentElement).appendChild(docScript);
|
||||
document.addEventListener("yt-navigate-start", resetValues);
|
||||
document.addEventListener("yt-navigate-finish", refreshListners);
|
||||
window.addEventListener("message", windowListenerHandler)
|
||||
}
|
||||
|
||||
function addHotkeyListener(): void {
|
||||
|
||||
Reference in New Issue
Block a user