2023-01-06 02:02:50 +01:00
|
|
|
import { REFRESH_TIMEOUT, API_URLS, BADGE_COLORS } from './config.js';
|
2023-05-24 19:42:58 +02:00
|
|
|
// [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/',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-20 17:12:49 +01:00
|
|
|
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();
|
2021-09-22 22:49:30 +02:00
|
|
|
}
|
2018-12-27 10:37:31 +01:00
|
|
|
|
2022-12-24 13:50:28 +01:00
|
|
|
/**
|
|
|
|
* @param {number} days
|
2023-02-04 18:03:49 +01:00
|
|
|
* @returns {Promise<import("../types").Calendar>}
|
2022-12-24 13:50:28 +01:00
|
|
|
*/
|
2021-09-22 22:49:30 +02: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);
|
2021-09-22 22:49:30 +02:00
|
|
|
}
|
2018-12-27 10:37:31 +01:00
|
|
|
|
2022-12-24 13:50:28 +01:00
|
|
|
/**
|
2023-02-04 18:03:49 +01:00
|
|
|
* @returns {Promise<import("../types").SpaceApi>}
|
2022-12-24 13:50:28 +01:00
|
|
|
*/
|
2021-09-22 22:49:30 +02:00
|
|
|
function fetchSpaceApi() {
|
2023-01-06 02:02:50 +01:00
|
|
|
return fetchJson(API_URLS.spaceApi);
|
2021-09-22 22:49:30 +02:00
|
|
|
}
|
2018-12-27 10:37:31 +01:00
|
|
|
|
2021-09-22 22:49:30 +02:00
|
|
|
async function fetchNewData() {
|
2023-01-21 02:10:50 +01:00
|
|
|
if (!globalThis.navigator.onLine) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-22 22:49:30 +02:00
|
|
|
try {
|
2023-02-04 18:05:47 +01:00
|
|
|
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
|
|
|
|
2022-12-25 01:26:20 +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
|
|
|
|
2023-01-20 17:42:45 +01:00
|
|
|
await setBadgeStatus(spaceApiJson.state.open, eventActive);
|
2022-12-24 15:21:06 +01:00
|
|
|
|
2023-02-04 18:03:49 +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) {
|
2023-01-20 17:42:45 +01:00
|
|
|
await createStatusChangedNotification(spaceApiJson.state.open);
|
2022-12-25 01:26:20 +01:00
|
|
|
}
|
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,
|
|
|
|
});
|
2021-09-22 22:49:30 +02:00
|
|
|
} catch (error) {
|
2021-09-22 21:04:32 +02:00
|
|
|
console.error(error);
|
2021-09-22 22:49:30 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-27 10:37:31 +01:00
|
|
|
|
2023-01-20 17:42:45 +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 });
|
2023-01-20 17:42:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-20 17:12:49 +01:00
|
|
|
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
|
|
|
});
|
2021-09-22 22:49:30 +02:00
|
|
|
}
|
2019-08-28 21:50:30 +02:00
|
|
|
|
2023-01-20 17:12:49 +01:00
|
|
|
init();
|