forked from XimeX/usrspace-browser-addon
change to normal function and use async/await
This commit is contained in:
parent
7aaf0d9840
commit
05d2d257e2
|
@ -1,20 +1,20 @@
|
|||
const fetchJson = (url) => {
|
||||
function fetchJson(url) {
|
||||
return fetch(url).then((response) => (response.json()));
|
||||
};
|
||||
}
|
||||
|
||||
const fetchCalendar = (days = 28) => {
|
||||
function fetchCalendar(days = 28) {
|
||||
let url = `${Config.calenderUrl}?o=json`;
|
||||
if (days) {
|
||||
url += `&r=${days}`;
|
||||
}
|
||||
return fetchJson(url);
|
||||
};
|
||||
}
|
||||
|
||||
const fetchSpaceApi = () => {
|
||||
function fetchSpaceApi() {
|
||||
return fetchJson(Config.spaceApiUrl);
|
||||
};
|
||||
}
|
||||
|
||||
const updateBadge = (open) => {
|
||||
async function updateBadge(open) {
|
||||
let badgeText, badgeColor;
|
||||
if (open) {
|
||||
badgeText = browser.browserAction.setBadgeText({text: 'open'});
|
||||
|
@ -23,56 +23,58 @@ const updateBadge = (open) => {
|
|||
badgeText = browser.browserAction.setBadgeText({text: ''});
|
||||
badgeColor = browser.browserAction.setBadgeBackgroundColor({color: null});
|
||||
}
|
||||
Promise.all([badgeText, badgeColor])
|
||||
.then()
|
||||
.catch((error) => {
|
||||
try {
|
||||
await Promise.all([badgeText, badgeColor]);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const sendNotification = (open) => {
|
||||
function 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) => {
|
||||
async function fetchNewData() {
|
||||
try {
|
||||
const json = await fetchCalendar();
|
||||
window.calendar = json;
|
||||
}).catch((error) => {
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
|
||||
fetchSpaceApi().then((json) => {
|
||||
try {
|
||||
const json = await fetchSpaceApi();
|
||||
if (window.spaceApi && window.spaceApi.state.open !== json.state.open) {
|
||||
sendNotification(json.state.open)
|
||||
}
|
||||
window.spaceApi = json;
|
||||
updateBadge(window.spaceApi.state.open);
|
||||
}).catch((error) => {
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
let intervalHandler = null;
|
||||
|
||||
const stopFetching = () => {
|
||||
function stopFetching() {
|
||||
if (intervalHandler !== null) {
|
||||
clearInterval(intervalHandler);
|
||||
intervalHandler = null;
|
||||
}
|
||||
};
|
||||
const startFetching = () => {
|
||||
}
|
||||
function startFetching() {
|
||||
fetchNewData();
|
||||
if (intervalHandler === null) {
|
||||
intervalHandler = setInterval(() => {
|
||||
fetchNewData();
|
||||
}, Config.refreshTimeout);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
window.addEventListener('offline', () => {
|
||||
stopFetching();
|
||||
|
|
|
@ -1,25 +1,32 @@
|
|||
Array.from(document.getElementsByClassName('link')).forEach((element) => {
|
||||
element.addEventListener('click', (event) => {
|
||||
const newTab = browser.tabs.create({
|
||||
url: event.currentTarget.dataset.url
|
||||
});
|
||||
newTab.then((data) => {
|
||||
// console.log(data);
|
||||
}).catch((error) => {
|
||||
async function linkElementClickListener(event) {
|
||||
try {
|
||||
const tab = await browser.tabs.create({url: event.currentTarget.dataset.url});
|
||||
// console.log(tab);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function init() {
|
||||
const linkElements = Array.from(document.getElementsByClassName('link'));
|
||||
linkElements.forEach((linkElement) => {
|
||||
linkElement.addEventListener('click', linkElementClickListener);
|
||||
});
|
||||
|
||||
browser.runtime.getBackgroundPage().then((page) => {
|
||||
try {
|
||||
const page = await browser.runtime.getBackgroundPage();
|
||||
console.log(page);
|
||||
|
||||
updateNextEvent(page.calendar);
|
||||
updateSpaceApiJson(page.spaceApi);
|
||||
updateState(page.spaceApi);
|
||||
}).catch((error) => {
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
init();
|
||||
|
||||
const updateNextEvent = (nextEvents) => {
|
||||
function updateNextEvent(nextEvents) {
|
||||
const calendarElement = document.getElementById('calendar');
|
||||
calendarElement.innerText = '';
|
||||
|
||||
|
@ -54,18 +61,18 @@ const updateNextEvent = (nextEvents) => {
|
|||
}
|
||||
calendarElement.append(divElement);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
const updateSpaceApiJson = (spaceApi) => {
|
||||
function updateSpaceApiJson(spaceApi) {
|
||||
const spaceApiElement = document.querySelector('#space-api code');
|
||||
const json = JSON.stringify(spaceApi, null, 2).replace(/ /g, ' ').replace(/\n/g, '<br/>');
|
||||
spaceApiElement.innerHTML = json;
|
||||
// const jsonNode = document.createTextNode(json);
|
||||
// spaceApiElement.innerText = '';
|
||||
// spaceApiElement.append(jsonNode);
|
||||
};
|
||||
}
|
||||
|
||||
const updateState = (spaceApi) => {
|
||||
function updateState(spaceApi) {
|
||||
const stateElement = document.getElementById('state');
|
||||
const since = new Date(spaceApi.state.lastchange * 1000);
|
||||
const sinceStr = ` seit <time datetime="${since.toISOString()}">${since.toLocaleString([], {dateStyle: "short", timeStyle: "short"})}</time>`;
|
||||
|
@ -74,4 +81,4 @@ const updateState = (spaceApi) => {
|
|||
} else {
|
||||
stateElement.innerHTML = `<img src="../icons/font-awesome/lock-solid.svg" width="19" height="19" alt="Geschlossen" />Geschlossen ${sinceStr}`;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue