-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
60 lines (51 loc) · 1.54 KB
/
background.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
const ApiUrl = "https://tmkoc-perfect-episodes.cyclic.app";
// console.log("background.js running");
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (
changeInfo.status === "complete" &&
tab.url &&
tab.url.includes("youtube.com/watch")
) {
const queryParameters = tab.url.split("?")[1];
const urlParameters = new URLSearchParams(queryParameters);
// console.log(tab);
chrome.tabs.sendMessage(tabId, {
type: "NEW",
videoId: urlParameters.get("v"),
});
}
});
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
// console.log("get response from contentScript");
if (message.type === "FETCH_EPISODES") {
// console.log("message received");
// getEpisodes().then(() => {
// const responseData = "OK";
// console.log(responseData);
// });
// sendResponse(responseData);
// return true;
getEpisodes();
const responseData = "OK";
// console.log(responseData);
sendResponse(responseData);
}
});
function getEpisodes() {
return new Promise((resolve, reject) => {
fetch(`${ApiUrl}/episodes`)
.then((response) => response.json())
.then((data) => {
const episodes = data.episodes;
chrome.storage.local.set({ episodes: episodes }, () => {
// console.log("Episodes stored in local storage:", episodes);
resolve();
});
})
.catch((error) => {
console.error("Error fetching episodes:", error);
reject(error);
});
});
}
getEpisodes();