Skip to content

Instantly share code, notes, and snippets.

@iamandrewluca
Last active April 25, 2024 13:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iamandrewluca/71b57cb2d905ab06429c481251db9833 to your computer and use it in GitHub Desktop.
Save iamandrewluca/71b57cb2d905ab06429c481251db9833 to your computer and use it in GitHub Desktop.
Request picture in picture on first found video that is playing #bookmarklet
javascript: void ((function() {
/* More bookmarklets at https://gist.github.com/iamandrewluca/61feacf07bc4f2f50e70f986c2e9b2d2 */
/** @type {NodeListOf<HTMLIFrameElement>} */
const iFrames = window.document.querySelectorAll('iframe');
console.log(`Found ${iFrames.length} iframes`);
/** @type {Document[]} */
const allDocuments = [
window.document,
...Array.from(iFrames)
.map(i => i.contentDocument)
/**
* some iFrames have `null` contentDocument
* one reason for this is because of not Same-Origin policy
*/
.filter(Boolean)
];
console.log(`Found ${allDocuments.length} documents`);
/** @type {HTMLVideoElement[]} */
const allVideos = allDocuments.flatMap(d => Array.from(d.querySelectorAll('video')));
console.log(`Found ${allVideos.length} videos`);
/** @type {HTMLVideoElement} */
const playingVideo = allVideos.find(v => !v.paused);
if (playingVideo) {
console.log(`Found playing video`);
playingVideo.requestPictureInPicture();
}
})());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment