-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
48 lines (43 loc) · 1.47 KB
/
background.js
File metadata and controls
48 lines (43 loc) · 1.47 KB
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
// SourceCalc Background Service Worker
// Handles exchange rate updates and future premium features
const EXCHANGE_RATE_KEY = 'exchangeRate';
const RATE_UPDATE_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours
// Fetch latest CNY/USD exchange rate
async function updateExchangeRate() {
try {
// Using a free exchange rate API
const response = await fetch('https://open.er-api.com/v6/latest/CNY');
if (response.ok) {
const data = await response.json();
const rate = data.rates.USD;
await chrome.storage.local.set({
[EXCHANGE_RATE_KEY]: rate,
exchangeRateUpdated: Date.now(),
});
console.log('SourceCalc: Exchange rate updated:', rate);
return rate;
}
} catch (e) {
console.log('SourceCalc: Failed to fetch exchange rate, using default');
}
return 0.137; // fallback
}
// Update rate on install and periodically
chrome.runtime.onInstalled.addListener(() => {
updateExchangeRate();
chrome.alarms.create('updateRate', { periodInMinutes: 60 * 12 }); // every 12 hours
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'updateRate') {
updateExchangeRate();
}
});
// Listen for messages from content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'getExchangeRate') {
chrome.storage.local.get([EXCHANGE_RATE_KEY], (data) => {
sendResponse({ rate: data[EXCHANGE_RATE_KEY] || 0.137 });
});
return true; // async response
}
});