Add: API.getStyleFromDB

This commit is contained in:
eight 2018-09-23 17:13:47 +08:00
parent ec477f0c02
commit 1e60a7fb7e
2 changed files with 13 additions and 3 deletions

View File

@ -14,6 +14,9 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, {
saveStyle, saveStyle,
deleteStyle, deleteStyle,
getStyleFromDB: id =>
dbExec('get', id).then(event => event.target.result),
download(msg) { download(msg) {
delete msg.method; delete msg.method;
return download(msg.url, msg); return download(msg.url, msg);

View File

@ -228,7 +228,7 @@ function isUsercss(style) {
function initStyleData() { function initStyleData() {
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425) // TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425)
const params = new URLSearchParams(location.search.replace(/^\?/, '')); const params = new URLSearchParams(location.search.replace(/^\?/, ''));
const id = params.get('id'); const id = Number(params.get('id'));
const createEmptyStyle = () => ({ const createEmptyStyle = () => ({
id: null, id: null,
name: params.get('domain') || name: params.get('domain') ||
@ -244,8 +244,8 @@ function initStyleData() {
) )
], ],
}); });
return API.getStyles({id: id || -1}) return fetchStyle()
.then(([style = createEmptyStyle()]) => { .then(style => {
styleId = style.id; styleId = style.id;
if (styleId) sessionStorage.justEditedStyleId = styleId; if (styleId) sessionStorage.justEditedStyleId = styleId;
// we set "usercss" class on <html> when <body> is empty // we set "usercss" class on <html> when <body> is empty
@ -259,6 +259,13 @@ function initStyleData() {
} }
return style; return style;
}); });
function fetchStyle() {
if (id) {
return API.getStyleFromDB(id);
}
return Promise.resolve(createEmptyStyle());
}
} }
function initHooks() { function initHooks() {