Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
fix: PIP controls inject timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorWesterlund committed May 2, 2022
1 parent f676e4c commit c18d3a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
37 changes: 15 additions & 22 deletions chrome/assets/js/script.js
Original file line number Diff line number Diff line change
@@ -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/
Expand All @@ -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";
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit c18d3a8

Please sign in to comment.