diff --git a/background.js b/background.js index 4421fa3d3..7622d2b10 100644 --- a/background.js +++ b/background.js @@ -209,10 +209,12 @@ chrome.windows.onFocusChanged.addListener(function (wId) { windowId = wId; //console.log('onFocusChanged', windowId, tabPrev, tab); tabPrune(function () { - if (windowId != tab.windowId && tab.tabId && tabConnected[tab.tabId]) { + if (windowId != tab.windowId && tab.tabId && !tab.blur && tabConnected[tab.tabId]) { + tab.blur = true; chrome.tabs.sendMessage(tab.tabId, {action: 'blur'}); //console.log('blur', tab.tabId, windowId); - } else if (windowId && tab.tabId && tabConnected[tab.tabId]) { + } else if (windowId == tab.windowId && tab.tabId && tab.blur && tabConnected[tab.tabId]) { + tab.blur = false; chrome.tabs.sendMessage(tab.tabId, {action: 'focus'}); //console.log('focus', tab.tabId, windowId); } diff --git a/js&css/web-accessible/www.youtube.com/player.js b/js&css/web-accessible/www.youtube.com/player.js index f1bf06f83..f3e5df7e7 100644 --- a/js&css/web-accessible/www.youtube.com/player.js +++ b/js&css/web-accessible/www.youtube.com/player.js @@ -42,15 +42,16 @@ ImprovedTube.forcedPlayVideoFromTheBeginning = function () { AUTOPAUSE WHEN SWITCHING TABS ------------------------------------------------------------------------------*/ ImprovedTube.playerAutopauseWhenSwitchingTabs = function () { - var player = ImprovedTube.elements.player; - - if (this.storage.player_autopause_when_switching_tabs === true && player) { - if (this.focus === false) { - this.played_before_blur = player.getPlayerState() === 1; + const player = this.elements.player; - player.pauseVideo(); - } else if (this.focus === true && this.played_before_blur === true) { + if (this.storage.player_autopause_when_switching_tabs && player) { + if (this.focus && this.played_before_blur && this.elements.video.paused) { player.playVideo(); + } else { + this.played_before_blur = !this.elements.video.paused; + if (!this.elements.video.paused) { + player.pauseVideo(); + } } } };