From 95c57843cf920f0f4ef90451dbd9c4e0f1acf5d2 Mon Sep 17 00:00:00 2001 From: HighImKevin Date: Fri, 25 Sep 2026 05:05:52 -0400 Subject: [PATCH] Fix situations where Watch Together button might not appear in Video OSD --- scripts/watchTogether.js | 45 +++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/scripts/watchTogether.js b/scripts/watchTogether.js index 4295f95..45d2289 100644 --- a/scripts/watchTogether.js +++ b/scripts/watchTogether.js @@ -33,6 +33,8 @@ let inContinuousVideoSession = false; let isAddingOsdButton = false; + let osdButtonObserver = null; + let osdButtonObserverTimeout = null; function getUi() { return window.KefinTweaksUI || {}; @@ -1290,10 +1292,9 @@ // If the WatchTogether button doesn't exist, show the toast as we can assume it's the start of a new session if (!watchTogetherOsdButtonExists()) { showWatchTogetherSessionToast(); - return; } - addWatchTogetherOsdButton(); + ensureWatchTogetherOsdButton(); if (!shouldPromptWatchTogether()) { updateSyncListenerState(); @@ -1302,18 +1303,53 @@ stampLastSelectionAt(getEnabledSessionUsers().map((u) => u.userId)); updateSyncListenerState(); - addWatchTogetherOsdButton(); inContinuousVideoSession = true; } function handleVideoPageLeave() { inContinuousVideoSession = false; + disconnectOsdButtonObserver(); } function watchTogetherOsdButtonExists() { return !!document.querySelector('.skinBody .skinHeader button.btnWatchTogether'); } + function disconnectOsdButtonObserver() { + if (osdButtonObserver) { + osdButtonObserver.disconnect(); + osdButtonObserver = null; + } + if (osdButtonObserverTimeout != null) { + clearTimeout(osdButtonObserverTimeout); + osdButtonObserverTimeout = null; + } + } + + function ensureWatchTogetherOsdButton() { + addWatchTogetherOsdButton(); + if (watchTogetherOsdButtonExists()) { + disconnectOsdButtonObserver(); + return; + } + if (osdButtonObserver) return; + + const root = document.getElementById('reactRoot') || document.body; + if (!root) return; + + osdButtonObserver = new MutationObserver(() => { + if (!document.querySelector('.skinHeader.osdHeader')) return; + addWatchTogetherOsdButton(); + if (watchTogetherOsdButtonExists()) { + disconnectOsdButtonObserver(); + } + }); + osdButtonObserver.observe(root, { childList: true, subtree: true }); + osdButtonObserverTimeout = setTimeout(() => { + disconnectOsdButtonObserver(); + }, 30000); + } + function createWatchTogetherOsdButton(anchor = null) { const button = document.createElement('button'); button.type = 'button'; @@ -1403,8 +1439,7 @@ if (onVideo && !previousOnVideo) { handleVideoPageEnter(); - addWatchTogetherOsdButton(); - } else if (inContinuousVideoSession) { + } else if (!onVideo && (previousOnVideo || inContinuousVideoSession)) { handleVideoPageLeave(); } }, {