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}); return usercss.invokeWorker({action: 'parse', code});
}, },
getPrefs: prefs.getAll, getPrefs: prefs.getAll,
healthCheck: () => dbExec().then(() => true),
// FIXME: who uses this?
healthCheck: () => db.exec().then(() => true),
detectSloppyRegexps, detectSloppyRegexps,
openEditor, openEditor,

View File

@ -14,6 +14,7 @@ const styleManager = (() => {
const BAD_MATCHER = {test: () => false}; const BAD_MATCHER = {test: () => false};
return ensurePrepared({ return ensurePrepared({
get,
getStylesInfo, getStylesInfo,
getSectionsByUrl, getSectionsByUrl,
installStyle, installStyle,
@ -27,6 +28,10 @@ const styleManager = (() => {
countStylesByUrl, // used by icon badge countStylesByUrl, // used by icon badge
}); });
function get(id) {
return styles.get(id).data;
}
function getAllStyles() { function getAllStyles() {
return [...styles.values()].map(s => s.data); 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'; 'use strict';
(() => { (() => {
API_METHODS.installUsercss = installUsercss;
// API_METHODS.saveUsercss = style => save(style, false); API_METHODS.editSaveUsercss = editSaveUsercss;
// API_METHODS.saveUsercssUnsafe = style => save(style, true); API_METHODS.configUsercssVars = configUsercssVars;
API_METHODS.installUsercss
API_METHODS.editSaveUsercss
API_METHODS.configUsercssVars = (id, vars) => newVars
API_METHODS.buildUsercss = build; API_METHODS.buildUsercss = build;
API_METHODS.openUsercssInstallPage API_METHODS.openUsercssInstallPage = install;
// API_METHODS.installUsercss = install;
API_METHODS.parseUsercss = parse; API_METHODS.parseUsercss = parse;
API_METHODS.findUsercss = find; API_METHODS.findUsercss = find;
@ -59,15 +56,17 @@
if (style.reason === 'config' && style.id) { if (style.reason === 'config' && style.id) {
return style; return style;
} }
const dup = find(style); return find(style)
if (dup) { .then(dup => {
style.id = dup.id; if (dup) {
if (style.reason !== 'config') { style.id = dup.id;
// preserve style.vars during update if (style.reason !== 'config') {
usercss.assignVars(style, dup); // preserve style.vars during update
} usercss.assignVars(style, dup);
} }
return style; }
return style;
});
} }
/** /**
@ -85,29 +84,54 @@
}) { }) {
const task = buildMeta({sourceCode}); const task = buildMeta({sourceCode});
return (metaOnly ? task : task.then(usercss.buildCode)) return (metaOnly ? task : task.then(usercss.buildCode))
.then(style => ({ .then(style => {
style, if (!checkDup) {
dup: checkDup && find(style), return {style};
})); }
return find(style)
.then(dup => ({style, dup}));
});
} }
// Parse the source, apply customizations, report fatal/syntax errors // Parse the source, apply customizations, report fatal/syntax errors
function parse(style, allowErrors = false) { function parse(style) {
// restore if stripped by getStyleWithNoCode return fetchStyle()
if (typeof style.sourceCode !== 'string') { .then(buildMeta)
style.sourceCode = cachedStyles.byId.get(style.id).sourceCode;
}
return buildMeta(style)
.then(assignVars) .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) { function installUsercss(style) {
return parse(style, allowErrors) return parse(style)
.then(result => .then(styleManager.installStyle);
allowErrors ? }
saveStyle(result.style).then(style => ({style, errors: result.errors})) :
saveStyle(result)); 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} * @returns {Style}
*/ */
function find(styleOrData) { function find(styleOrData) {
if (styleOrData.id) return cachedStyles.byId.get(styleOrData.id); if (styleOrData.id) {
const {name, namespace} = styleOrData.usercssData || styleOrData; return styleManager.get(styleOrData.id);
for (const dup of cachedStyles.list) {
const data = dup.usercssData;
if (!data) continue;
if (data.name === name &&
data.namespace === namespace) {
return dup;
}
} }
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) { function install({url, direct, downloaded, tab}, sender) {