-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
75 lines (68 loc) · 2.41 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// background.js
async function myBackgroundFunction(message) {
var url = "https://youtubefetch.onrender.com/url";
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ userUrl: message }),
});
console.log(response);
const data_1 = await response.json();
console.log(data_1);
return data_1;
} catch (error) {
console.error(error);
throw error;
}
}
chrome.runtime.onConnect.addListener(function (port) {
console.assert(port.name === "content-script");
// Listen for messages from the content script
port.onMessage.addListener(function (msg) {
if (msg.action === "callBackgroundFunction") {
myBackgroundFunction(msg.message)
.then((result) => {
console.log("Result from myBackgroundFunction:", result);
})
.catch((error) => {
console.error("Error in myBackgroundFunction:", error);
});
}
});
});
// chrome.runtime.onInstalled.addListener(function () {
// console.log("STARTED")
// chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
// var activeTab = tabs[0];
// var tabUrl = activeTab.url;
// console.log("Active tab URL:", tabs);
// // Add your initialization logic here, using the tabUrl if needed
// });
// })
chrome.tabs.onActivated.addListener(function (activeInfo) {
// Get the URL of the active tab
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var url = tabs[0].url;
// Store the URL in a variable or object
url = url.split("=")[1]
if (url.includes("&")) {
url = url.split("&")[0]
}
var videoId = url
console.log(videoId)
url = "https://youtubefetch.onrender.com/url"
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ userUrl: videoId }),
})
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
});
});