forked from 1MindLabs/mivro-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
77 lines (73 loc) · 2.4 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
console.log("background.js loaded");
/**
* Fetches an SVG from a given URL and sends it as a response.
* @param {string} url - The URL of the SVG to fetch.
* @param {function} sendResponse - The function to call with the SVG data.
*/
function fetchSvg(url, sendResponse) {
fetch(url)
.then((response) => response.text())
.then((data) => {
sendResponse({ svg: data });
})
.catch((error) => {
console.error("Error fetching SVG:", error);
sendResponse({ error: error.toString() });
});
}
/**
* Fetches product information and sends it as a response.
* @param {function} sendResponse - The function to call with the product information.
*/
function fetchProductInfo(sendResponse, product) {
fetch("http://127.0.0.1:5000/api/v1/search/database", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Mivro-Email": "[email protected]",
"Mivro-Password": "test@1",
},
body: JSON.stringify({
product_keyword: product,
}),
timeout: 15000,
})
.then((res) => {
if (!res.ok) {
throw new Error(`Failed to fetch data: HTTP Status ${res.status}`);
}
return res.json();
})
.then((data) => {
console.log("Fetch Successful:", data);
sendResponse({ productInfo: data });
})
.catch((error) => {
console.error("Fetch Failed:", error);
sendResponse({ error: error.toString() });
});
}
const iconPaths = {
fetchIconM: "assets/btn-icons/m.svg",
fetchIconMivro: "assets/oth-icons/mivro.svg",
fetchIconClose: "assets/btn-icons/close.svg",
fetchIconShare: "assets/btn-icons/share.svg",
fetchIconHeart: "assets/btn-icons/heart.svg",
fetchHeartFilledSvg: "assets/btn-icons/heart-filled.svg",
fetchHeartSvg: "assets/btn-icons/heart.svg",
fetchIconInfo: "assets/btn-icons/info.svg",
fetchIconFlag: "assets/btn-icons/flag.svg",
fetchIconGemini: "assets/oth-icons/gemini.svg",
};
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.text in iconPaths) {
fetchSvg(chrome.runtime.getURL(iconPaths[msg.text]), sendResponse);
return true; // keeps the message channel open until sendResponse is called
} else if (msg.text === "fetchProductInfo") {
fetchProductInfo(sendResponse, msg.product);
return true;
} else {
console.error(`Unknown message text: ${msg.text}`);
sendResponse({ error: `Unknown message text: ${msg.text}` });
}
});