write/read styleDigest in the backup file

This commit is contained in:
tophf 2017-04-27 00:49:03 +03:00
parent 809e70a89c
commit a22874a898
2 changed files with 26 additions and 4 deletions

View File

@ -59,6 +59,10 @@ function importFromString(jsonString) {
const oldStyles = json.length && BG.deepCopy(BG.cachedStyles.list || []); const oldStyles = json.length && BG.deepCopy(BG.cachedStyles.list || []);
const oldStylesByName = json.length && new Map( const oldStylesByName = json.length && new Map(
oldStyles.map(style => [style.name.trim(), style])); oldStyles.map(style => [style.name.trim(), style]));
let oldDigests;
chrome.storage.local.get(null, data => (oldDigests = data));
const stats = { const stats = {
added: {names: [], ids: [], legend: 'importReportLegendAdded'}, added: {names: [], ids: [], legend: 'importReportLegendAdded'},
unchanged: {names: [], ids: [], legend: 'importReportLegendIdentical'}, unchanged: {names: [], ids: [], legend: 'importReportLegendIdentical'},
@ -67,12 +71,14 @@ function importFromString(jsonString) {
codeOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedCode'}, codeOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedCode'},
invalid: {names: [], legend: 'importReportLegendInvalid'}, invalid: {names: [], legend: 'importReportLegendInvalid'},
}; };
let index = 0; let index = 0;
let lastRenderTime = performance.now(); let lastRenderTime = performance.now();
const renderQueue = []; const renderQueue = [];
const RENDER_NAP_TIME_MAX = 1000; // ms const RENDER_NAP_TIME_MAX = 1000; // ms
const RENDER_QUEUE_MAX = 50; // number of styles const RENDER_QUEUE_MAX = 50; // number of styles
const SAVE_OPTIONS = {reason: 'import', notify: false}; const SAVE_OPTIONS = {reason: 'import', notify: false};
return new Promise(proceed); return new Promise(proceed);
function proceed(resolve) { function proceed(resolve) {
@ -214,6 +220,7 @@ function importFromString(jsonString) {
deleteStyleSafe({id, notify: false}).then(id => { deleteStyleSafe({id, notify: false}).then(id => {
const oldStyle = oldStylesById.get(id); const oldStyle = oldStylesById.get(id);
if (oldStyle) { if (oldStyle) {
oldStyle.styleDigest = oldDigests[BG.DIGEST_KEY_PREFIX + id];
saveStyleSafe(Object.assign(oldStyle, SAVE_OPTIONS)) saveStyleSafe(Object.assign(oldStyle, SAVE_OPTIONS))
.then(undoNextId); .then(undoNextId);
} else { } else {
@ -275,7 +282,14 @@ function importFromString(jsonString) {
$('#file-all-styles').onclick = () => { $('#file-all-styles').onclick = () => {
getStylesSafe().then(styles => { 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;
});
const text = JSON.stringify(styles, null, '\t'); const text = JSON.stringify(styles, null, '\t');
const fileName = generateFileName(); const fileName = generateFileName();

View File

@ -5,7 +5,9 @@ const RX_NAMESPACE = new RegExp([/[\s\r\n]*/,
/[\s\r\n]*/].map(rx => rx.source).join(''), 'g'); /[\s\r\n]*/].map(rx => rx.source).join(''), 'g');
const RX_CSS_COMMENTS = /\/\*[\s\S]*?\*\//g; const RX_CSS_COMMENTS = /\/\*[\s\S]*?\*\//g;
const SLOPPY_REGEXP_PREFIX = '\0'; const SLOPPY_REGEXP_PREFIX = '\0';
const DIGEST_KEY_PREFIX = 'originalDigest';
// eslint-disable-next-line no-var
var DIGEST_KEY_PREFIX = 'originalDigest';
// Note, only 'var'-declared variables are visible from another extension page // Note, only 'var'-declared variables are visible from another extension page
// eslint-disable-next-line no-var // eslint-disable-next-line no-var
@ -227,9 +229,11 @@ function saveStyle(style) {
const id = Number(style.id) >= 0 ? Number(style.id) : null; const id = Number(style.id) >= 0 ? Number(style.id) : null;
const reason = style.reason; const reason = style.reason;
const notify = style.notify !== false; const notify = style.notify !== false;
const styleDigest = style.styleDigest;
delete style.method; delete style.method;
delete style.reason; delete style.reason;
delete style.notify; delete style.notify;
delete style.styleDigest;
if (!style.name) { if (!style.name) {
delete style.name; delete style.name;
} }
@ -282,7 +286,11 @@ function saveStyle(style) {
if (reason == 'update') { if (reason == 'update') {
updateStyleDigest(style); updateStyleDigest(style);
} else if (reason == 'import') { } else if (reason == 'import') {
chrome.storage.local.remove(DIGEST_KEY_PREFIX + style.id, ignoreChromeError); 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; return style;
} }
@ -566,7 +574,7 @@ function getStyleDigests(style) {
function updateStyleDigest(style) { function updateStyleDigest(style) {
calcStyleDigest(style).then(digest => calcStyleDigest(style).then(digest =>
chromeLocal.set({[DIGEST_KEY_PREFIX + style.id]: digest})); chromeLocal.setValue(DIGEST_KEY_PREFIX + style.id, digest));
} }