diff --git a/js/api.js b/js/api.js index 7e1c6ea..66498f1 100644 --- a/js/api.js +++ b/js/api.js @@ -862,14 +862,21 @@ const FEATURE_WIDGETS = { на ранней загрузке (ДО построения сайдбара/виджетов) — против мигания (FOUC), затем обновляется по свежему /api/features. */ function _applyFeatureCss(feats) { - if (!feats) return; const sels = []; - for (const [key, hrefs] of Object.entries(FEATURE_HREFS)) { - if (feats[key] === false) { - hrefs.forEach(h => sels.push(`[href="${h}"]`)); - (FEATURE_WIDGETS[key] || []).forEach(s => sels.push(s)); + if (feats) { + for (const [key, hrefs] of Object.entries(FEATURE_HREFS)) { + if (feats[key] === false) { + hrefs.forEach(h => sels.push(`[href="${h}"]`)); + (FEATURE_WIDGETS[key] || []).forEach(s => sels.push(s)); + } } } + // Скрытые exam-prep треки (подготовка): кэш хрефов с прошлой загрузки — против мигания. + // /api/exam-prep/tracks асинхронен, поэтому держим точный список скрытых ссылок в кэше. + try { + JSON.parse(localStorage.getItem('ls_examhide') || '[]') + .forEach(h => sels.push(`[href="${h}"]`)); + } catch { /* пусто */ } const css = sels.length ? sels.join(',') + '{display:none !important}' : ''; let el = document.getElementById('ls-feat-hide'); if (!el) { @@ -879,13 +886,13 @@ function _applyFeatureCss(feats) { } el.textContent = css; // Геймификация: класс на (доступен раньше body) → kill-switch без мигания. - document.documentElement.classList.toggle('no-gamification', feats.gamification === false); + if (feats) document.documentElement.classList.toggle('no-gamification', feats.gamification === false); } /* Ранняя синхронная попытка из кэша прошлой загрузки — нет мигания на повторных заходах. (FEATURE_HREFS — const, поэтому этот вызов идёт ПОСЛЕ его объявления.) */ try { const _cachedFeats = JSON.parse(localStorage.getItem('ls_feat_cache') || 'null'); - if (_cachedFeats) _applyFeatureCss(_cachedFeats); + _applyFeatureCss(_cachedFeats); // применит и кэш фич, и кэш скрытых exam-prep ссылок } catch { /* нет кэша / приватный режим — просто ждём async */ } /* Прячет группы сайдбара (.sb-group), у которых не осталось ни одного видимого пункта, @@ -942,7 +949,6 @@ async function hideDisabledFeatures() { document.querySelector('[onclick*="tab-account"]')?.click(); } } - hideEmptySidebarGroups(); // после обновления видимости пунктов — спрятать пустые группы // Exam-prep track links (/exam-prep/): показываем только включённые // (exam_tracks.enabled) и доступные пользователю треки. /api/exam-prep/tracks @@ -952,10 +958,16 @@ async function hideDisabledFeatures() { try { const data = await apiFetch('/api/exam-prep/tracks'); const allowed = new Set((data.tracks || []).map(t => t.exam_key)); + // Собираем точные хрефы скрытых треков и кэшируем — чтобы на СЛЕДУЮЩЕЙ загрузке + // _applyFeatureCss спрятал их синхронно из кэша ещё до сборки сайдбара (без мигания). + const hide = []; examLinks.forEach(el => { - const m = (el.getAttribute('href') || '').match(/^\/exam-prep\/([^/?#]+)/); - if (m && !allowed.has(m[1])) el.style.display = 'none'; + const href = el.getAttribute('href') || ''; + const m = href.match(/^\/exam-prep\/([^/?#]+)/); + if (m && !allowed.has(m[1])) hide.push(href); }); + try { localStorage.setItem('ls_examhide', JSON.stringify(hide)); } catch {} + _applyFeatureCss(_featuresCache); // обновить