don't export redundant values (#1373)

+ implement proper check for same code in usercss so unchanged styles won't be unnecessarily imported
This commit is contained in:
tophf 2021-12-25 13:08:38 +03:00 committed by GitHub
parent 6b9cdf2bc2
commit 3cdf526fa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -124,7 +124,14 @@ async function importFromString(jsonString) {
if (item && !item.id && item[prefs.STORAGE_KEY]) { if (item && !item.id && item[prefs.STORAGE_KEY]) {
return analyzeStorage(item); return analyzeStorage(item);
} }
if (typeof item !== 'object' || !styleJSONseemsValid(item)) { if (
!item ||
typeof item !== 'object' || (
isEmptyObj(item.usercssData)
? !styleJSONseemsValid(item)
: typeof item.sourceCode !== 'string'
)
) {
stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`); stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`);
return; return;
} }
@ -144,8 +151,8 @@ async function importFromString(jsonString) {
item.id = byName.id; item.id = byName.id;
oldStyle = byName; oldStyle = byName;
} }
const metaEqual = oldStyle && deepEqual(oldStyle, item, ['sections', '_rev']); const metaEqual = oldStyle && deepEqual(oldStyle, item, ['sections', 'sourceCode', '_rev']);
const codeEqual = oldStyle && styleSectionsEqual(oldStyle, item); const codeEqual = oldStyle && sameCode(oldStyle, item);
if (metaEqual && codeEqual) { if (metaEqual && codeEqual) {
stats.unchanged.names.push(oldStyle.name); stats.unchanged.names.push(oldStyle.name);
stats.unchanged.ids.push(oldStyle.id); stats.unchanged.ids.push(oldStyle.id);
@ -172,6 +179,14 @@ async function importFromString(jsonString) {
} }
} }
function sameCode(oldStyle, newStyle) {
const d1 = oldStyle.usercssData;
const d2 = newStyle.usercssData;
return !d1 + !d2
? styleSectionsEqual(oldStyle, newStyle)
: oldStyle.sourceCode === newStyle.sourceCode && deepEqual(d1.vars, d2.vars);
}
function sameStyle(oldStyle, newStyle) { function sameStyle(oldStyle, newStyle) {
return oldStyle.name.trim() === newStyle.name.trim() || return oldStyle.name.trim() === newStyle.name.trim() ||
['updateUrl', 'originalMd5', 'originalDigest'] ['updateUrl', 'originalMd5', 'originalDigest']
@ -324,7 +339,7 @@ async function exportToFile() {
Object.assign({ Object.assign({
[prefs.STORAGE_KEY]: prefs.values, [prefs.STORAGE_KEY]: prefs.values,
}, await chromeSync.getLZValues()), }, await chromeSync.getLZValues()),
...await API.styles.getAll(), ...(await API.styles.getAll()).map(cleanupStyle),
]; ];
const text = JSON.stringify(data, null, ' '); const text = JSON.stringify(data, null, ' ');
const type = 'application/json'; const type = 'application/json';
@ -333,6 +348,19 @@ async function exportToFile() {
download: generateFileName(), download: generateFileName(),
type, type,
}).dispatchEvent(new MouseEvent('click')); }).dispatchEvent(new MouseEvent('click'));
/** strip `sections`, `null` and empty objects */
function cleanupStyle(style) {
const copy = {};
for (let [key, val] of Object.entries(style)) {
if (key === 'sections'
// Keeping dummy `sections` for compatibility with older Stylus
? !style.usercssData || (val = [{code: ''}])
: typeof val !== 'object' || !isEmptyObj(val)) {
copy[key] = val;
}
}
return copy;
}
function generateFileName() { function generateFileName() {
const today = new Date(); const today = new Date();
const dd = ('0' + today.getDate()).substr(-2); const dd = ('0' + today.getDate()).substr(-2);