Skip to content

Instantly share code, notes, and snippets.

@sunoru
Last active March 2, 2021 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunoru/86a84d5d6ecd1cb802329033b24e8b20 to your computer and use it in GitHub Desktop.
Save sunoru/86a84d5d6ecd1cb802329033b24e8b20 to your computer and use it in GitHub Desktop.
// startPictureInPicture.js
(() => {
const getVideos = (doc) => {
let videos = Array.from(doc.getElementsByTagName("video"))
const iframes = doc.getElementsByTagName("iframe")
for (const iframe of iframes) {
try {
videos = videos.concat(getVideos(iframe.contentDocument))
} catch (e) {
console.warn(iframe)
}
}
return videos
}
const startPictureInPicutre = () => {
const videos = getVideos(document)
if (videos.length === 0) {
console.log("No videos.")
} else {
const getKey = (i) => `ok${i === 0 ? "" : i}`
const clearOKs = () => videos.forEach((_, i) => void delete window[getKey(i)])
videos.forEach((video, i) => {
const key = getKey(i)
console.log(`${key}: `, video)
Object.defineProperty(window, key, {
configurable: true,
get: () => {
video.requestPictureInPicture()
clearOKs()
}
})
})
console.log("ok[i=0] ?")
}
}
startPictureInPicutre()
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment