refactor(popup): improve init of popup script and simplify setLinks
This commit is contained in:
parent
fe31d9c6ec
commit
52aeb58698
|
@ -2,6 +2,12 @@
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
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
|
## \[[0.9.5](https://git.usrspace.at/XimeX/usrspace-browser-addon/releases/tag/v0.9.5)] - 2023-09-13
|
||||||
|
|
||||||
### 🐛 Bug Fixes
|
### 🐛 Bug Fixes
|
||||||
|
|
54
src/popup.js
54
src/popup.js
|
@ -3,6 +3,32 @@ import { QUICK_LINKS } from './config.js';
|
||||||
|
|
||||||
const dateTimeFormat = Intl.DateTimeFormat([], { dateStyle: 'medium', timeStyle: 'short' });
|
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() {
|
function setL10n() {
|
||||||
document.querySelector('html').setAttribute('lang', browser.i18n.getUILanguage());
|
document.querySelector('html').setAttribute('lang', browser.i18n.getUILanguage());
|
||||||
|
|
||||||
|
@ -22,7 +48,9 @@ function setLinks() {
|
||||||
for (const quickLink of QUICK_LINKS) {
|
for (const quickLink of QUICK_LINKS) {
|
||||||
const template = getCleanTemplateById('template-link-item');
|
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);
|
const svgIcon = getCleanTemplateById(quickLink.iconTemplateId);
|
||||||
svgIcon.querySelector('svg').setAttribute('aria-label', quickLink.text);
|
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
|
* @param {import("../types").Calendar} nextEvents
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue