-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
106 lines (96 loc) · 2.89 KB
/
background.js
File metadata and controls
106 lines (96 loc) · 2.89 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
97
98
99
100
101
102
103
104
105
106
let playerTabId = null;
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "audioUrls") {
const tabId = sender.tab.id;
chrome.storage.local.get({ audioUrls: {} }, (result) => {
const audioUrls = result.audioUrls;
const existingUrls = new Set(audioUrls[tabId] || []);
message.urls.forEach(url => existingUrls.add(url));
audioUrls[tabId] = Array.from(existingUrls);
chrome.storage.local.set({ audioUrls }, () => {
updateBadge(tabId);
});
});
} else if (message.type === "addToPlaylist") {
addToPlaylist(message.url);
} else if (message.type === "openPlaylist") {
openPlayer();
}
});
function createPlayerTab() {
chrome.tabs.create({ url: chrome.runtime.getURL("player.html") }, (tab) => {
playerTabId = tab.id;
});
}
function findPlayer() {
chrome.tabs.query({ url: chrome.runtime.getURL("player.html") }, (tabs) => {
if (tabs.length > 0) {
playerTabId = tabs[0].id;
chrome.tabs.update(playerTabId, { active: true });
} else {
createPlayerTab();
}
})
}
function openPlayer() {
if (playerTabId) {
chrome.tabs.get(playerTabId, (tab) => {
if (tab) {
chrome.tabs.update(playerTabId, { active: true });
} else {
findPlayer();
}
});
} else {
findPlayer();
}
}
function addToPlaylist(url) {
chrome.storage.local.get({ playlist: [] }, (result) => {
let playlist = result.playlist;
const count = playlist.length;
if (Array.isArray(url)) {
let uniqueSet = new Set(playlist);
url.forEach(item => uniqueSet.add(item));
playlist = [...uniqueSet];
} else if (!playlist.includes(url)) {
playlist.push(url);
}
if (playlist.length > count) {
chrome.storage.local.set({ playlist });
}
});
}
chrome.tabs.onRemoved.addListener((tabId) => {
if (tabId === playerTabId) {
playerTabId = null;
}
chrome.storage.local.get({ audioUrls: {} }, (result) => {
const audioUrls = result.audioUrls;
delete audioUrls[tabId];
chrome.storage.local.set({ audioUrls });
});
});
chrome.tabs.onActivated.addListener((activeInfo) => {
updateBadge(activeInfo.tabId);
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'loading') {
chrome.storage.local.get({ audioUrls: {} }, (result) => {
const audioUrls = result.audioUrls;
if (audioUrls[tabId]) {
delete audioUrls[tabId];
chrome.storage.local.set({ audioUrls }, () => {
updateBadge(tabId);
});
}
});
}
});
function updateBadge(tabId) {
chrome.storage.local.get({ audioUrls: {} }, (result) => {
const count = result.audioUrls[tabId] ? result.audioUrls[tabId].length : 0;
chrome.action.setBadgeText({ text: count > 0 ? count.toString() : "", tabId });
chrome.action.setBadgeBackgroundColor({ color: "#007bff", tabId });
});
}