Picture in Picture is an amazing API, it let's you keep on working in another tab but have a little playable live thumbnail of the video on the screen for you to keep up with. Yet there are a number of sites that disable the feature.
I just wanted a quick and easy access button that I can press in my browser that will just PIP the video. So I made a bookmarklet.
Just drag the link to you Bookmark bar and when you press it, the currently playing video will be put in to Picture in Picture mode.
If it tickles your fancy, the code is below.
// dont define a variable on scope. Only pip the first video [ document, ...[...document.querySelectorAll("iframe")] .map(iframe => iframe.contentDocument) .filter(iframe => !!iframe) ].some(d => [...d.querySelectorAll("video")] .filter(video => video.paused == false && video.ended == false) .some( video => !!video.requestPictureInPicture().catch(err => console.log(err)) ) );
Why one line? Well, Bookmarklets run their code in the local scope, and I worry about adding new variables on to the global object that might clash with others on the current page.
The above code will find the first video that is playing in either the document or an iframe that is on the same origin.
First we build a list of valid documents, then for each document (using
some) we check to see if there are any videos that are currently playing and then using
The use of
some is interesting because it means that we don't need to iterate over each video in each document, instead the first document to contain a playing video will be Picture in Pictured.
That was a fun 30 minutes :D
I lead the Chrome Developer Relations team at Google.
We want people to have the best experience possible on the web without having to install a native app or produce content in a walled garden.
Our team tries to make it easier for developers to build on the web by supporting every Chrome release, creating great content to support developers on web.dev, contributing to MDN, helping to improve browser compatibility, and some of the best developer tools like Lighthouse, Workbox, Squoosh to name just a few.