-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.ts
67 lines (57 loc) · 1.81 KB
/
background.ts
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
chrome.tabs.onActivated.addListener(async (activeInfo) => {
console.log("🔵 whatmedoin onActivated", activeInfo);
handleTabChange(activeInfo.tabId);
});
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
console.log("🔵 whatmedoin onUpdated", { tabId, changeInfo, tab });
if (changeInfo.status === "complete") {
handleTabChange(tabId);
}
});
async function handleTabChange(tabId: number) {
const tab = await chrome.tabs.get(tabId);
if (!tab.url || !tab.active) return;
const allowedDomains = ["guidetojapanese.org", "animelon.com", "youtube.com"];
const url = new URL(tab.url);
const isAllowedDomain = allowedDomains.some(
(domain) => url.hostname === domain || url.hostname.endsWith("." + domain),
);
if (!isAllowedDomain) {
console.log("🟡 whatmedoin: URL not in allowed domains", tab);
return;
}
try {
const storageValue = await chrome.storage.local.get("apiUrl");
const apiUrl = storageValue.apiUrl;
if (!apiUrl || apiUrl === "") {
console.warn("🟡 whatmedoin: No API URL provided");
return;
}
const payload = {
platform: "browser",
title: tab.title,
url: tab.url,
};
console.log("Sending to", apiUrl, payload);
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (response.ok) {
console.log("🟢 whatmedoin");
} else {
console.error("🔴 whatmedoin API error:", response.statusText);
}
} catch (error) {
console.error("🔴 whatmedoin extension error:", error);
}
}
// chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// if (message.type === "api-url") {
// API_URL = message.url;
// }
// return true;
// });