-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
28 lines (28 loc) · 899 Bytes
/
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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.to === 'background') {
fetch(
`https://puzzled-erin-petticoat.cyclic.app/search?q=${message.data.product_name}`
)
.then((response) => response.json())
.then((data) => {
// Display the response in the response element
if (data?.length === 0) {
chrome.runtime.sendMessage({ error: 'Error Fetching' });
}
chrome.storage.session.set({
productList: data.length !== 0 ? data : [],
});
chrome.runtime.sendMessage({
to: 'popup',
data,
originalProduct: message.data,
});
return true;
})
.catch((error) => {
// Display the error in the response element
console.log(error);
chrome.runtime.sendMessage({ error: error.message });
});
}
});