forked from XimeX/usrspace-browser-addon
extract code into setBadgeStatus and createStatusChangedNotification function
This commit is contained in:
parent
b1dc1a0d21
commit
ad9abe3c94
|
@ -60,20 +60,11 @@ async function fetchNewData() {
|
||||||
new Date(event.begin) <= now && now <= new Date(event.end)
|
new Date(event.begin) <= now && now <= new Date(event.end)
|
||||||
)) ?? false;
|
)) ?? false;
|
||||||
|
|
||||||
const badgeText = browser.i18n.getMessage(spaceApiJson.state.open ? 'badgeOpen' : eventActive ? 'badgeEvent' : 'badgeClosed');
|
await setBadgeStatus(spaceApiJson.state.open, eventActive);
|
||||||
const badgeBgColor = spaceApiJson.state.open ? BADGE_COLORS.open : eventActive ? BADGE_COLORS.event : BADGE_COLORS.closed;
|
|
||||||
await browser.action.setBadgeText({text: badgeText});
|
|
||||||
await browser.action.setBadgeBackgroundColor({color: badgeBgColor});
|
|
||||||
|
|
||||||
const {spaceApi} = await browser.storage.local.get('spaceApi');
|
const {spaceApi} = await browser.storage.local.get('spaceApi');
|
||||||
if (spaceApi && spaceApi.state.open !== spaceApiJson.state.open) {
|
if (spaceApi && spaceApi.state.open !== spaceApiJson.state.open) {
|
||||||
const state = browser.i18n.getMessage(spaceApiJson.state.open ? 'open' : 'closed');
|
await createStatusChangedNotification(spaceApiJson.state.open);
|
||||||
await browser.notifications.create('status-change', {
|
|
||||||
type: 'basic',
|
|
||||||
title: browser.i18n.getMessage('stateNotificationTitle'),
|
|
||||||
message: browser.i18n.getMessage('stateNotificationMessage', state),
|
|
||||||
iconUrl: browser.runtime.getURL('icons/favicon.svg'),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,6 +77,30 @@ async function fetchNewData() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {boolean} open
|
||||||
|
* @param {boolean} eventActive
|
||||||
|
*/
|
||||||
|
async function setBadgeStatus(open, eventActive) {
|
||||||
|
const badgeText = browser.i18n.getMessage(open ? 'badgeOpen' : eventActive ? 'badgeEvent' : 'badgeClosed');
|
||||||
|
const badgeBgColor = open ? BADGE_COLORS.open : eventActive ? BADGE_COLORS.event : BADGE_COLORS.closed;
|
||||||
|
await browser.action.setBadgeText({text: badgeText});
|
||||||
|
await browser.action.setBadgeBackgroundColor({color: badgeBgColor});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {boolean} open
|
||||||
|
*/
|
||||||
|
async function createStatusChangedNotification(open) {
|
||||||
|
const stateL10n = browser.i18n.getMessage(open ? 'open' : 'closed');
|
||||||
|
await browser.notifications.create('status-changed', {
|
||||||
|
type: 'basic',
|
||||||
|
title: browser.i18n.getMessage('stateNotificationTitle'),
|
||||||
|
message: browser.i18n.getMessage('stateNotificationMessage', stateL10n),
|
||||||
|
iconUrl: browser.runtime.getURL('icons/favicon.svg'),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
fetchNewData();
|
fetchNewData();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue