usrspace-browser-addon/src/background.js

122 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-01-06 02:02:50 +01:00
import { REFRESH_TIMEOUT, API_URLS, BADGE_COLORS } from './config.js';
// [INJECT-BROWSER-POLYFILL]
2023-01-04 23:21:40 +01:00
2023-01-20 16:06:17 +01:00
browser.runtime.onInstalled.addListener(async (details) => {
if (details.reason === 'install') {
await browser.tabs.create({
url: 'https://www.usrspace.at/',
});
}
});
browser.runtime.onStartup.addListener(async () => {
await browser.storage.local.remove(['calendar', 'spaceApi']);
});
browser.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'fetchData') {
fetchNewData();
}
});
2022-12-24 13:50:28 +01:00
/**
* @param {string} url
* @returns {Promise<any>}
*/
async function fetchJson(url) {
const response = await fetch(url);
return response.json();
}
2018-12-27 10:37:31 +01:00
2022-12-24 13:50:28 +01:00
/**
* @param {number} days
* @returns {Promise<import("../types").Calendar>}
2022-12-24 13:50:28 +01:00
*/
function fetchCalendar(days = 28) {
2023-01-06 02:02:50 +01:00
let url = `${API_URLS.calender}?o=json`;
2021-09-22 21:04:32 +02:00
if (days) {
2021-09-22 21:36:54 +02:00
url += `&r=${days}`;
2021-09-22 21:04:32 +02:00
}
return fetchJson(url);
}
2018-12-27 10:37:31 +01:00
2022-12-24 13:50:28 +01:00
/**
* @returns {Promise<import("../types").SpaceApi>}
2022-12-24 13:50:28 +01:00
*/
function fetchSpaceApi() {
2023-01-06 02:02:50 +01:00
return fetchJson(API_URLS.spaceApi);
}
2018-12-27 10:37:31 +01:00
async function fetchNewData() {
2023-01-21 02:10:50 +01:00
if (!globalThis.navigator.onLine) {
return;
}
try {
const [resultCalendar, resultSpaceApi] = await Promise.allSettled([fetchCalendar(), fetchSpaceApi()]);
const calendarJson = (resultCalendar.status === 'fulfilled') ? resultCalendar.value : undefined;
const spaceApiJson = (resultSpaceApi.status === 'fulfilled') ? resultSpaceApi.value : undefined;
2018-12-27 10:37:31 +01:00
if (spaceApiJson) {
const now = new Date();
const eventActive = calendarJson?.some((event) => (
new Date(event.begin) <= now && now <= new Date(event.end)
)) ?? false;
2022-12-24 15:21:06 +01:00
await setBadgeStatus(spaceApiJson.state.open, eventActive);
2022-12-24 15:21:06 +01:00
/**
* @type {import("../types").Storage}
*/
2023-02-04 17:09:47 +01:00
const { spaceApi } = await browser.storage.local.get('spaceApi');
2022-12-25 02:33:24 +01:00
if (spaceApi && spaceApi.state.open !== spaceApiJson.state.open) {
await createStatusChangedNotification(spaceApiJson.state.open);
}
2021-09-22 21:04:32 +02:00
}
2022-12-24 15:21:06 +01:00
2022-12-25 02:56:18 +01:00
await browser.storage.local.set({
calendar: calendarJson,
spaceApi: spaceApiJson,
});
} catch (error) {
2021-09-22 21:04:32 +02:00
console.error(error);
}
}
2018-12-27 10:37:31 +01:00
/**
* @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;
2023-02-04 17:09:47 +01:00
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() {
2021-09-22 21:04:32 +02:00
fetchNewData();
2022-12-25 16:45:34 +01:00
const now = new Date();
2023-01-06 02:02:50 +01:00
now.setMinutes(Math.ceil((now.getMinutes() + 1) / REFRESH_TIMEOUT) * REFRESH_TIMEOUT, 0, 0);
2022-12-25 16:45:34 +01:00
browser.alarms.create('fetchData', {
when: now.getTime(),
2023-01-06 02:02:50 +01:00
periodInMinutes: REFRESH_TIMEOUT,
2022-12-25 16:45:34 +01:00
});
}
init();