lint&fix video-toggle plugin

This commit is contained in:
Araxeus
2021-11-09 15:17:26 +02:00
parent eafdd5046d
commit 72b4398024

View File

@ -20,7 +20,7 @@ module.exports = (_options) => {
function setup() { function setup() {
$('ytmusic-player-page').prepend(switchButtonDiv); $('ytmusic-player-page').prepend(switchButtonDiv);
$('#song-image.ytmusic-player').style.display = "block" $('#song-image.ytmusic-player').style.display = "block";
if (options.hideVideo) { if (options.hideVideo) {
$('.video-switch-button-checkbox').checked = false; $('.video-switch-button-checkbox').checked = false;
@ -39,29 +39,35 @@ function setup() {
} }
function changeDisplay(showVideo) { function changeDisplay(showVideo) {
if (!showVideo && $('ytmusic-player').getAttribute('playback-mode') !== "ATV_PREFERRED") { if (!showVideo) {
$('video').style.top = "0"; $('video').style.top = "0";
$('ytmusic-player').style.margin = "auto 21.5px"; $('ytmusic-player').style.margin = "auto 0px";
$('ytmusic-player').setAttribute('playback-mode', "ATV_PREFERRED"); $('ytmusic-player').setAttribute('playback-mode', "ATV_PREFERRED");
}
showVideo ?
$('#song-video.ytmusic-player').style.display = "unset" :
$('#song-video.ytmusic-player').style.display = "none"; $('#song-video.ytmusic-player').style.display = "none";
} else {
$('#song-video.ytmusic-player').style.display = "unset";
// fix black video
$('video').pause(); $('video').play();
}
} }
function videoStarted() { function videoStarted() {
if (videoExist()) { if (videoExist()) {
// switch to high res thumbnail
const thumbnails = $('#movie_player').getPlayerResponse()?.videoDetails?.thumbnail?.thumbnails; const thumbnails = $('#movie_player').getPlayerResponse()?.videoDetails?.thumbnail?.thumbnails;
if (thumbnails && thumbnails.length > 0) { if (thumbnails && thumbnails.length > 0) {
$('#song-image img').src = thumbnails[thumbnails.length-1].url; $('#song-image img').src = thumbnails[thumbnails.length - 1].url;
} }
// show toggle button
switchButtonDiv.style.display = "initial"; switchButtonDiv.style.display = "initial";
// change display to video mode if video exist & video is hidden & option.hideVideo = false
if (!options.hideVideo && $('#song-video.ytmusic-player').style.display === "none") { if (!options.hideVideo && $('#song-video.ytmusic-player').style.display === "none") {
changeDisplay(true); changeDisplay(true);
} }
} else { } else {
// video doesn't exist -> switch to song mode
changeDisplay(false); changeDisplay(false);
// hide toggle button
switchButtonDiv.style.display = "none"; switchButtonDiv.style.display = "none";
} }
} }
@ -75,11 +81,11 @@ function videoExist() {
function forcePlaybackMode() { function forcePlaybackMode() {
const playbackModeObserver = new MutationObserver(mutations => { const playbackModeObserver = new MutationObserver(mutations => {
mutations.forEach(mutation => { mutations.forEach(mutation => {
if (mutation.type === 'attributes' && mutation.attributeName === 'playback-mode' && mutation.target.getAttribute('playback-mode') !== "ATV_PREFERRED") { if (mutation.target.getAttribute('playback-mode') !== "ATV_PREFERRED") {
playbackModeObserver.disconnect(); playbackModeObserver.disconnect();
mutation.target.setAttribute('playback-mode', "ATV_PREFERRED"); mutation.target.setAttribute('playback-mode', "ATV_PREFERRED");
} }
}); });
}); });
playbackModeObserver.observe($('ytmusic-player'), { attributeFilter: ["playback-mode"] }) playbackModeObserver.observe($('ytmusic-player'), { attributeFilter: ["playback-mode"] });
} }