68 lines
1.6 KiB
JavaScript
68 lines
1.6 KiB
JavaScript
const fetchJson = (url) => {
|
|
return fetch(url).then((response) => (response.json()));
|
|
};
|
|
|
|
const fetchCalendar = (days = null) => {
|
|
let url = Config.calenderUrl + '?o=json';
|
|
if (days) {
|
|
url += '&r=' + days;
|
|
}
|
|
return fetchJson(url);
|
|
};
|
|
|
|
const fetchSpaceApi = () => {
|
|
return fetchJson(Config.spaceApiUrl);
|
|
};
|
|
|
|
const updateBadge = (open) => {
|
|
let badgeText, badgeColor;
|
|
if (open) {
|
|
badgeText = browser.browserAction.setBadgeText({text: 'open'});
|
|
badgeColor = browser.browserAction.setBadgeBackgroundColor({color: Config.openColor});
|
|
} else {
|
|
badgeText = browser.browserAction.setBadgeText({text: ''});
|
|
badgeColor = browser.browserAction.setBadgeBackgroundColor({color: null});
|
|
}
|
|
badgeText.then(() => {
|
|
|
|
}).catch((error) => {
|
|
console.error(error);
|
|
});
|
|
badgeColor.then(() => {
|
|
|
|
}).catch((error) => {
|
|
console.error(error);
|
|
});
|
|
};
|
|
|
|
const sendNotification = (open) => {
|
|
browser.notifications.create('notification-id', {
|
|
type: 'basic',
|
|
iconUrl: browser.runtime.getURL('icons/logo.svg'),
|
|
title: 'Space Status',
|
|
message: 'Space ist jetzt ' + (open ? 'offen' : 'geschlossen') + '.'
|
|
});
|
|
};
|
|
|
|
const fetchNewData = () => {
|
|
fetchCalendar().then((json) => {
|
|
window.calendar = json;
|
|
}).catch((error) => {
|
|
console.error(error);
|
|
});
|
|
|
|
fetchSpaceApi().then((json) => {
|
|
if (window.spaceApi && window.spaceApi.state.open !== json.state.open) {
|
|
sendNotification(json.state.open)
|
|
}
|
|
window.spaceApi = json;
|
|
updateBadge(window.spaceApi.state.open);
|
|
}).catch((error) => {
|
|
console.error(error);
|
|
});
|
|
};
|
|
|
|
fetchNewData();
|
|
setInterval(() => {
|
|
fetchNewData();
|
|
}, Config.refreshTimeout); |