diff --git a/src/background/style-manager/fixer.js b/src/background/style-manager/fixer.js index 0e33908f3a..218e5f2cce 100644 --- a/src/background/style-manager/fixer.js +++ b/src/background/style-manager/fixer.js @@ -13,14 +13,19 @@ const MISSING_PROPS = { const hasVarsAndImport = ({code}) => code.startsWith(':root {\n --') && /@import\s/i.test(code); -export function fixKnownProblems(style, initIndex, initArray) { +/** + * @param {StyleObj} style + * @param {boolean} [revive] + * @return {?StyleObj|Promise} + */ +export function fixKnownProblems(style, revive) { let res = 0; let v; if (!style || typeof style !== 'object') - style = {}; - if (!style.id && initArray) { + return style; + if (!+style.id && revive) { res = new Set(); - for (const x of initArray) if (x) res.add(x.id); + for (const x of revive) if (x) res.add(x.id); for (v = 1, res.has(v); ;) v++; } for (const key in MISSING_PROPS) { @@ -76,7 +81,7 @@ export function fixKnownProblems(style, initIndex, initArray) { if (!style.url) res = style.url = v; if (!style.installationUrl) res = style.installationUrl = v; } - if (initArray && ( + if (revive && ( !Array.isArray(v = style.sections) && (v = 0, true) || /* @import must precede `vars` that we add at beginning */ !isEmptyObj(style[UCD]?.vars) && v.some(hasVarsAndImport) diff --git a/src/background/style-manager/index.js b/src/background/style-manager/index.js index 7dd9634c28..7543429a70 100644 --- a/src/background/style-manager/index.js +++ b/src/background/style-manager/index.js @@ -1,7 +1,7 @@ import {IMPORT_THROTTLE, kUrl, UCD} from '@/js/consts'; import * as prefs from '@/js/prefs'; import {calcStyleDigest, styleCodeEmpty} from '@/js/sections-util'; -import {isEmptyObj, mapObj, sleep} from '@/js/util'; +import {isEmptyObj, mapObj} from '@/js/util'; import {broadcast} from '../broadcast'; import * as colorScheme from '../color-scheme'; import {bgBusy, bgInit, uuidIndex} from '../common'; @@ -28,17 +28,13 @@ bgInit.push(async () => { db.getAll(), styleCache.loadAll(), ]); - (async () => { - await bgBusy; - await sleep(); - __.DEBUGLOG('styleMan fixKnownProblems...'); - const fixed = (await Promise.all(styles.map(fixKnownProblems))).filter(Boolean); - setTimeout(db.putMany, 0, fixed); - })(); setOrderImpl(orderFromDb, {store: false}); - for (const style of styles) - try { storeInMap(style); } catch {} + initStyleMap(styles); styleCache.hydrate(dataMap); + __.DEBUGLOG('styleMan init done'); +}); + +bgBusy.then(() => { colorScheme.onChange(() => { for (const {style} of dataMap.values()) { if (colorScheme.SCHEMES.includes(style.preferScheme)) { @@ -46,7 +42,6 @@ bgInit.push(async () => { } } }, !__.MV3); - __.DEBUGLOG('styleMan init done'); }); styleCache.setOnDeleted(val => { @@ -58,6 +53,28 @@ styleCache.setOnDeleted(val => { export * from '../style-search-db'; export {getById as get}; +function initStyleMap(styles) { + let fixed, lost; + for (let style of styles) { + try { + if (+style.id > 0 && style._id.trim() && typeof style.sections[0].code === 'string') { + storeInMap(style); + continue; + } + } catch {} + style = fixKnownProblems(style, true); + if (style) (fixed ??= {})[style.id] = style; + else (lost ??= []).push(style); + } + if (fixed) { + console.warn(`Fixed ${fixed.length} styles, ids:`, ...Object.keys(fixed)); + Promise.all([...bgBusy, Object.values(fixed)]) + .then(() => setTimeout(db.putMany, 0, fixed)); + } + if (lost) + console.error(`Skipped ${lost.length} invalid entries:`, lost); +} + /** @returns {Promise} */ export async function config(id, prop, value) { const style = Object.assign({}, getById(id));