mirror of
https://github.com/chylex/Firefox-SCsCC.git
synced 2025-04-09 16:15:43 +02:00
add exchange rate pattern & handle undefined currRate value
This commit is contained in:
parent
f616cc3281
commit
9387e629a9
src
background
content
options
popup
@ -69,7 +69,7 @@ browser.storage.local.get()
|
||||
const newStorage = {};
|
||||
|
||||
if (storage.preferences && Object.keys(storage.preferences).length === Object.keys(preferences).length) {
|
||||
preferences = storage.preferences;
|
||||
({ preferences } = storage);
|
||||
} else {
|
||||
preferences = Object.assign({}, preferences, storage.preferences || {});
|
||||
newStorage.preferences = preferences;
|
||||
@ -89,7 +89,7 @@ browser.storage.local.get()
|
||||
currRates = newCurrRates;
|
||||
newStorage.currRates = newCurrRates;
|
||||
} else {
|
||||
currRates = storage.currRates;
|
||||
({ currRates } = storage);
|
||||
}
|
||||
} else {
|
||||
newStorage.currRates = {};
|
||||
@ -121,7 +121,7 @@ const checkCurrRate = (currRate, fromCurr, toCurr) => {
|
||||
const reqKey = `${fromCurr}to${toCurr}`;
|
||||
|
||||
if (!currRates[reqKey] || currRates[reqKey].value !== currRate.value || currRates[reqKey].updatedAt !== currRate.updatedAt) {
|
||||
if (preferences.noti) {
|
||||
if (preferences.noti && currRate.value) {
|
||||
const oldValue = (currRates[reqKey] && currRates[reqKey].value) || null;
|
||||
showNotification(fromCurr, toCurr, oldValue, currRate.value);
|
||||
}
|
||||
|
@ -1,5 +1,19 @@
|
||||
const requests = {};
|
||||
|
||||
const patterns = [
|
||||
/id=['"]?exchange_rate['"]?.*value=['"]?(\d+\.\d+)['"]?/i,
|
||||
/id=['"]?knowledge-currency__tgt-input['"]?.*value=['"]?(\d+\.\d+)['"]?/i,
|
||||
];
|
||||
|
||||
const getTxtMatch = (txt) =>
|
||||
patterns.reduce((txtMatch, pattern) => {
|
||||
if (txtMatch) return txtMatch;
|
||||
|
||||
const match = txt.match(pattern);
|
||||
if (match && match[1]) return match[1];
|
||||
return '';
|
||||
}, '');
|
||||
|
||||
// on getCurrRate request complete
|
||||
const reqComplete = (request, currRates, reqKey) => {
|
||||
const currRate = currRates[reqKey] ? Object.assign({}, currRates[reqKey]) : {};
|
||||
@ -7,14 +21,14 @@ const reqComplete = (request, 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);
|
||||
const txtMatch = getTxtMatch(request.responseText);
|
||||
const newValue = parseFloat(txtMatch);
|
||||
|
||||
if (txtMatch && txtMatch[1]) {
|
||||
const newValue = parseFloat(txtMatch[1]);
|
||||
|
||||
if (!Number.isNaN(newValue) && newValue !== currRate.value) {
|
||||
currRate.value = newValue;
|
||||
}
|
||||
if (!Number.isNaN(newValue)) {
|
||||
currRate.value = newValue;
|
||||
} else if (!currRate.value) {
|
||||
// will try again if requested after 10 minues
|
||||
currRate.updatedAt = Date.now() - 3000000;
|
||||
}
|
||||
} else {
|
||||
// will try again if requested after 10 minues
|
||||
@ -29,7 +43,7 @@ export default (currRates, fromCurr, 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) {
|
||||
if (currRates[reqKey] && currRates[reqKey].updatedAt && Date.now() - currRates[reqKey].updatedAt < 3600000) {
|
||||
return Promise.resolve(currRates[reqKey]);
|
||||
}
|
||||
|
||||
|
@ -10,9 +10,9 @@ export default (fromCurr, toCurr, oldValue, newValue) => {
|
||||
iconUrl: browser.runtime.getURL(manifest.icons[48]),
|
||||
};
|
||||
|
||||
if (oldValue) { // on update
|
||||
if (oldValue) { // on update
|
||||
opts.message = `${fromCurr} to ${toCurr} exchange rate updated:\n${oldValue} → ${newValue}`;
|
||||
} else { // on frist get
|
||||
} else { // on frist get
|
||||
opts.message = `${fromCurr} to ${toCurr} exchange rate got:\n${newValue}`;
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ const replaceText = ({ node, matches }) => {
|
||||
const dataNodes = getDataNodes(node, matches);
|
||||
if (!dataNodes.length) return;
|
||||
|
||||
const parentNode = node.parentNode;
|
||||
const { parentNode } = node;
|
||||
|
||||
const tmpDivElem = document.createElement('div');
|
||||
tmpDivElem.appendChild(node.cloneNode());
|
||||
|
@ -23,7 +23,7 @@ browser.storage.onChanged.addListener((changes) => {
|
||||
|
||||
const get = (fromCurr, toCurr) => {
|
||||
const reqKey = `${fromCurr}to${toCurr}`;
|
||||
const currRate = currRates[reqKey] ? currRates[reqKey].value : null;
|
||||
const currRate = currRates[reqKey] ? currRates[reqKey].value : undefined;
|
||||
|
||||
if (!requests[reqKey]) {
|
||||
const data = { from: fromCurr, to: toCurr };
|
||||
|
@ -42,7 +42,7 @@ const checkSiblingMatches = (textNode, toCurr, { numPatt, symbPatts }) => {
|
||||
chckTxt.prev = parentPrevElemSibling.lastChild.nodeValue.trim();
|
||||
}
|
||||
|
||||
const nextSibling = textNode.nextSibling;
|
||||
const { nextSibling } = textNode;
|
||||
const parentNextSibling = textNode.parentNode.nextSibling;
|
||||
const parentNextElemSibling = textNode.parentNode.nextElementSibling;
|
||||
// check next sibling of
|
||||
|
@ -3,8 +3,7 @@ import getButtonRowElem from './utils/getButtonRowElem';
|
||||
import getChangedPrefs from './utils/getChangedPrefs';
|
||||
|
||||
|
||||
const onChange = (event) => {
|
||||
const target = event.target;
|
||||
const onChange = ({ target }) => {
|
||||
const changedPrefs = getChangedPrefs(target);
|
||||
|
||||
if (Object.keys(changedPrefs).length) {
|
||||
|
@ -17,6 +17,9 @@ body {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.container h1 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.row {
|
||||
margin-bottom: 5px;
|
||||
|
@ -30,7 +30,7 @@ const refreshCurrRatesList = (currRates) => {
|
||||
Object.keys(currRates).forEach((currKey) => {
|
||||
const currRate = currRates[currKey];
|
||||
const updatedTxt = getUpdatedTxt(currRate.updatedAt);
|
||||
const rateLiElem = getRateLiElem(currKey, currRate.value, updatedTxt);
|
||||
const rateLiElem = getRateLiElem(currKey, currRate.value || 'n/a', updatedTxt);
|
||||
|
||||
ratesUlElem.appendChild(rateLiElem);
|
||||
});
|
||||
@ -55,8 +55,7 @@ browser.runtime.getBackgroundPage()
|
||||
return browser.storage.local.get();
|
||||
})
|
||||
.then((storage) => {
|
||||
const enabled = storage.preferences.enabled;
|
||||
const toCurr = storage.preferences.toCurr;
|
||||
const { enabled, toCurr } = storage.preferences;
|
||||
const currRates = storage.currRates || {};
|
||||
|
||||
setState(enabled);
|
||||
@ -65,7 +64,7 @@ browser.runtime.getBackgroundPage()
|
||||
|
||||
browser.storage.onChanged.addListener((changes) => {
|
||||
if (changes.preferences) {
|
||||
const { oldValue, newValue } = changes.preferences.oldValue;
|
||||
const { oldValue, newValue } = changes.preferences;
|
||||
|
||||
if (oldValue.enabled !== newValue.enabled) setState(newValue.enabled);
|
||||
if (oldValue.toCurr !== newValue.toCurr) setToCurr(newValue.toCurr);
|
||||
@ -79,8 +78,7 @@ browser.runtime.getBackgroundPage()
|
||||
|
||||
stateBtnElem.addEventListener('click', () => {
|
||||
browser.storage.local.get('preferences')
|
||||
.then((storage) => {
|
||||
const preferences = storage.preferences;
|
||||
.then(({ preferences }) => {
|
||||
preferences.enabled = !preferences.enabled;
|
||||
|
||||
browser.storage.local.set({ preferences });
|
||||
|
Loading…
Reference in New Issue
Block a user