store data in localStorage

This commit is contained in:
Thomas Rupprecht 2022-12-25 02:33:24 +01:00
parent 4f717f7c80
commit fc2ad7afad
2 changed files with 13 additions and 11 deletions

View file

@ -45,7 +45,8 @@ async function fetchNewData() {
await browser.browserAction.setBadgeText({text: badgeText}); await browser.browserAction.setBadgeText({text: badgeText});
await browser.browserAction.setBadgeBackgroundColor({color: badgeBgColor}); await browser.browserAction.setBadgeBackgroundColor({color: badgeBgColor});
if (window.spaceApi && window.spaceApi.state.open !== spaceApiJson.state.open) { const spaceApi = JSON.parse(localStorage.getItem('spaceApi'));
if (spaceApi && spaceApi.state.open !== spaceApiJson.state.open) {
const state = browser.i18n.getMessage(spaceApiJson.state.open ? 'open' : 'closed'); const state = browser.i18n.getMessage(spaceApiJson.state.open ? 'open' : 'closed');
await browser.notifications.create('status-change', { await browser.notifications.create('status-change', {
type: 'basic', type: 'basic',
@ -56,8 +57,8 @@ async function fetchNewData() {
} }
} }
window.calendar = calendarJson; localStorage.setItem('calendar', JSON.stringify(calendarJson));
window.spaceApi = spaceApiJson; localStorage.setItem('spaceApi', JSON.stringify(spaceApiJson));
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }

View file

@ -49,20 +49,21 @@ async function init() {
linkElement.addEventListener('click', linkElementClickListener); linkElement.addEventListener('click', linkElementClickListener);
}); });
try { const calendar = JSON.parse(localStorage.getItem('calendar'));
const page = await browser.runtime.getBackgroundPage(); const spaceApi = JSON.parse(localStorage.getItem('spaceApi'));
updateNextEvent(page.calendar); if (calendar) {
updateSpaceApiJson(page.spaceApi); updateNextEvent(calendar);
updateState(page.spaceApi); }
} catch (error) { if (spaceApi) {
console.error(error); updateSpaceApiJson(spaceApi);
updateState(spaceApi);
} }
} }
init(); init();
/** /**
* @param {object} nextEvents * @param {Array<object>} nextEvents
*/ */
function updateNextEvent(nextEvents) { function updateNextEvent(nextEvents) {
const calendarElement = document.getElementById('calendar'); const calendarElement = document.getElementById('calendar');