embed digest in DB as style.originalDigest
This commit is contained in:
parent
f3c42fc27b
commit
5f0b57bebf
|
@ -67,6 +67,24 @@ if ('commands' in chrome) {
|
|||
browserUIlanguage: UIlang,
|
||||
});
|
||||
}
|
||||
// TODO: remove in the future
|
||||
// embed style digests
|
||||
chrome.storage.local.get(null, data => {
|
||||
const digestKeys = Object.keys(data).filter(key => key.startsWith('originalDigest'));
|
||||
if (!digestKeys.length) {
|
||||
return;
|
||||
}
|
||||
chrome.storage.local.remove(digestKeys);
|
||||
getStyles().then(styles => {
|
||||
for (const style of styles) {
|
||||
const digest = data['originalDigest' + style.id];
|
||||
if (!style.originalDigest && digest) {
|
||||
style.originalDigest = digest;
|
||||
dbExec('put', style);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
// bind for 60 seconds max and auto-unbind if it's a normal run
|
||||
chrome.runtime.onInstalled.addListener(onInstall);
|
||||
|
|
|
@ -60,9 +60,6 @@ function importFromString(jsonString) {
|
|||
const oldStylesByName = json.length && new Map(
|
||||
oldStyles.map(style => [style.name.trim(), style]));
|
||||
|
||||
let oldDigests;
|
||||
chrome.storage.local.get(null, data => (oldDigests = data));
|
||||
|
||||
const stats = {
|
||||
added: {names: [], ids: [], legend: 'importReportLegendAdded'},
|
||||
unchanged: {names: [], ids: [], legend: 'importReportLegendIdentical'},
|
||||
|
@ -220,7 +217,6 @@ function importFromString(jsonString) {
|
|||
deleteStyleSafe({id, notify: false}).then(id => {
|
||||
const oldStyle = oldStylesById.get(id);
|
||||
if (oldStyle) {
|
||||
oldStyle.styleDigest = oldDigests[BG.DIGEST_KEY_PREFIX + id];
|
||||
saveStyleSafe(Object.assign(oldStyle, SAVE_OPTIONS))
|
||||
.then(undoNextId);
|
||||
} else {
|
||||
|
@ -282,14 +278,7 @@ function importFromString(jsonString) {
|
|||
|
||||
|
||||
$('#file-all-styles').onclick = () => {
|
||||
Promise.all([
|
||||
BG.chromeLocal.get(null),
|
||||
getStylesSafe(),
|
||||
]).then(([data, styles]) => {
|
||||
styles = styles.map(style => {
|
||||
const styleDigest = data[BG.DIGEST_KEY_PREFIX + style.id];
|
||||
return styleDigest ? Object.assign({styleDigest}, style) : style;
|
||||
});
|
||||
getStylesSafe().then(styles => {
|
||||
const text = JSON.stringify(styles, null, '\t');
|
||||
const url = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text);
|
||||
return url;
|
||||
|
|
95
storage.js
95
storage.js
|
@ -6,9 +6,6 @@ const RX_NAMESPACE = new RegExp([/[\s\r\n]*/,
|
|||
const RX_CSS_COMMENTS = /\/\*[\s\S]*?\*\//g;
|
||||
const SLOPPY_REGEXP_PREFIX = '\0';
|
||||
|
||||
// eslint-disable-next-line no-var
|
||||
var DIGEST_KEY_PREFIX = 'originalDigest';
|
||||
|
||||
// Note, only 'var'-declared variables are visible from another extension page
|
||||
// eslint-disable-next-line no-var
|
||||
var cachedStyles = {
|
||||
|
@ -232,37 +229,55 @@ function saveStyle(style) {
|
|||
const id = Number(style.id) || null;
|
||||
const reason = style.reason;
|
||||
const notify = style.notify !== false;
|
||||
const styleDigest = style.styleDigest;
|
||||
delete style.method;
|
||||
delete style.reason;
|
||||
delete style.notify;
|
||||
delete style.styleDigest;
|
||||
if (!style.name) {
|
||||
delete style.name;
|
||||
}
|
||||
let existed, codeIsUpdated;
|
||||
if (id !== null) {
|
||||
// Update or create
|
||||
style.id = id;
|
||||
return dbExec('get', id).then((event, store) => {
|
||||
const oldStyle = event.target.result;
|
||||
existed = Boolean(oldStyle);
|
||||
codeIsUpdated = !existed || style.sections && !styleSectionsEqual(style, oldStyle);
|
||||
style = Object.assign({}, oldStyle, style);
|
||||
return write(style, store);
|
||||
if (reason == 'update' || reason == 'update-digest') {
|
||||
return calcStyleDigest(style).then(digest => {
|
||||
style.originalDigest = digest;
|
||||
decide();
|
||||
});
|
||||
} else {
|
||||
// Create
|
||||
delete style.id;
|
||||
style = Object.assign({
|
||||
// Set optional things if they're undefined
|
||||
enabled: true,
|
||||
updateUrl: null,
|
||||
md5Url: null,
|
||||
url: null,
|
||||
originalMd5: null,
|
||||
}, style);
|
||||
return write(style);
|
||||
}
|
||||
if (reason == 'import') {
|
||||
style.originalDigest = style.originalDigest || style.styleDigest; // TODO: remove in the future
|
||||
delete style.styleDigest; // TODO: remove in the future
|
||||
if (typeof style.originalDigest != 'string' || style.originalDigest.length != 40) {
|
||||
delete style.originalDigest;
|
||||
}
|
||||
}
|
||||
return decide();
|
||||
|
||||
function decide() {
|
||||
if (id !== null) {
|
||||
// Update or create
|
||||
style.id = id;
|
||||
return dbExec('get', id).then((event, store) => {
|
||||
const oldStyle = event.target.result;
|
||||
existed = Boolean(oldStyle);
|
||||
if (reason == 'update-digest' && oldStyle.originalDigest == style.originalDigest) {
|
||||
return style;
|
||||
}
|
||||
codeIsUpdated = !existed || style.sections && !styleSectionsEqual(style, oldStyle);
|
||||
style = Object.assign({}, oldStyle, style);
|
||||
return write(style, store);
|
||||
});
|
||||
} else {
|
||||
// Create
|
||||
delete style.id;
|
||||
style = Object.assign({
|
||||
// Set optional things if they're undefined
|
||||
enabled: true,
|
||||
updateUrl: null,
|
||||
md5Url: null,
|
||||
url: null,
|
||||
originalMd5: null,
|
||||
}, style);
|
||||
return write(style);
|
||||
}
|
||||
}
|
||||
|
||||
function write(style, store) {
|
||||
|
@ -277,6 +292,9 @@ function saveStyle(style) {
|
|||
}
|
||||
|
||||
function done(event) {
|
||||
if (reason == 'update-digest') {
|
||||
return style;
|
||||
}
|
||||
style.id = style.id || event.target.result;
|
||||
invalidateCache(existed ? {updated: style} : {added: style});
|
||||
compileStyleRegExps({style});
|
||||
|
@ -286,15 +304,6 @@ function saveStyle(style) {
|
|||
style, codeIsUpdated, reason,
|
||||
});
|
||||
}
|
||||
if (reason == 'update') {
|
||||
updateStyleDigest(style);
|
||||
} else if (reason == 'import') {
|
||||
if (typeof styleDigest == 'string' && styleDigest.length == 40) {
|
||||
chromeLocal.setValue(DIGEST_KEY_PREFIX + style.id, styleDigest);
|
||||
} else {
|
||||
chrome.storage.local.remove(DIGEST_KEY_PREFIX + style.id);
|
||||
}
|
||||
}
|
||||
return style;
|
||||
}
|
||||
}
|
||||
|
@ -302,7 +311,7 @@ function saveStyle(style) {
|
|||
|
||||
function deleteStyle({id, notify = true}) {
|
||||
id = Number(id);
|
||||
chrome.storage.local.remove(DIGEST_KEY_PREFIX + id, ignoreChromeError);
|
||||
chrome.storage.local.remove('originalDigest' + id, ignoreChromeError); // TODO: remove in the future
|
||||
return dbExec('delete', id).then(() => {
|
||||
invalidateCache({deletedId: id});
|
||||
if (notify) {
|
||||
|
@ -567,20 +576,6 @@ function normalizeStyleSections({sections}) {
|
|||
}
|
||||
|
||||
|
||||
function getStyleDigests(style) {
|
||||
return Promise.all([
|
||||
chromeLocal.getValue(DIGEST_KEY_PREFIX + style.id),
|
||||
calcStyleDigest(style),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
function updateStyleDigest(style) {
|
||||
calcStyleDigest(style).then(digest =>
|
||||
chromeLocal.setValue(DIGEST_KEY_PREFIX + style.id, digest));
|
||||
}
|
||||
|
||||
|
||||
function calcStyleDigest(style) {
|
||||
const jsonString = JSON.stringify(normalizeStyleSections(style));
|
||||
const text = new TextEncoder('utf-8').encode(jsonString);
|
||||
|
|
16
update.js
16
update.js
|
@ -1,5 +1,5 @@
|
|||
/* global getStyles, saveStyle, styleSectionsEqual, chromeLocal */
|
||||
/* global getStyleDigests, updateStyleDigest */
|
||||
/* global calcStyleDigest */
|
||||
'use strict';
|
||||
|
||||
// eslint-disable-next-line no-var
|
||||
|
@ -39,7 +39,6 @@ var updater = {
|
|||
},
|
||||
|
||||
checkStyle({style, observer = () => {}, save = true, ignoreDigest}) {
|
||||
let hasDigest;
|
||||
/*
|
||||
Original style digests are calculated in these cases:
|
||||
* style is installed or updated from server
|
||||
|
@ -54,7 +53,7 @@ var updater = {
|
|||
|
||||
'ignoreDigest' option is set on the second manual individual update check on the manage page.
|
||||
*/
|
||||
return getStyleDigests(style)
|
||||
return (ignoreDigest ? Promise.resolve() : calcStyleDigest(style))
|
||||
.then(maybeFetchMd5)
|
||||
.then(maybeFetchCode)
|
||||
.then(maybeSave)
|
||||
|
@ -68,9 +67,8 @@ var updater = {
|
|||
updater.log(updater.SKIPPED + ` (${err}) #${style.id} ${style.name}`);
|
||||
});
|
||||
|
||||
function maybeFetchMd5([originalDigest, current]) {
|
||||
hasDigest = Boolean(originalDigest);
|
||||
if (hasDigest && !ignoreDigest && originalDigest != current) {
|
||||
function maybeFetchMd5(digest) {
|
||||
if (!ignoreDigest && style.originalDigest && style.originalDigest != digest) {
|
||||
return Promise.reject(updater.EDITED);
|
||||
}
|
||||
return download(style.md5Url);
|
||||
|
@ -80,7 +78,7 @@ var updater = {
|
|||
if (!md5 || md5.length != 32) {
|
||||
return Promise.reject(updater.ERROR_MD5);
|
||||
}
|
||||
if (md5 == style.originalMd5 && hasDigest && !ignoreDigest) {
|
||||
if (md5 == style.originalMd5 && style.originalDigest && !ignoreDigest) {
|
||||
return Promise.reject(updater.SAME_MD5);
|
||||
}
|
||||
return download(style.updateUrl);
|
||||
|
@ -95,9 +93,9 @@ var updater = {
|
|||
if (styleSectionsEqual(json, style)) {
|
||||
// JSONs may have different order of items even if sections are effectively equal
|
||||
// so we'll update the digest anyway
|
||||
updateStyleDigest(json);
|
||||
saveStyle(Object.assign(json, {reason: 'update-digest'}));
|
||||
return Promise.reject(updater.SAME_CODE);
|
||||
} else if (!hasDigest && !ignoreDigest) {
|
||||
} else if (!style.originalDigest && !ignoreDigest) {
|
||||
return Promise.reject(updater.MAYBE_EDITED);
|
||||
}
|
||||
return !save ? json :
|
||||
|
|
Loading…
Reference in New Issue
Block a user