From 3b59f691cf71cbdc069b82f02dffaebc20affec1 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Sat, 15 Jan 2022 14:16:52 +0100 Subject: [PATCH] userScript: add preview mute functionality --- src/config.js | 3 ++- src/ui.js | 9 +++++++++ src/userScript.js | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index a93ffe28..a7493b7c 100644 --- a/src/config.js +++ b/src/config.js @@ -7,7 +7,8 @@ const defaultConfig = { enableSponsorBlockOutro: true, enableSponsorBlockInteraction: true, enableSponsorBlockSelfPromo: true, - enableSponsorBlockMusicOfftopic: true + enableSponsorBlockMusicOfftopic: true, + enablePreviewMute: false }; let localConfig; diff --git a/src/ui.js b/src/ui.js index 4bce127e..ece1c195 100644 --- a/src/ui.js +++ b/src/ui.js @@ -48,6 +48,7 @@ uiContainer.addEventListener( uiContainer.innerHTML = `

webOS YouTube Extended

+
@@ -67,6 +68,14 @@ uiContainer.querySelector('#__adblock').addEventListener('change', (evt) => { configWrite('enableAdBlock', evt.target.checked); }); +uiContainer.querySelector('#__previewMute').checked = + configRead('enablepreviewMute'); +uiContainer + .querySelector('#__previewMute') + .addEventListener('change', (evt) => { + configWrite('enablePreviewMute', evt.target.checked); + }); + uiContainer.querySelector('#__sponsorblock').checked = configRead('enableSponsorBlock'); uiContainer diff --git a/src/userScript.js b/src/userScript.js index 0ee2da5f..624ce1ba 100644 --- a/src/userScript.js +++ b/src/userScript.js @@ -2,6 +2,7 @@ import 'whatwg-fetch'; import './domrect-polyfill'; import { handleLaunch, waitForChildAdd } from './utils'; +import { configRead } from './config'; document.addEventListener( 'webOSRelaunch', @@ -24,6 +25,19 @@ import './ui.js'; (node) => node instanceof HTMLVideoElement ); + const applyMuteOverride = (evt) => { + const rect = evt.target.getBoundingClientRect(); + const enablePreviewMute = configRead('enablePreviewMute'); + const targetMute = enablePreviewMute && rect.left !== 0; + if (targetMute !== evt.target.muted) { + console.info('Setting mute override to:', targetMute); + evt.target.muted = targetMute; + } + }; + + video.addEventListener('play', (evt) => applyMuteOverride(evt)); + video.addEventListener('volumechange', (evt) => applyMuteOverride(evt)); + const playerCtrlObs = new MutationObserver(() => { const style = video.style;