Fix: usercss API

This commit is contained in:
eight 2018-10-07 23:28:41 +08:00
parent 057111b171
commit 02f471f077
3 changed files with 80 additions and 45 deletions

View File

@ -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,

View File

@ -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);
}

View File

@ -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,7 +56,8 @@
if (style.reason === 'config' && style.id) {
return style;
}
const dup = find(style);
return find(style)
.then(dup => {
if (dup) {
style.id = dup.id;
if (style.reason !== 'config') {
@ -68,6 +66,7 @@
}
}
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) {
function parse(style) {
return fetchStyle()
.then(buildMeta)
.then(assignVars)
.then(usercss.buildCode);
function fetchStyle() {
// restore if stripped by getStyleWithNoCode
if (typeof style.sourceCode !== 'string') {
style.sourceCode = cachedStyles.byId.get(style.id).sourceCode;
return styleManager.get(style.id)
.then(oldStyle => {
style.sourceCode = oldStyle.sourceCode;
return style;
});
}
return Promise.resolve(style);
}
return buildMeta(style)
.then(assignVars)
.then(style => usercss.buildCode(style, allowErrors));
}
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,9 +139,12 @@
* @returns {Style}
*/
function find(styleOrData) {
if (styleOrData.id) return cachedStyles.byId.get(styleOrData.id);
if (styleOrData.id) {
return styleManager.get(styleOrData.id);
}
const {name, namespace} = styleOrData.usercssData || styleOrData;
for (const dup of cachedStyles.list) {
return styleManager.getAllStyles(styleList => {
for (const dup of styleList) {
const data = dup.usercssData;
if (!data) continue;
if (data.name === name &&
@ -125,6 +152,7 @@
return dup;
}
}
});
}
function install({url, direct, downloaded, tab}, sender) {