-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
30 lines (29 loc) · 937 Bytes
/
background.js
File metadata and controls
30 lines (29 loc) · 937 Bytes
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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "fetchCurrency") {
fetchCurrency().then(rate => {
sendResponse({ rate });
}).catch(error => {
sendResponse({ error: error.message });
});
return true; // indicates that sendResponse will be called asynchronously
}
});
async function fetchCurrency() {
const response = await fetch('https://api.sandbox.transferwise.tech/v3/quotes/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"sourceCurrency": "CNY",
"targetCurrency": "GBP",
"sourceAmount": null,
"targetAmount": null
})
});
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
const data = await response.json();
return data.rate;
} // TODO: not sure if needed anymore??