-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
60 lines (53 loc) · 2.28 KB
/
popup.js
File metadata and controls
60 lines (53 loc) · 2.28 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
document.addEventListener("DOMContentLoaded", () => {
const openPlaylistButton = document.getElementById("open-playlist");
openPlaylistButton.addEventListener("click", () => {
chrome.runtime.sendMessage({ type: "openPlaylist" });
});
const addAllButton = document.getElementById("add-all");
const exportPlaylistButton = document.getElementById("export-playlist");
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tabId = tabs[0].id;
chrome.storage.local.get({ audioUrls: {} }, (result) => {
const audioList = document.getElementById("audio-list");
const urls = result.audioUrls[tabId] || [];
if (urls.length > 0) {
urls.forEach((url) => {
const listItem = document.createElement("li");
const link = document.createElement("a");
link.href = url;
link.textContent = decodeURI(url).split("/").pop();
link.target = "_blank";
const addButton = document.createElement("button");
addButton.textContent = "+";
addButton.addEventListener("click", () => {
chrome.runtime.sendMessage({ type: "addToPlaylist", url });
});
listItem.appendChild(link);
listItem.appendChild(addButton);
audioList.appendChild(listItem);
});
addAllButton.addEventListener("click", () => {
chrome.runtime.sendMessage({ type: "addToPlaylist", url: urls });
});
exportPlaylistButton.addEventListener("click", () => {
const m3uContent = "#EXTM3U\n" + urls.join("\n");
const blob = new Blob([m3uContent], { type: "audio/x-mpegurl" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "playlist.m3u";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
} else {
const noAudioMessage = document.createElement("p");
noAudioMessage.textContent = "No audio files detected on this page.";
audioList.parentElement.replaceChild(noAudioMessage, audioList);
addAllButton.style.display = "none";
exportPlaylistButton.style.display = "none";
}
});
});
});