Chrome Extension: How to trigger Picture-in-Picture on tab switch without user gesture?

6 views
Skip to first unread message

Erez

unread,
9:03 AM (5 hours ago) 9:03 AM
to Chromium Extensions
I'm building a Chrome Extension (Manifest V3) that automatically triggers Picture-in-Picture (PiP) for YouTube videos when the user switches to a different tab, and exits PiP when they return.

The Problem:

The extension works perfectly the first time:

User plays video on YouTube
User switches tabs → PiP activates ✅
User returns to YouTube → PiP closes ✅
User switches tabs again (without clicking anything) → PiP fails ❌
The error in console is:

Failed to execute 'requestPictureInPicture' on 'HTMLVideoElement':
Must be handling a user gesture if there isn't already an element in Picture-in-Picture.
What I've Tried:

Direct API call via video.requestPictureInPicture() - works only after user gesture
autoPictureInPicture attribute - doesn't seem to work in Chrome
Clicking YouTube's PiP button programmatically - same gesture restriction
Native Messaging Host with simulated mouse clicks - clicks happen on wrong window/tab
My Architecture:

background.js
: Listens for chrome.tabs.onActivated, sends message to content script
content.js
: Receives message and calls video.requestPictureInPicture()
Question:

Is there any way to bypass or work around the user gesture requirement for PiP when the user switches tabs? I understand it's a security feature, but this seems like a legitimate use case.

Alternatively, is there a way to "preserve" the user gesture context so it persists after returning to the tab?

Environment:

Chrome 131+
Manifest V3
Windows 11
Reply all
Reply to author
Forward
0 new messages