-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
89 lines (83 loc) · 2.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
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
console.log("background.js is running");
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === "install") {
chrome.storage.local.set({ openai_api_key: "" });
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "openSidePanel") {
chrome.windows.create(
{
url: "modal.html",
type: "popup",
width: 400,
height: 300,
},
(window) => {
chrome.windows.onRemoved.addListener((windowId) => {
if (windowId === window.id) {
sendResponse({ popupClosed: true });
}
});
}
);
return true;
} else if (message.action === "extractTranscript") {
function getYoutubeVideoId(url) {
const regex = /[?&]v=([^&#]*)/;
const match = url.match(regex);
return match ? match[1] : null;
}
let vidId = getYoutubeVideoId(message.url); // Use the URL passed in the message
fetch(`https://youtubetranscript.com/?server_vid2=${vidId}`, {
headers: {
accept: "application/xml, text/xml, */*; q=0.01",
"accept-language": "en-US,en;q=0.9",
priority: "u=1, i",
"sec-ch-ua": `\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google
Chrome\";v=\"126\""`,
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
},
referrerPolicy: "strict-origin-when-cross-origin",
body: null,
method: "GET",
mode: "cors",
credentials: "omit",
})
.then((resp) => resp.text())
.then((resp) => {
console.log(resp);
sendResponse({ status: "Success", data: resp });
})
.catch((error) => {
console.error("Error:", error);
sendResponse({ status: "Error", error: error.message });
});
return true; // Keep the message channel open for asynchronous response
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete") {
if (tab.url && tab.url.match(/https:\/\/www\.youtube\.com\/watch\?v=.*/)) {
chrome.sidePanel
.setOptions({
tabId: tabId,
path: "sidepanel/sidepanel.html",
enabled: true,
})
.then(() => {
chrome.sidePanel.open({ tabId: tabId });
});
} else {
chrome.sidePanel.setOptions({
tabId: tabId,
enabled: false,
});
}
}
});