From 02f471f07758db8753beed09c40aaba921940b77 Mon Sep 17 00:00:00 2001 From: eight Date: Sun, 7 Oct 2018 23:28:41 +0800 Subject: [PATCH] Fix: usercss API --- background/background.js | 4 +- background/style-manager.js | 5 ++ background/usercss-helper.js | 116 ++++++++++++++++++++++------------- 3 files changed, 80 insertions(+), 45 deletions(-) diff --git a/background/background.js b/background/background.js index eaa12eec..7158b165 100644 --- a/background/background.js +++ b/background/background.js @@ -27,7 +27,9 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, { return usercss.invokeWorker({action: 'parse', code}); }, getPrefs: prefs.getAll, - healthCheck: () => dbExec().then(() => true), + + // FIXME: who uses this? + healthCheck: () => db.exec().then(() => true), detectSloppyRegexps, openEditor, diff --git a/background/style-manager.js b/background/style-manager.js index 5ca04594..cac8ebd5 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -14,6 +14,7 @@ const styleManager = (() => { const BAD_MATCHER = {test: () => false}; return ensurePrepared({ + get, getStylesInfo, getSectionsByUrl, installStyle, @@ -27,6 +28,10 @@ const styleManager = (() => { countStylesByUrl, // used by icon badge }); + function get(id) { + return styles.get(id).data; + } + function getAllStyles() { return [...styles.values()].map(s => s.data); } diff --git a/background/usercss-helper.js b/background/usercss-helper.js index 1079511c..be4e5716 100644 --- a/background/usercss-helper.js +++ b/background/usercss-helper.js @@ -1,17 +1,14 @@ -/* global API_METHODS usercss chromeLocal */ +/* global API_METHODS usercss chromeLocal styleManager */ 'use strict'; (() => { - - // API_METHODS.saveUsercss = style => save(style, false); - // API_METHODS.saveUsercssUnsafe = style => save(style, true); - API_METHODS.installUsercss - API_METHODS.editSaveUsercss - API_METHODS.configUsercssVars = (id, vars) => newVars + API_METHODS.installUsercss = installUsercss; + API_METHODS.editSaveUsercss = editSaveUsercss; + API_METHODS.configUsercssVars = configUsercssVars; API_METHODS.buildUsercss = build; - API_METHODS.openUsercssInstallPage - // API_METHODS.installUsercss = install; + API_METHODS.openUsercssInstallPage = install; + API_METHODS.parseUsercss = parse; API_METHODS.findUsercss = find; @@ -59,15 +56,17 @@ if (style.reason === 'config' && style.id) { return style; } - const dup = find(style); - if (dup) { - style.id = dup.id; - if (style.reason !== 'config') { - // preserve style.vars during update - usercss.assignVars(style, dup); - } - } - return style; + return find(style) + .then(dup => { + if (dup) { + style.id = dup.id; + if (style.reason !== 'config') { + // preserve style.vars during update + usercss.assignVars(style, dup); + } + } + return style; + }); } /** @@ -85,29 +84,54 @@ }) { const task = buildMeta({sourceCode}); return (metaOnly ? task : task.then(usercss.buildCode)) - .then(style => ({ - style, - dup: checkDup && find(style), - })); + .then(style => { + if (!checkDup) { + return {style}; + } + return find(style) + .then(dup => ({style, dup})); + }); } // Parse the source, apply customizations, report fatal/syntax errors - function parse(style, allowErrors = false) { - // restore if stripped by getStyleWithNoCode - if (typeof style.sourceCode !== 'string') { - style.sourceCode = cachedStyles.byId.get(style.id).sourceCode; - } - return buildMeta(style) + function parse(style) { + return fetchStyle() + .then(buildMeta) .then(assignVars) - .then(style => usercss.buildCode(style, allowErrors)); + .then(usercss.buildCode); + + function fetchStyle() { + // restore if stripped by getStyleWithNoCode + if (typeof style.sourceCode !== 'string') { + return styleManager.get(style.id) + .then(oldStyle => { + style.sourceCode = oldStyle.sourceCode; + return style; + }); + } + return Promise.resolve(style); + } } - function save(style, allowErrors = false) { - return parse(style, allowErrors) - .then(result => - allowErrors ? - saveStyle(result.style).then(style => ({style, errors: result.errors})) : - saveStyle(result)); + function installUsercss(style) { + return parse(style) + .then(styleManager.installStyle); + } + + function editSaveUsercss(style) { + return parse(style) + .then(styleManager.editSave); + } + + function configUsercssVars(id, vars) { + return styleManager.get(id) + .then(style => { + const newStyle = deepCopy(style); + newStyle.usercssData.vars = vars; + return usercss.buildCode(newStyle); + }) + .then(style => styleManager.installStyle(style, 'config')) + .then(style => style.usercssData.vars); } /** @@ -115,16 +139,20 @@ * @returns {Style} */ function find(styleOrData) { - if (styleOrData.id) return cachedStyles.byId.get(styleOrData.id); - const {name, namespace} = styleOrData.usercssData || styleOrData; - for (const dup of cachedStyles.list) { - const data = dup.usercssData; - if (!data) continue; - if (data.name === name && - data.namespace === namespace) { - return dup; - } + if (styleOrData.id) { + return styleManager.get(styleOrData.id); } + const {name, namespace} = styleOrData.usercssData || styleOrData; + return styleManager.getAllStyles(styleList => { + for (const dup of styleList) { + const data = dup.usercssData; + if (!data) continue; + if (data.name === name && + data.namespace === namespace) { + return dup; + } + } + }); } function install({url, direct, downloaded, tab}, sender) {