eliseomartelli
HomeBlogPhotosAbout

YouTube's PiP but with ⌥+P

Apr 27, 2023 - ⏱️ 1 minute to read

I was watching a video on YouTube and I realized that there wasn't a way to invoke the picture-in-picture functionality with a shortcut (at least on Safari 16.4).
My fingers rested on ⌥+P, so it made sense to add a shortcut to toggle the picture-in-picture with that key combination.

Since I'm already using UserScripts, the most straightforward thing to do was writing an userscript.

UserScripts icon UserScripts

Open

An open source Safari extension that lets you save and run arbitrary bits of JavaScript (and CSS) code for the websites you visit.

The code is pretty simple, but it gets the job done and, most importantly, saves me a few clicks.

/**
 * Key combination to use.
 *
 * @param {KeyboardEvent} e - Keyboard Event
 */
const KEY_COMBINATION = (e) => e.code === "KeyP" && e.altKey;
 
(function () {
  "use strict";
  const video = document.querySelector("video");
  document.onkeydown = (e) => {
    if (KEY_COMBINATION(e)) {
      const isPiP = document.pictureInPictureElement !== null;
      video.webkitSetPresentationMode(isPiP ? "inline" : "picture-in-picture");
    }
  };
})();

If you are using an extension that supports userscripts, you can install this extension using the box below.

YouTube PiP Shortcut

Install

Open PiP in YouTube with a shortcut

Subscribe to RSS

Writing

Here are some of my thoughts.

Newsletter

Stay in the loop and get news about what I have my eyes on!

Past Issues