-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
38 lines (35 loc) · 1.08 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
const apiKey = "API_KEY";
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const url = tabs[0].url;
const ul = document.getElementById("keywords");
const videoId = url.match(
/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i
);
const request = new Request(
`https://www.googleapis.com/youtube/v3/videos?key=${apiKey}&id=${videoId[1]}&part=snippet`,
{ cache: "default" }
);
if (videoId) {
fetch(request)
.then((response) => {
return response.json();
})
.then((data) => {
const keywords = data.items[0].snippet.tags;
ul.textContent = "";
if (!keywords) {
ul.innerHTML = "<h3>No keywords found</h3>";
}
for (let i = 0; i < keywords.length; i++) {
const li = document.createElement("li");
li.textContent = keywords[i];
ul.appendChild(li);
}
})
.catch((error) => {
console.error(error);
});
} else {
ul.innerHTML = "<h3>This is not a youtube video page</h3>";
}
});