From 7d340d62dcb7a25a1ccdc6648ab0683afbda917d Mon Sep 17 00:00:00 2001 From: eight Date: Sat, 13 Oct 2018 15:13:11 +0800 Subject: [PATCH] Change: drop storage.js, some functions are moved to sections-util --- background/storage.js | 30 ------------------------------ js/sections-util.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 31 deletions(-) delete mode 100644 background/storage.js diff --git a/background/storage.js b/background/storage.js deleted file mode 100644 index e7a1153f..00000000 --- a/background/storage.js +++ /dev/null @@ -1,30 +0,0 @@ -/* exported calcStyleDigest detectSloppyRegexps */ -'use strict'; - -function normalizeStyleSections({sections}) { - // retain known properties in an arbitrarily predefined order - return (sections || []).map(section => ({ - code: section.code || '', - urls: section.urls || [], - urlPrefixes: section.urlPrefixes || [], - domains: section.domains || [], - regexps: section.regexps || [], - })); -} - -function calcStyleDigest(style) { - const jsonString = style.usercssData ? - style.sourceCode : JSON.stringify(normalizeStyleSections(style)); - const text = new TextEncoder('utf-8').encode(jsonString); - return crypto.subtle.digest('SHA-1', text).then(hex); - - function hex(buffer) { - const parts = []; - const PAD8 = '00000000'; - const view = new DataView(buffer); - for (let i = 0; i < view.byteLength; i += 4) { - parts.push((PAD8 + view.getUint32(i).toString(16)).slice(-8)); - } - return parts.join(''); - } -} diff --git a/js/sections-util.js b/js/sections-util.js index 79413a7c..71ffabbe 100644 --- a/js/sections-util.js +++ b/js/sections-util.js @@ -1,4 +1,4 @@ -/* exported styleSectionsEqual styleCodeEmpty */ +/* exported styleSectionsEqual styleCodeEmpty calcStyleDigest */ 'use strict'; function styleCodeEmpty(code) { @@ -65,3 +65,31 @@ function styleSectionsEqual(a, b, {ignoreCode, checkSource} = {}) { ); } } + +function normalizeStyleSections({sections}) { + // retain known properties in an arbitrarily predefined order + return (sections || []).map(section => ({ + code: section.code || '', + urls: section.urls || [], + urlPrefixes: section.urlPrefixes || [], + domains: section.domains || [], + regexps: section.regexps || [], + })); +} + +function calcStyleDigest(style) { + const jsonString = style.usercssData ? + style.sourceCode : JSON.stringify(normalizeStyleSections(style)); + const text = new TextEncoder('utf-8').encode(jsonString); + return crypto.subtle.digest('SHA-1', text).then(hex); + + function hex(buffer) { + const parts = []; + const PAD8 = '00000000'; + const view = new DataView(buffer); + for (let i = 0; i < view.byteLength; i += 4) { + parts.push((PAD8 + view.getUint32(i).toString(16)).slice(-8)); + } + return parts.join(''); + } +}