mirror of
https://github.com/chylex/Firefox-SCsCC.git
synced 2025-04-09 16:15:43 +02:00
rewrite bg scripts
This commit is contained in:
parent
2ca1b8eebb
commit
461736002b
src/background_scripts
@ -1,4 +1,6 @@
|
||||
import options from './options';
|
||||
import getCurrRate from './utils/getCurrRate';
|
||||
import showNotification from './utils/showNotification';
|
||||
|
||||
import icon16 from '../icons/icon16.png';
|
||||
import icon32 from '../icons/icon32.png';
|
||||
@ -7,8 +9,6 @@ import icon16Off from '../icons/icon16_off.png';
|
||||
import icon32Off from '../icons/icon32_off.png';
|
||||
import icon48Off from '../icons/icon48_off.png';
|
||||
|
||||
const manifest = browser.runtime.getManifest();
|
||||
const requests = {};
|
||||
const icons = {
|
||||
enabled: {
|
||||
16: browser.runtime.getURL(icon16),
|
||||
@ -75,143 +75,39 @@ browser.storage.local.get()
|
||||
|
||||
browser.storage.onChanged.addListener((changes) => {
|
||||
if (changes.preferences && changes.preferences.newValue) {
|
||||
onPrefsChange(changes.preferences.newValue);
|
||||
const newPrefs = changes.preferences.newValue;
|
||||
if (newPrefs.enabled !== preferences.enabled) browser.browserAction.setIcon({ path: newPrefs.enabled ? icons.enabled : icons.disabled });
|
||||
|
||||
preferences = newPrefs;
|
||||
}
|
||||
if (changes.currRates && changes.currRates.newValue) {
|
||||
onCurrRatesChange(changes.currRates.newValue);
|
||||
currRates = changes.currRates.newValue;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
browser.runtime.onMessage.addListener(({ type, data }, sender, sendResponse) => {
|
||||
if (type === 'getStorage') {
|
||||
sendResponse({ preferences, currRates });
|
||||
return false;
|
||||
}
|
||||
|
||||
browser.runtime.onMessage.addListener(({ type, data }) => {
|
||||
if (type === 'getCurrRate') {
|
||||
getCurrRate(data.from, data.to)
|
||||
.then((currRate) => { sendResponse(currRate); });
|
||||
return true;
|
||||
const { from: fromCurr, to: toCurr } = data;
|
||||
|
||||
return getCurrRate(currRates, fromCurr, toCurr)
|
||||
.then((currRate) => {
|
||||
console.log(fromCurr, toCurr, currRate);
|
||||
const reqKey = `${fromCurr}to${toCurr}`;
|
||||
|
||||
if (!currRates[reqKey] || currRates[reqKey].value !== currRate.value || currRates[reqKey].updatedAt !== currRate.updatedAt) {
|
||||
if (preferences.noti) {
|
||||
const oldValue = (currRates[reqKey] && currRates[reqKey].value) || null;
|
||||
showNotification(fromCurr, toCurr, oldValue, currRate.value);
|
||||
}
|
||||
|
||||
const newCurrRates = Object.assign({}, currRates, { [reqKey]: currRate });
|
||||
browser.storage.local.set({ currRates: newCurrRates });
|
||||
}
|
||||
|
||||
return currRate;
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
|
||||
browser.tabs.onActivated.addListener((activeInfo) => {
|
||||
browser.tabs.sendMessage(activeInfo.tabId, { preferences, currRates })
|
||||
.catch(onError);
|
||||
});
|
||||
|
||||
|
||||
function onPrefsChange(newPrefs) {
|
||||
if (newPrefs.enabled !== preferences.enabled) browser.browserAction.setIcon({ path: newPrefs.enabled ? icons.enabled : icons.disabled });
|
||||
|
||||
preferences = newPrefs;
|
||||
|
||||
sendToActiveTabs({ preferences });
|
||||
}
|
||||
|
||||
function onCurrRatesChange(newCurrRates) {
|
||||
currRates = newCurrRates;
|
||||
|
||||
sendToActiveTabs({ currRates });
|
||||
}
|
||||
|
||||
function sendToActiveTabs(data) {
|
||||
const eachActiveTab = (activeTab) => {
|
||||
browser.tabs.sendMessage(activeTab.id, data)
|
||||
.catch(onError);
|
||||
};
|
||||
|
||||
browser.tabs.query({ active: true })
|
||||
.then((activeTabs) => { activeTabs.forEach(eachActiveTab); })
|
||||
.catch(onError);
|
||||
}
|
||||
|
||||
|
||||
function getCurrRate(fromCurr, toCurr) {
|
||||
const reqKey = `${fromCurr}to${toCurr}`;
|
||||
|
||||
if (!requests[reqKey]) {
|
||||
// if last update was within an hour, resolve
|
||||
if (currRates[reqKey] && currRates[reqKey].value && currRates[reqKey].updatedAt && Date.now() - currRates[reqKey].updatedAt < 3600000) {
|
||||
return Promise.resolve({ currRate: currRates[reqKey] });
|
||||
}
|
||||
|
||||
requests[reqKey] = new Promise((resolve) => {
|
||||
// console.log(`SCsCC - get ${fromCurr} to ${toCurr}`);
|
||||
const req = new XMLHttpRequest();
|
||||
|
||||
const onEnd = function listener(event) {
|
||||
const request = event ? event.target : null;
|
||||
const currRate = reqComplete(request, fromCurr, toCurr);
|
||||
|
||||
resolve({ currRate });
|
||||
requests[reqKey] = undefined;
|
||||
};
|
||||
|
||||
req.addEventListener('load', onEnd);
|
||||
req.addEventListener('error', onEnd);
|
||||
|
||||
req.open('GET', `https://www.google.com/search?q=1+${fromCurr}+to+${toCurr}&hl=en`, true);
|
||||
req.send();
|
||||
});
|
||||
}
|
||||
|
||||
return requests[reqKey];
|
||||
}
|
||||
|
||||
// on getCurrRate request complete
|
||||
function reqComplete(request, fromCurr, toCurr) {
|
||||
const reqKey = `${fromCurr}to${toCurr}`;
|
||||
const currRate = currRates[reqKey] ? Object.assign({}, currRates[reqKey]) : {};
|
||||
|
||||
if (request && request.status === 200) {
|
||||
currRate.updatedAt = Date.now();
|
||||
|
||||
const txtMatch = request.responseText.match(/id=['"]?exchange_rate['"]?(?:\s+type=['"]?hidden['"]?)?\s+value=['"]?(\d+\.\d+)/i);
|
||||
|
||||
if (txtMatch && txtMatch[1]) {
|
||||
const newValue = parseFloat(txtMatch[1]);
|
||||
|
||||
if (!isNaN(newValue) && newValue !== currRate.value) {
|
||||
showNotification(fromCurr, toCurr, newValue);
|
||||
|
||||
currRate.value = newValue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// will try again if requested after 10 minues
|
||||
currRate.updatedAt = Date.now() - 3000000;
|
||||
}
|
||||
|
||||
if (!currRates[reqKey] || currRates[reqKey].value !== currRate.value || currRates[reqKey].updatedAt !== currRate.updatedAt) {
|
||||
const newCurrRates = Object.assign({}, currRates);
|
||||
newCurrRates[reqKey] = currRate;
|
||||
browser.storage.local.set({ currRates: newCurrRates });
|
||||
}
|
||||
|
||||
return currRate;
|
||||
}
|
||||
|
||||
// show notification about exchange rate updates if enabled in preferences
|
||||
function showNotification(fromCurr, toCurr, newValue) {
|
||||
if (!preferences.noti) return;
|
||||
|
||||
const reqKey = `${fromCurr}to${toCurr}`;
|
||||
const opts = {
|
||||
type: 'basic',
|
||||
title: manifest.name,
|
||||
iconUrl: icons.enabled[48],
|
||||
};
|
||||
|
||||
if (currRates[reqKey] && currRates[reqKey].value) { // on update
|
||||
opts.message = `${fromCurr} to ${toCurr} exchange rate updated:\n${currRates[reqKey].value} → ${newValue}`;
|
||||
} else { // on frist get
|
||||
opts.message = `${fromCurr} to ${toCurr} exchange rate got:\n${newValue}`;
|
||||
}
|
||||
|
||||
browser.notifications.create(opts);
|
||||
}
|
||||
|
57
src/background_scripts/utils/getCurrRate.js
Normal file
57
src/background_scripts/utils/getCurrRate.js
Normal file
@ -0,0 +1,57 @@
|
||||
const requests = {};
|
||||
|
||||
// on getCurrRate request complete
|
||||
const reqComplete = (request, currRates, reqKey) => {
|
||||
const currRate = currRates[reqKey] ? Object.assign({}, currRates[reqKey]) : {};
|
||||
|
||||
if (request && request.status === 200) {
|
||||
currRate.updatedAt = Date.now();
|
||||
|
||||
const txtMatch = request.responseText.match(/id=['"]?exchange_rate['"]?(?:\s+type=['"]?hidden['"]?)?\s+value=['"]?(\d+\.\d+)/i);
|
||||
|
||||
if (txtMatch && txtMatch[1]) {
|
||||
const newValue = parseFloat(txtMatch[1]);
|
||||
|
||||
if (!Number.isNaN(newValue) && newValue !== currRate.value) {
|
||||
currRate.value = newValue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// will try again if requested after 10 minues
|
||||
currRate.updatedAt = Date.now() - 3000000;
|
||||
}
|
||||
|
||||
return currRate;
|
||||
};
|
||||
|
||||
export default (currRates, fromCurr, toCurr) => {
|
||||
const reqKey = `${fromCurr}to${toCurr}`;
|
||||
|
||||
if (!requests[reqKey]) {
|
||||
// if last update was within an hour, resolve
|
||||
if (currRates[reqKey] && currRates[reqKey].value && currRates[reqKey].updatedAt && Date.now() - currRates[reqKey].updatedAt < 3600000) {
|
||||
return Promise.resolve({ currRate: currRates[reqKey] });
|
||||
}
|
||||
|
||||
requests[reqKey] = new Promise((resolve) => {
|
||||
// console.log(`SCsCC - get ${fromCurr} to ${toCurr}`);
|
||||
const req = new XMLHttpRequest();
|
||||
|
||||
const onEnd = function listener(event) {
|
||||
const request = event ? event.target : null;
|
||||
const currRate = reqComplete(request, currRates, reqKey);
|
||||
|
||||
resolve(currRate);
|
||||
requests[reqKey] = undefined;
|
||||
};
|
||||
|
||||
req.addEventListener('load', onEnd);
|
||||
req.addEventListener('error', onEnd);
|
||||
|
||||
req.open('GET', `https://www.google.com/search?q=1+${fromCurr}+to+${toCurr}&hl=en`, true);
|
||||
req.send();
|
||||
});
|
||||
}
|
||||
|
||||
return requests[reqKey];
|
||||
};
|
20
src/background_scripts/utils/showNotification.js
Normal file
20
src/background_scripts/utils/showNotification.js
Normal file
@ -0,0 +1,20 @@
|
||||
const manifest = browser.runtime.getManifest();
|
||||
|
||||
// show notification about exchange rate updates if enabled in preferences
|
||||
export default (fromCurr, toCurr, oldValue, newValue) => {
|
||||
if (oldValue === newValue) return;
|
||||
|
||||
const opts = {
|
||||
type: 'basic',
|
||||
title: manifest.name,
|
||||
iconUrl: browser.runtime.getURL(manifest.icons[48]),
|
||||
};
|
||||
|
||||
if (oldValue) { // on update
|
||||
opts.message = `${fromCurr} to ${toCurr} exchange rate updated:\n${oldValue} → ${newValue}`;
|
||||
} else { // on frist get
|
||||
opts.message = `${fromCurr} to ${toCurr} exchange rate got:\n${newValue}`;
|
||||
}
|
||||
|
||||
browser.notifications.create(opts);
|
||||
};
|
Loading…
Reference in New Issue
Block a user