2022-12-24 13:37:50 +01:00
|
|
|
import Config from "./config.js";
|
2022-12-24 13:08:15 +01:00
|
|
|
|
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
|
2022-12-24 15:21:06 +01:00
|
|
|
* @returns {Promise<object>}
|
2022-12-24 13:50:28 +01:00
|
|
|
*/
|
2021-09-22 22:49:30 +02:00
|
|
|
function fetchCalendar(days = 28) {
|
2021-09-22 21:36:54 +02:00
|
|
|
let url = `${Config.calenderUrl}?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
|
|
|
/**
|
2022-12-24 15:21:06 +01:00
|
|
|
* @returns {Promise<object>}
|
2022-12-24 13:50:28 +01:00
|
|
|
*/
|
2021-09-22 22:49:30 +02:00
|
|
|
function fetchSpaceApi() {
|
2021-09-22 21:04:32 +02:00
|
|
|
return fetchJson(Config.spaceApiUrl);
|
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() {
|
|
|
|
try {
|
2022-12-25 01:26:20 +01:00
|
|
|
const result = await Promise.allSettled([fetchCalendar(), fetchSpaceApi()]);
|
|
|
|
const calendarJson = result[0].value;
|
|
|
|
const spaceApiJson = result[1].value;
|
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
|
|
|
|
2022-12-25 01:26:20 +01:00
|
|
|
const badgeText = browser.i18n.getMessage(spaceApiJson.state.open ? 'badgeOpen' : eventActive ? 'badgeEvent' : 'badgeClosed');
|
|
|
|
const badgeBgColor = spaceApiJson.state.open ? Config.openColor : eventActive ? Config.eventColor : Config.closedColor;
|
|
|
|
await browser.browserAction.setBadgeText({text: badgeText});
|
|
|
|
await browser.browserAction.setBadgeBackgroundColor({color: badgeBgColor});
|
2022-12-24 15:21:06 +01:00
|
|
|
|
2022-12-25 01:26:20 +01:00
|
|
|
if (window.spaceApi && window.spaceApi.state.open !== spaceApiJson.state.open) {
|
|
|
|
const state = browser.i18n.getMessage(spaceApiJson.state.open ? 'open' : 'closed');
|
|
|
|
await browser.notifications.create('status-change', {
|
|
|
|
type: 'basic',
|
|
|
|
title: browser.i18n.getMessage('stateNotificationTitle'),
|
|
|
|
message: browser.i18n.getMessage('stateNotificationMessage', state),
|
2022-12-25 01:52:02 +01:00
|
|
|
iconUrl: browser.runtime.getURL('icons/favicon.svg')
|
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 01:26:20 +01:00
|
|
|
window.calendar = calendarJson;
|
2022-12-24 15:21:06 +01:00
|
|
|
window.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
|
|
|
|
2019-08-28 21:50:30 +02:00
|
|
|
let intervalHandler = null;
|
|
|
|
|
2021-09-22 22:49:30 +02:00
|
|
|
function stopFetching() {
|
2021-09-22 21:04:32 +02:00
|
|
|
if (intervalHandler !== null) {
|
|
|
|
clearInterval(intervalHandler);
|
|
|
|
intervalHandler = null;
|
|
|
|
}
|
2021-09-22 22:49:30 +02:00
|
|
|
}
|
|
|
|
function startFetching() {
|
2021-09-22 21:04:32 +02:00
|
|
|
fetchNewData();
|
|
|
|
if (intervalHandler === null) {
|
|
|
|
intervalHandler = setInterval(() => {
|
|
|
|
fetchNewData();
|
|
|
|
}, Config.refreshTimeout);
|
|
|
|
}
|
2021-09-22 22:49:30 +02:00
|
|
|
}
|
2019-08-28 21:50:30 +02:00
|
|
|
|
|
|
|
window.addEventListener('offline', () => {
|
2021-09-22 21:04:32 +02:00
|
|
|
stopFetching();
|
2019-08-28 21:50:30 +02:00
|
|
|
});
|
|
|
|
window.addEventListener('online', () => {
|
2021-09-22 21:04:32 +02:00
|
|
|
startFetching();
|
2019-08-28 21:50:30 +02:00
|
|
|
});
|
|
|
|
if (window.navigator.onLine) {
|
2021-09-22 21:04:32 +02:00
|
|
|
startFetching();
|
2019-08-28 21:50:30 +02:00
|
|
|
}
|