add mediaSession, remove bindMediaKey

This commit is contained in:
Michael C
2022-01-27 14:52:13 -05:00
parent 22fcea99f5
commit ba7d46dd73
6 changed files with 23 additions and 31 deletions

View File

@@ -967,7 +967,8 @@ function getYouTubeVideoIDFromURL(url: string): string | boolean {
return id.length == 11 ? id : false;
} else if (urlObject.pathname.startsWith("/embed/") || urlObject.pathname.startsWith("/shorts/")) {
try {
return urlObject.pathname.split("/")[2].slice(0, 11);
const id = urlObject.pathname.split("/")[2]
if (id?.length >=11 ) return id.slice(0, 11);
} catch (e) {
console.error("[SB] Video ID not valid for " + url);
return false;
@@ -1272,9 +1273,14 @@ function skipToTime({v, skipTime, skippingSegments, openNotice, forceAutoSkip, u
if (autoSkip && Config.config.audioNotificationOnSkip) {
const beep = new Audio(chrome.runtime.getURL("icons/beep.ogg"));
beep.volume = video.volume * 0.1;
const oldMetadata = navigator.mediaSession.metadata
beep.play();
if (Config.config.audioNotificationOnSkip && Config.config.bindMediaKey)
window.addEventListener("keydown", mediaPauseListener);
beep.addEventListener("ended", () => {
navigator.mediaSession.metadata = null
setTimeout(() =>
navigator.mediaSession.metadata = oldMetadata
)
})
}
if (!autoSkip
@@ -1985,7 +1991,6 @@ function sendRequestToCustomServer(type, fullAddress, callback) {
function updateAdFlag(): void {
const wasAdPlaying = isAdPlaying;
isAdPlaying = document.getElementsByClassName('ad-showing').length > 0;
if(wasAdPlaying != isAdPlaying) {
updatePreviewBar();
updateVisibilityOfPlayerControlsButton();
@@ -2039,9 +2044,4 @@ function checkForPreloadedSegment() {
}
}
}
}
function mediaPauseListener(e: KeyboardEvent) {
if (e.key === "MediaPlayPause")
video?.paused ? video.play() : video.pause();
}