From c18d3a86dc34ab7594e1b0f5274c01d22004b25f Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Mon, 2 May 2022 12:07:06 +0200 Subject: [PATCH] fix: PIP controls inject timeout --- chrome/assets/js/script.js | 37 +++++++++++++++---------------------- chrome/manifest.json | 2 +- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/chrome/assets/js/script.js b/chrome/assets/js/script.js index d41189c..a3be3b3 100644 --- a/chrome/assets/js/script.js +++ b/chrome/assets/js/script.js @@ -1,35 +1,18 @@ class PIPController { constructor() { this.element = null; - - const config = { - childList: true, - subtree: true - } - - const mutationTimeout = 500; - let mutating = null; - let prevHref = null; // SPA navigation handler this.observer = new MutationObserver(() => { if (this.isPlayerPage()) { - clearTimeout(mutating); - - // Wait for mutation to stop - mutating = setTimeout(() => { - // Attempt to inject PIP controls - if (prevHref !== window.location.href) { - this.injectPipControls(); - prevHref = window.location.href; - } - }, mutationTimeout); - } else { - prevHref = window.location.href; + this.injectPipControls(); } }); - this.observer.observe(window.document.body, config); + this.observer.observe(window.document.body, { + childList: true, + subtree: true + }); } // Returns true if the page is /video/ @@ -56,6 +39,7 @@ class PIPController { const type = "button"; const pip = document.createElement(type); + pip.id = "pip-btn"; pip.type = type; pip.role = type; pip.tabindex = "0"; @@ -68,8 +52,17 @@ class PIPController { return pip; } + // PIP element has been created and injected into the DOM + hasPipControls() { + return document.getElementById("pip-btn") ? true : false; + } + // Insert the PIP toggle button and enable PIP on the video element injectPipControls() { + if (this.hasPipControls()) { + return true; + } + this.element = document.getElementsByTagName("video")[0]; if (!this.isPlayerPage() || !this.element) { diff --git a/chrome/manifest.json b/chrome/manifest.json index 7823956..9436653 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -1,7 +1,7 @@ { "name": "Disney+ PIP", "description": "Adds Picture-in-Picture (PIP) support for Disney+", - "version": "1.0", + "version": "1.1", "manifest_version": 3, "permissions": [ "activeTab"