-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
143 lines (115 loc) · 4.41 KB
/
popup.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { getActiveTabURL } from "./utils.js";
let allPlaylists;
const addNewPlaylist = (playlists, playlist, index) => {
console.log(playlist);
const playlistTitleElement = document.createElement("div");
const controlsElement = document.createElement("div");
const newPlaylistElement = document.createElement("div");
playlistTitleElement.textContent = playlist.name;
playlistTitleElement.className = "playlist-title";
playlistTitleElement.addEventListener("click", viewPlaylist);
controlsElement.className = "playlist-controls";
setPlaylistAttributes("shuffle", onShuffle, controlsElement);
setPlaylistAttributes("delete", onDelete, controlsElement);
newPlaylistElement.id = "playlist-" + playlist.id;
newPlaylistElement.className = "playlist";
newPlaylistElement.setAttribute("playlistId", playlist.id);
newPlaylistElement.setAttribute("playlistIndex", index);
newPlaylistElement.appendChild(playlistTitleElement);
newPlaylistElement.appendChild(controlsElement);
playlists.appendChild(newPlaylistElement);
};
const addNewVideo = (videos,video) => {
const videoTitleElement = document.createElement("div");
const newVideoElement = document.createElement("div");
videoTitleElement.textContent = video.name;
videoTitleElement.className = "video-title";
newVideoElement.id = "video-" + video.id;
newVideoElement.className = "video";
newVideoElement.setAttribute("videoId", video.id);
newVideoElement.appendChild(videoTitleElement);
videos.appendChild(newVideoElement);
}
const viewPlaylist = async e => {
console.log(e.target);
var index = e.target.parentNode.getAttribute("playlistindex");
var curVideos = allPlaylists[index].videos;
console.log(curVideos);
document.getElementsByClassName("title")[0].innerHTML = e.target.textContent;
const videosElement = document.getElementById("items");
videosElement.innerHTML = "";
if (curVideos.length > 0) {
for (let i = 0; i < curVideos.length; i++) {
const video = curVideos[i];
addNewVideo(videosElement, video);
}
} else {
videosElement.innerHTML = '<i class="row">This playlist has no videos.</i>';
}
}
const viewPlaylists = (playlists = []) => {
document.getElementsByClassName("title")[0].innerHTML = "Your playlists";
const playlistsElement = document.getElementById("items");
playlistsElement.innerHTML = "";
console.log(playlists);
if (playlists.length > 0) {
for (let i = 0; i < playlists.length; i++) {
const playlist = playlists[i];
addNewPlaylist(playlistsElement, playlist, i);
}
} else {
playlistsElement.innerHTML = '<i class="row">You have no playlists.</i>';
}
return;
};
const onShuffle = async e => {
const playlistId = e.target.parentNode.parentNode.getAttribute("playlistid");
const activeTab = await getActiveTabURL();
chrome.tabs.sendMessage(activeTab.id, {
type: "STARTSHUFFLE",
value: playlistId,
});
};
const onDelete = async e => {
const activeTab = await getActiveTabURL();
const playlistId = e.target.parentNode.parentNode.getAttribute("playlistid");
const playlistElementToDelete = document.getElementById("playlist-" + playlistId);
playlistElementToDelete.parentNode.removeChild(playlistElementToDelete);
chrome.tabs.sendMessage(activeTab.id, {
type: "DELETE",
value: playlistId,
}, window.location.reload);
};
const setPlaylistAttributes = (src, eventListener, controlParentElement) => {
const controlElement = document.createElement("img");
controlElement.src = "assets/" + src + ".png";
controlElement.title = src;
controlElement.addEventListener("click", eventListener);
controlParentElement.appendChild(controlElement);
};
document.addEventListener("DOMContentLoaded", async () => {
const activeTab = await getActiveTabURL();
if (activeTab.url.includes("youtube.com")) {
var playlistIds = await new Promise((resolve) => {
chrome.storage.local.get(null, (playlistIds) => {
resolve(Object.keys(playlistIds));
});
});
var playlists = [], curPlaylist;
allPlaylists = [];
for (var key in playlistIds) {
curPlaylist = await new Promise((resolve) => {
chrome.storage.local.get([playlistIds.at(key)], (obj) => {
console.log(obj[playlistIds.at(key)]);
resolve(JSON.parse(obj[playlistIds.at(key)]));
});
});
playlists.push(curPlaylist);
allPlaylists.push(curPlaylist);
}
viewPlaylists(playlists);
} else {
const container = document.getElementsByClassName("container")[0];
container.innerHTML = '<div class="title">This is not a youtube page.</div>';
}
});