<script src="main.js"></srcipt> -> <script src="main.js?1234567890"></srcipt>
const makeTree = (root) => {
const tree = {};
const step = (root, branch) => {
root.querySelectorAll(':scope > [class^="block"]').forEach((element, index) => {
const className = [...element.classList.values()].find(
(className) => className.startsWith('block')
) ?? element.className;
const key = `${className}_${index + 1}`;
branch[key] = {};
const control = element.querySelector(':scope > input');
if (control) {
branch[key].value = control.value;
}
step(element, branch[key]);
});
};
step(root, tree);
return tree;
};
const tree = makeTree(document.body);
console.log(tree);
const makeTree = (root) => {
const tree = {};
const step = (root, branch) => {
root.querySelectorAll(':scope > [class^="block"]').forEach((element, index) => {
const className = [...element.classList.values()].find(
(className) => className.startsWith('block')
) ?? element.className;
const key = `${className}_${index + 1}`;
const controls = element.querySelectorAll(':scope > input');
branch[key] = {
values: [...controls].reduce((acc, control) => {
if (control.id) {
acc[control.id] = control.value;
}
return acc;
}, {})
};
step(element, branch[key]);
});
};
step(root, tree);
return tree;
};
const tree = makeTree(document.body);
console.log(tree);
main.js
или main.ts
импортируете его:import '@fontsource/source-sans-pro';
import '@fontsource/source-sans-pro/cyrillic.css';
head
:+ function applyTheme() {
+ const isDark = (localStorage.getItem('is-dark-theme') ?? 'true') === 'true';
+ if (isDark) {
+ document.body.classList.remove('light-mode');
+ document.body.classList.add('dark-mode');
+ } else {
+ document.body.classList.remove('dark-mode');
+ document.body.classList.add('light-mode');
+ }
+ }
- var dark = true;
function themeChange() {
- var element = document.body;
-
- if (dark == true) {
- element.classList.remove("dark-mode");
- element.classList.add("light-mode");
- dark = false;
- }
-
- else {
- element.classList.remove("light-mode");
- element.classList.add("dark-mode");
- dark = true;
- }
+ localStorage.setItem('is-dark-theme', !((localStorage.getItem('is-dark-theme') ?? 'true') === 'true'));
+ applyTheme();
}
+ window.addEventListener('DOMContentLoaded', function () {
+ applyTheme();
+ });