refactor(popup): improve init of popup script and simplify `setLinks`

This commit is contained in:
Thomas Rupprecht 2023-09-14 02:13:49 +02:00
parent fe31d9c6ec
commit 52aeb58698
2 changed files with 35 additions and 25 deletions

View File

@ -2,6 +2,12 @@
All notable changes to this project will be documented in this file.
## \[unreleased]
### ⛰️ Features
- Add `.cjs` and `.mjs` to `.editorconfig`
## \[[0.9.5](https://git.usrspace.at/XimeX/usrspace-browser-addon/releases/tag/v0.9.5)] - 2023-09-13
### 🐛 Bug Fixes

View File

@ -3,6 +3,32 @@ import { QUICK_LINKS } from './config.js';
const dateTimeFormat = Intl.DateTimeFormat([], { dateStyle: 'medium', timeStyle: 'short' });
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', async () => {
await init();
});
} else {
await init();
}
async function init() {
setL10n();
setLinks();
/**
* @type {import("../types").Storage}
*/
const { calendar, spaceApi } = await browser.storage.session.get(['calendar', 'spaceApi']);
if (calendar) {
updateNextEvent(calendar);
}
if (spaceApi) {
updateSpaceApiJson(spaceApi);
updateState(spaceApi);
}
}
function setL10n() {
document.querySelector('html').setAttribute('lang', browser.i18n.getUILanguage());
@ -22,7 +48,9 @@ function setLinks() {
for (const quickLink of QUICK_LINKS) {
const template = getCleanTemplateById('template-link-item');
template.querySelector('.link').dataset.url = quickLink.url;
const linkElement = template.querySelector('.link')
linkElement.dataset.url = quickLink.url;
linkElement.addEventListener('click', linkElementClickListener);
const svgIcon = getCleanTemplateById(quickLink.iconTemplateId);
svgIcon.querySelector('svg').setAttribute('aria-label', quickLink.text);
@ -45,30 +73,6 @@ async function linkElementClickListener(event) {
}
}
async function init() {
setL10n();
setLinks();
const linkElements = document.querySelectorAll('.link');
for (const linkElement of linkElements) {
linkElement.addEventListener('click', linkElementClickListener);
}
/**
* @type {import("../types").Storage}
*/
const { calendar, spaceApi } = await browser.storage.session.get(['calendar', 'spaceApi']);
if (calendar) {
updateNextEvent(calendar);
}
if (spaceApi) {
updateSpaceApiJson(spaceApi);
updateState(spaceApi);
}
}
await init();
/**
* @param {import("../types").Calendar} nextEvents
*/