This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip.js
60 lines (51 loc) · 1.8 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function addVideoEls() {
let videoEls = document.querySelectorAll('video');
let pipButton = document.createElement('button');
pipButton.setAttribute('class', 'pipvid pipvid-pip');
pipButton.setAttribute('title', 'Make video picture in picture');
pipButton.setAttribute('data-pipvid-action', 'pip');
let pipImage = document.createElement('img');
pipImage.src = chrome.runtime.getURL('icon.png');
pipButton.append(pipImage);
let pipVidControls = document.createElement('div');
pipVidControls.setAttribute('class', 'pipvid-controls');
pipVidControls.append(pipButton);
let vcount = 0;
videoEls.forEach((video) => {
vcount++;
video.setAttribute('data-pipvid-id', `pv-${vcount}`);
pipButton.setAttribute('data-pipvid', `pv-${vcount}`);
video.parentNode.prepend(pipVidControls);
});
triggerActions();
}
function triggerActions() {
let pipButtons = document.querySelectorAll('.pipvid');
pipButtons.forEach((el) => {
el.addEventListener('click', () => {
let pipVidId = el.getAttribute('data-pipvid');
let pipVidAction = el.getAttribute('data-pipvid-action');
let selector = `[data-pipvid-id=${pipVidId}]`;
let vid = document.querySelector(selector);
if (pipVidAction == "pip") {
triggerPip(vid);
return;
}
return;
});
});
}
function cleanSlate() {
let pipvidControls = document.querySelectorAll('.pipvid-controls');
pipvidControls.forEach((el) => el.parentNode.removeChild(el));
}
function triggerPip(vid) {
if (vid) {
if (document.pictureInPictureElement) {
document.exitPictureInPicture();
}
vid.requestPictureInPicture();
}
}
cleanSlate();
addVideoEls();