Skip to content

Instantly share code, notes, and snippets.

@vojtechjurasek
Last active March 8, 2019 19:56
Show Gist options
  • Save vojtechjurasek/9414cd539fef69a3bb4c9080621a3bb3 to your computer and use it in GitHub Desktop.
Save vojtechjurasek/9414cd539fef69a3bb4c9080621a3bb3 to your computer and use it in GitHub Desktop.
Request Picture-in-Picture in Google Chrome bookmark
/* Check if pip feature is present in browser */
if (
'pictureInPictureEnabled' in document &&
'querySelectorAll' in document
) {
async function pip() {
/* Select all video elements */
const videos = document.querySelectorAll('video');
if (videos.length === 0) {
window.alert('Sorry, no videos on the page.');
} else if (videos.length > 0) {
/* I assume main video in the document is the first one */
const firstVideo = videos[0];
try {
if (firstVideo !== document.pictureInPictureElement) {
/* Request Picture-in-Picture */
await firstVideo.requestPictureInPicture();
} else {
/* Exit Picture-in-Picture */
await document.exitPictureInPicture();
}
} catch (error) {
console.error(error);
}
}
}
/* Call async pip function */
pip();
} else if (!('pictureInPictureEnabled' in document)) {
window.alert('Picture-in-Picture is disabled.');
} else if (!document.pictureInPictureEnabled) {
window.alert('Picture-in-Picture not available.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment