-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbackground.js
More file actions
58 lines (56 loc) · 2.37 KB
/
background.js
File metadata and controls
58 lines (56 loc) · 2.37 KB
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
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const tabs = await chrome.tabs.query({});
for (const tab of tabs) {
if (tab.url && tab.url.includes("youtube.com/watch")) {
try {
chrome.storage.sync.get(["extensionEnabled", "autoPiPEnabled", "closePiPOnReturnEnabled"], (data) => {
const extensionEnabled = data.extensionEnabled ?? true;
const autoPiPEnabled = data.autoPiPEnabled ?? false;
const closePiPOnReturnEnabled = data.closePiPOnReturnEnabled ?? false;
const tabBool = (tab.id === activeInfo.tabId);
// If the extension is disabled, do nothing
if (!extensionEnabled) return;
// If the extension is enabled, execute the script
if (autoPiPEnabled || closePiPOnReturnEnabled) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (autoPiPEnabled, closePiPOnReturnEnabled, tabBool) => {
if (autoPiPEnabled && !tabBool) {
var PipEnablerEvent;
if (!PipEnablerEvent){
PipEnablerEvent = () => {
// window.focus();
console.log("heell0")
const video = document.querySelector("video");
if ((video) && (document.pictureInPictureElement !== video) && (!video.paused)) {
video.requestPictureInPicture().catch(console.error);
}
}
window.onblur = PipEnablerEvent;
}
PipEnablerEvent();
}
if (closePiPOnReturnEnabled && tabBool) {
var PipDisablerEvent;
if (!PipDisablerEvent){
PipDisablerEvent = () => {
window.focus();
if (document.pictureInPictureElement) {
document.exitPictureInPicture().catch(console.error);
}
}
window.onfocus = PipDisablerEvent;
}
PipDisablerEvent();
}
},
args: [autoPiPEnabled, closePiPOnReturnEnabled, tabBool]
});
}
});
} catch (error) {
console.error("Error executing script:", error);
}
}
}
});