-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
59 lines (49 loc) · 1.77 KB
/
Copy pathbackground.js
File metadata and controls
59 lines (49 loc) · 1.77 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
// ==========================================
// CONTEXT MENU
// ==========================================
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "toggle-pip-menu",
title: "Toggle Picture-in-Picture",
contexts: ["video", "page"]
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "toggle-pip-menu") {
triggerPiP(tab.id);
}
});
// ==========================================
// KEYBOARD COMMANDS
// ==========================================
chrome.commands.onCommand.addListener((command) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
if (!tab) return;
if (command === "toggle-pip") {
triggerPiP(tab.id);
} else if (command === "close-pip") {
closePiP(tab.id);
}
});
});
// ==========================================
// AUTO-PIP ON VISIBILITY CHANGE
// ==========================================
// Handled directly in content/main.js using `visibilitychange` listener.
// This natively supports both tab switching and browser minimizing.
// ==========================================
// HELPER FUNCTIONS
// ==========================================
function closePiP(tabId) {
chrome.tabs.sendMessage(tabId, { type: "CLOSE_PIP" }).catch(() => {
// If content script not ready or error
});
}
function triggerPiP(tabId) {
chrome.tabs.sendMessage(tabId, { type: "TOGGLE_PIP" }).catch((err) => {
// Fallback or ignore if script not ready
console.log("Could not send message to tab (styles/scripts might not be loaded):", err);
// Optionally inject scripts if missing (advanced) but simplified for now
});
}