-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpip.js
40 lines (36 loc) · 1.4 KB
/
pip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const VERSION = "1.1.1";
const messageSuffix = "\n\n--pip.js version v"+VERSION+"\nalanhogan.com/bookmarklets";
if (!document.pictureInPictureEnabled) {
alert("Your browser doesn’t support picture-in-picture!" + messageSuffix);
return;
}
const videos = document.getElementsByTagName("video");
if (videos.length < 1) {
alert("No HTML5 videos were detected on page! Note: The current version of this script does not deal with iFrames" + (document.getElementsByTagName('iframe').length ? " and there is at least one on the page." : ", but there are none detected on this page." ) + messageSuffix);
return;
}
let curPipFound = false;
for (let i = 0; i <videos.length; i++) {
const pipEl = document.pictureInPictureElement;
console.log('pipEl', pipEl);
if (!document.pictureInPictureElement) {
console.log('Showing PIP for video #' + (i + 1) + messageSuffix);
videos[i].requestPictureInPicture();
break;
} else if (!curPipFound && document.pictureInPictureElement == videos[i]) {
curPipFound = true;
console.log('Found!' + messageSuffix);
if (i == videos.length - 1) {
videos[0].requestPictureInPicture();
break;
}
} else if (!curPipFound) {
console.log('not found yet...' + messageSuffix);
} else if (curPipFound) {
videos[i].requestPictureInPicture();
break;
}
}
if (!curPipFound && document.pictureInPictureElement) {
document.exitPictureInPicture();
}