420733b93a
* add Patch CSP option * show style version, size, and update age in manager * add scope selector to style search in manager * keep scroll position and selections in tab's session * directly install usercss from raw github links * ditch localStorage, use on-demand SessionStore proxy * simplify localization * allow <code> tag in i18n-html * keep nodes in HTML templates * API.getAllStyles is actually faster with code untouched * fix fitToContent when applies-to is taller than window * dedupe linter.enableForEditor calls * prioritize visible CMs in refreshOnViewListener * don't scroll to last style on editing a new one * delay colorview for invisible CMs * eslint comma-dangle error + autofix files * styleViaXhr: also toggle for disableAll pref * styleViaXhr: allow cookies for sandbox CSP * simplify notes in options * simplify getStylesViaXhr * oldUI fixups: * remove separator before 1st applies-to * center name bubbles * fix updateToc focus on a newly added section * fix fitToContent when cloning section * remove CSS `contain` as it makes no difference * replace overrides with declarative CSS + code cosmetics * simplify adjustWidth and make it work in FF
68 lines
1.9 KiB
JavaScript
68 lines
1.9 KiB
JavaScript
/* global chromeLocal */
|
|
/* exported createChromeStorageDB */
|
|
'use strict';
|
|
|
|
function createChromeStorageDB() {
|
|
let INC;
|
|
|
|
const PREFIX = 'style-';
|
|
const METHODS = {
|
|
// FIXME: we don't use this method at all. Should we remove this?
|
|
get: id => chromeLocal.getValue(PREFIX + id),
|
|
put: obj =>
|
|
// FIXME: should we clone the object?
|
|
Promise.resolve(!obj.id && prepareInc().then(() => Object.assign(obj, {id: INC++})))
|
|
.then(() => chromeLocal.setValue(PREFIX + obj.id, obj))
|
|
.then(() => obj.id),
|
|
putMany: items => prepareInc()
|
|
.then(() =>
|
|
chromeLocal.set(items.reduce((data, item) => {
|
|
if (!item.id) item.id = INC++;
|
|
data[PREFIX + item.id] = item;
|
|
return data;
|
|
}, {})))
|
|
.then(() => items.map(i => i.id)),
|
|
delete: id => chromeLocal.remove(PREFIX + id),
|
|
getAll: () => chromeLocal.get()
|
|
.then(result => {
|
|
const output = [];
|
|
for (const key in result) {
|
|
if (key.startsWith(PREFIX) && Number(key.slice(PREFIX.length))) {
|
|
output.push(result[key]);
|
|
}
|
|
}
|
|
return output;
|
|
}),
|
|
};
|
|
|
|
return {exec};
|
|
|
|
function exec(method, ...args) {
|
|
if (METHODS[method]) {
|
|
return METHODS[method](...args)
|
|
.then(result => {
|
|
if (method === 'putMany' && result.map) {
|
|
return result.map(r => ({target: {result: r}}));
|
|
}
|
|
return {target: {result}};
|
|
});
|
|
}
|
|
return Promise.reject(new Error(`unknown DB method ${method}`));
|
|
}
|
|
|
|
function prepareInc() {
|
|
if (INC) return Promise.resolve();
|
|
return chromeLocal.get().then(result => {
|
|
INC = 1;
|
|
for (const key in result) {
|
|
if (key.startsWith(PREFIX)) {
|
|
const id = Number(key.slice(PREFIX.length));
|
|
if (id >= INC) {
|
|
INC = id + 1;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|