Last active
March 2, 2021 02:06
-
-
Save sunoru/86a84d5d6ecd1cb802329033b24e8b20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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