From a22874a898d6ee612a92184205897384f065a684 Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 27 Apr 2017 00:49:03 +0300 Subject: [PATCH] write/read styleDigest in the backup file --- backup/fileSaveLoad.js | 16 +++++++++++++++- storage.js | 14 +++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/backup/fileSaveLoad.js b/backup/fileSaveLoad.js index 62de23d4..9aa88603 100644 --- a/backup/fileSaveLoad.js +++ b/backup/fileSaveLoad.js @@ -59,6 +59,10 @@ function importFromString(jsonString) { const oldStyles = json.length && BG.deepCopy(BG.cachedStyles.list || []); 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'}, @@ -67,12 +71,14 @@ function importFromString(jsonString) { codeOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedCode'}, invalid: {names: [], legend: 'importReportLegendInvalid'}, }; + let index = 0; let lastRenderTime = performance.now(); const renderQueue = []; const RENDER_NAP_TIME_MAX = 1000; // ms const RENDER_QUEUE_MAX = 50; // number of styles const SAVE_OPTIONS = {reason: 'import', notify: false}; + return new Promise(proceed); function proceed(resolve) { @@ -214,6 +220,7 @@ 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 { @@ -275,7 +282,14 @@ function importFromString(jsonString) { $('#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 fileName = generateFileName(); diff --git a/storage.js b/storage.js index 0479c9cf..0959c640 100644 --- a/storage.js +++ b/storage.js @@ -5,7 +5,9 @@ const RX_NAMESPACE = new RegExp([/[\s\r\n]*/, /[\s\r\n]*/].map(rx => rx.source).join(''), 'g'); const RX_CSS_COMMENTS = /\/\*[\s\S]*?\*\//g; 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 // eslint-disable-next-line no-var @@ -227,9 +229,11 @@ function saveStyle(style) { const id = Number(style.id) >= 0 ? 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; } @@ -282,7 +286,11 @@ function saveStyle(style) { if (reason == 'update') { updateStyleDigest(style); } 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; } @@ -566,7 +574,7 @@ function getStyleDigests(style) { function updateStyleDigest(style) { calcStyleDigest(style).then(digest => - chromeLocal.set({[DIGEST_KEY_PREFIX + style.id]: digest})); + chromeLocal.setValue(DIGEST_KEY_PREFIX + style.id, digest)); }