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