fix: PIP controls inject timeout

This commit is contained in:
Victor Westerlund 2022-05-02 12:07:06 +02:00
parent f676e4cb70
commit c18d3a86dc
2 changed files with 16 additions and 23 deletions

View file

@ -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) {

View file

@ -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"