const sprintf = (...[string, ...args]) => { return string.replace(/{(\d+)}/g, (match, number) => args[number] ?? match); } const getJSON = async url => { const resp = await fetch(url) const json = await resp.json() return json; } const createPopup = async id => { const asics = getJSON('/api/asictypes') asicoptions = '' asics.forEach(elem => { asicoptions += sprintf('', elem['key'], elem['value']) }) document.querySelector('body').innerHTML += sprintf(popup, id, asicoptions, 'content') } const deletePopup = id => { document.querySelector('#popup-' + id).remove() } const update = () => { // console.log(getJSON('/api/')) } const datafn = async () => { // return await getJSON('/api/webinit') } const data = datafn() console.log(data) const cols = 40 const rows = 40 const css = ` ` const header = '
' + '
' + 'Всего устройств: {0}, Предупреждений, {1}, Ошибки: {2}, В сети: {3}, Не в сети: {4}' + '
' + '
' const cell = '
' + '' + '
' const grid = '
' + '{0}' + '
' const popup = '' const toNodes = html => { return new DOMParser().parseFromString(html, 'text/html').body } const getStats = async () => { let cells = '' let info = await getJSON('/api/getstats') for(let i = 0; i < cols * rows; i++) { if(info.asics[i]) // FIND!!! cells += sprintf(cell, i, 'green') else cells += sprintf(cell, i) } return cells } const run = async () => { let inner = sprintf(grid, await getStats()) document.querySelector('body').innerHTML += css document.querySelector('body').innerHTML += sprintf(header) document.querySelector('body').innerHTML += inner update() setInterval(() => { update() }, 10000) } run()