This repository was archived by the owner on Apr 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
96 lines (85 loc) · 3.45 KB
/
Copy pathbackground.js
File metadata and controls
96 lines (85 loc) · 3.45 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// background.js
// 1. Shared save function
function saveItem(itemData) {
chrome.storage.local.get({ watchlist: [] }, (result) => {
let list = result.watchlist;
let existingIndex = -1;
let pinned = false;
// 1. Check for Series Match (Smart Replacement)
if (itemData.seriesId) {
existingIndex = list.findIndex(item => item.seriesId === itemData.seriesId);
}
// 2. Fallback: Check for Exact URL Match (if no series match found)
if (existingIndex === -1) {
existingIndex = list.findIndex(item => item.id === itemData.id);
}
// 3. Remove existing item and preserve pin state
if (existingIndex > -1) {
pinned = list[existingIndex].pinned;
list.splice(existingIndex, 1);
}
itemData.pinned = pinned;
list.unshift(itemData);
if (list.length > 50) list.pop();
chrome.storage.local.set({ watchlist: list });
});
}
// 2. Handle Keyboard Shortcuts
chrome.commands.onCommand.addListener((command) => {
// This command now messages the background script itself to find video details
if (command === "manual-save") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
// Ask content script for video details
chrome.tabs.sendMessage(tabs[0].id, { action: "GET_VIDEO_DETAILS" }, (response) => {
if (chrome.runtime.lastError) {
console.log("Error getting video details:", chrome.runtime.lastError.message);
return;
}
if (response && response.data) {
saveItem(response.data);
}
});
}
});
}
});
// 3. Handle Messages from other scripts
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
// Message from content script with auto-saved video data
if (msg.action === "SAVE_VIDEO") {
saveItem(msg.data);
sendResponse({ success: true });
return true;
}
// Message from the side panel to manually bookmark a page
else if (msg.action === "TRIGGER_MANUAL_ADD") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs && tabs[0]) {
const tab = tabs[0];
const pageUrl = tab.url.split('#')[0];
const data = {
id: new URL(tab.url).hostname + new URL(tab.url).pathname,
url: pageUrl,
hostname: new URL(tab.url).hostname,
title: tab.title || pageUrl,
episode: '',
timestamp: 0,
duration: 0,
thumbnail: '',
favicon: tab.favIconUrl || `${new URL(tab.url).origin}/favicon.ico`,
lastWatched: Date.now()
};
saveItem(data);
// We could add a notification here if needed
}
});
sendResponse({ success: true });
return true;
}
});
// 4. (Chrome Only) Enable side panel behavior
if (chrome.sidePanel) {
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error(error));
}