2018-11-07 06:09:29 +00:00
|
|
|
/* global messageBox styleSectionsEqual getOwnTab API onDOMready
|
2019-04-16 12:32:06 +00:00
|
|
|
tryJSONparse scrollElementIntoView $ $$ API $create t animateElement
|
|
|
|
styleJSONseemsValid */
|
2017-07-12 18:17:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const STYLISH_DUMP_FILE_EXT = '.txt';
|
|
|
|
const STYLUS_BACKUP_FILE_EXT = '.json';
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
onDOMready().then(() => {
|
2020-02-02 04:36:54 +00:00
|
|
|
$('#file-all-styles').onclick = event => {
|
|
|
|
event.preventDefault();
|
|
|
|
exportToFile();
|
|
|
|
};
|
|
|
|
$('#unfile-all-styles').onclick = event => {
|
|
|
|
event.preventDefault();
|
2018-11-07 06:09:29 +00:00
|
|
|
importFromFile({fileTypeFilter: STYLUS_BACKUP_FILE_EXT});
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(document.body, {
|
|
|
|
ondragover(event) {
|
|
|
|
const hasFiles = event.dataTransfer.types.includes('Files');
|
|
|
|
event.dataTransfer.dropEffect = hasFiles || event.target.type === 'search' ? 'copy' : 'none';
|
|
|
|
this.classList.toggle('dropzone', hasFiles);
|
|
|
|
if (hasFiles) {
|
|
|
|
event.preventDefault();
|
|
|
|
clearTimeout(this.fadeoutTimer);
|
|
|
|
this.classList.remove('fadeout');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ondragend() {
|
|
|
|
animateElement(this, {className: 'fadeout', removeExtraClasses: ['dropzone']}).then(() => {
|
|
|
|
this.style.animationDuration = '';
|
|
|
|
});
|
|
|
|
},
|
|
|
|
ondragleave(event) {
|
|
|
|
try {
|
|
|
|
// in Firefox event.target could be XUL browser and hence there is no permission to access it
|
|
|
|
if (event.target === this) {
|
|
|
|
this.ondragend();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.ondragend();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ondrop(event) {
|
|
|
|
this.ondragend();
|
|
|
|
if (event.dataTransfer.files.length) {
|
|
|
|
event.preventDefault();
|
|
|
|
if ($('#only-updates input').checked) {
|
|
|
|
$('#only-updates input').click();
|
|
|
|
}
|
|
|
|
importFromFile({file: event.dataTransfer.files[0]});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2017-07-12 18:17:04 +00:00
|
|
|
|
|
|
|
function importFromFile({fileTypeFilter, file} = {}) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const fileInput = document.createElement('input');
|
|
|
|
if (file) {
|
|
|
|
readFile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fileInput.style.display = 'none';
|
|
|
|
fileInput.type = 'file';
|
|
|
|
fileInput.accept = fileTypeFilter || STYLISH_DUMP_FILE_EXT;
|
|
|
|
fileInput.acceptCharset = 'utf-8';
|
|
|
|
|
|
|
|
document.body.appendChild(fileInput);
|
|
|
|
fileInput.initialValue = fileInput.value;
|
|
|
|
fileInput.onchange = readFile;
|
|
|
|
fileInput.click();
|
|
|
|
|
|
|
|
function readFile() {
|
|
|
|
if (file || fileInput.value !== fileInput.initialValue) {
|
|
|
|
file = file || fileInput.files[0];
|
|
|
|
if (file.size > 100e6) {
|
|
|
|
console.warn("100MB backup? I don't believe you.");
|
|
|
|
importFromString('').then(resolve);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document.body.style.cursor = 'wait';
|
|
|
|
const fReader = new FileReader();
|
|
|
|
fReader.onloadend = event => {
|
|
|
|
fileInput.remove();
|
2017-12-06 08:03:43 +00:00
|
|
|
const text = event.target.result;
|
|
|
|
const maybeUsercss = !/^[\s\r\n]*\[/.test(text) &&
|
|
|
|
(text.includes('==UserStyle==') || /==UserStyle==/i.test(text));
|
|
|
|
(!maybeUsercss ?
|
|
|
|
importFromString(text) :
|
|
|
|
getOwnTab().then(tab => {
|
|
|
|
tab.url = URL.createObjectURL(new Blob([text], {type: 'text/css'}));
|
2018-11-07 06:09:29 +00:00
|
|
|
return API.openUsercssInstallPage({direct: true, tab})
|
2017-12-06 08:03:43 +00:00
|
|
|
.then(() => URL.revokeObjectURL(tab.url));
|
|
|
|
})
|
|
|
|
).then(numStyles => {
|
2017-07-12 18:17:04 +00:00
|
|
|
document.body.style.cursor = '';
|
|
|
|
resolve(numStyles);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
fReader.readAsText(file, 'utf-8');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function importFromString(jsonString) {
|
|
|
|
const json = tryJSONparse(jsonString);
|
|
|
|
if (!Array.isArray(json)) {
|
|
|
|
return Promise.reject(new Error('the backup is not a valid JSON file'));
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
let oldStyles;
|
|
|
|
let oldStylesById;
|
|
|
|
let oldStylesByName;
|
2017-07-12 18:17:04 +00:00
|
|
|
const stats = {
|
|
|
|
added: {names: [], ids: [], legend: 'importReportLegendAdded'},
|
|
|
|
unchanged: {names: [], ids: [], legend: 'importReportLegendIdentical'},
|
|
|
|
metaAndCode: {names: [], ids: [], legend: 'importReportLegendUpdatedBoth'},
|
|
|
|
metaOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedMeta'},
|
|
|
|
codeOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedCode'},
|
|
|
|
invalid: {names: [], legend: 'importReportLegendInvalid'},
|
|
|
|
};
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
return API.getAllStyles().then(styles => {
|
|
|
|
// make a copy of the current database, that may be used when we want to
|
|
|
|
// undo
|
|
|
|
oldStyles = styles;
|
|
|
|
oldStylesById = new Map(
|
|
|
|
oldStyles.map(style => [style.id, style]));
|
|
|
|
oldStylesByName = json.length && new Map(
|
|
|
|
oldStyles.map(style => [style.name.trim(), style]));
|
2018-11-11 06:04:22 +00:00
|
|
|
|
|
|
|
const items = [];
|
|
|
|
json.forEach((item, i) => {
|
2018-11-07 06:09:29 +00:00
|
|
|
const info = analyze(item, i);
|
2017-07-12 18:17:04 +00:00
|
|
|
if (info) {
|
2018-11-11 06:04:22 +00:00
|
|
|
items.push({info, item});
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2018-11-11 06:04:22 +00:00
|
|
|
});
|
|
|
|
return API.importManyStyles(items.map(i => i.item))
|
|
|
|
.then(styles => {
|
|
|
|
for (let i = 0; i < styles.length; i++) {
|
|
|
|
updateStats(styles[i], items[i].info);
|
|
|
|
}
|
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
})
|
|
|
|
.then(done);
|
2017-07-12 18:17:04 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function analyze(item, index) {
|
2019-04-16 12:32:06 +00:00
|
|
|
if (typeof item !== 'object' || !styleJSONseemsValid(item)) {
|
2017-07-12 18:17:04 +00:00
|
|
|
stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
item.name = item.name.trim();
|
2018-01-01 17:02:49 +00:00
|
|
|
const byId = oldStylesById.get(item.id);
|
2017-07-12 18:17:04 +00:00
|
|
|
const byName = oldStylesByName.get(item.name);
|
|
|
|
oldStylesByName.delete(item.name);
|
|
|
|
let oldStyle;
|
|
|
|
if (byId) {
|
|
|
|
if (sameStyle(byId, item)) {
|
|
|
|
oldStyle = byId;
|
|
|
|
} else {
|
2018-12-08 03:57:43 +00:00
|
|
|
delete item.id;
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!oldStyle && byName) {
|
|
|
|
item.id = byName.id;
|
|
|
|
oldStyle = byName;
|
|
|
|
}
|
|
|
|
const oldStyleKeys = oldStyle && Object.keys(oldStyle);
|
|
|
|
const metaEqual = oldStyleKeys &&
|
2017-07-16 18:02:00 +00:00
|
|
|
oldStyleKeys.length === Object.keys(item).length &&
|
|
|
|
oldStyleKeys.every(k => k === 'sections' || oldStyle[k] === item[k]);
|
2018-01-01 17:02:49 +00:00
|
|
|
const codeEqual = oldStyle && styleSectionsEqual(oldStyle, item);
|
2017-07-12 18:17:04 +00:00
|
|
|
if (metaEqual && codeEqual) {
|
|
|
|
stats.unchanged.names.push(oldStyle.name);
|
|
|
|
stats.unchanged.ids.push(oldStyle.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return {oldStyle, metaEqual, codeEqual};
|
|
|
|
}
|
|
|
|
|
|
|
|
function sameStyle(oldStyle, newStyle) {
|
|
|
|
return oldStyle.name.trim() === newStyle.name.trim() ||
|
|
|
|
['updateUrl', 'originalMd5', 'originalDigest']
|
2017-07-16 18:02:00 +00:00
|
|
|
.some(field => oldStyle[field] && oldStyle[field] === newStyle[field]);
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function updateStats(style, {oldStyle, metaEqual, codeEqual}) {
|
2017-07-12 18:17:04 +00:00
|
|
|
if (!oldStyle) {
|
|
|
|
stats.added.names.push(style.name);
|
|
|
|
stats.added.ids.push(style.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!metaEqual && !codeEqual) {
|
|
|
|
stats.metaAndCode.names.push(reportNameChange(oldStyle, style));
|
|
|
|
stats.metaAndCode.ids.push(style.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!codeEqual) {
|
|
|
|
stats.codeOnly.names.push(style.name);
|
|
|
|
stats.codeOnly.ids.push(style.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stats.metaOnly.names.push(reportNameChange(oldStyle, style));
|
|
|
|
stats.metaOnly.ids.push(style.id);
|
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function done() {
|
2017-07-12 18:17:04 +00:00
|
|
|
const numChanged = stats.metaAndCode.names.length +
|
|
|
|
stats.metaOnly.names.length +
|
|
|
|
stats.codeOnly.names.length +
|
|
|
|
stats.added.names.length;
|
2018-11-07 06:09:29 +00:00
|
|
|
const report = Object.keys(stats)
|
|
|
|
.filter(kind => stats[kind].names.length)
|
|
|
|
.map(kind => {
|
|
|
|
const {ids, names, legend} = stats[kind];
|
|
|
|
const listItemsWithId = (name, i) =>
|
|
|
|
$create('div', {dataset: {id: ids[i]}}, name);
|
|
|
|
const listItems = name =>
|
|
|
|
$create('div', name);
|
|
|
|
const block =
|
|
|
|
$create('details', {dataset: {id: kind}}, [
|
|
|
|
$create('summary',
|
|
|
|
$create('b', names.length + ' ' + t(legend))),
|
|
|
|
$create('small',
|
|
|
|
names.map(ids ? listItemsWithId : listItems)),
|
|
|
|
]);
|
|
|
|
return block;
|
|
|
|
});
|
|
|
|
scrollTo(0, 0);
|
|
|
|
messageBox({
|
|
|
|
title: t('importReportTitle'),
|
|
|
|
contents: report.length ? report : t('importReportUnchanged'),
|
|
|
|
buttons: [t('confirmClose'), numChanged && t('undo')],
|
|
|
|
onshow: bindClick,
|
|
|
|
})
|
|
|
|
.then(({button}) => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (button === 1) {
|
2017-07-12 18:17:04 +00:00
|
|
|
undo();
|
|
|
|
}
|
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
return Promise.resolve(numChanged);
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function undo() {
|
|
|
|
const newIds = [
|
|
|
|
...stats.metaAndCode.ids,
|
|
|
|
...stats.metaOnly.ids,
|
|
|
|
...stats.codeOnly.ids,
|
|
|
|
...stats.added.ids,
|
|
|
|
];
|
2018-01-03 15:26:31 +00:00
|
|
|
let tasks = Promise.resolve();
|
|
|
|
for (const id of newIds) {
|
2018-11-07 06:09:29 +00:00
|
|
|
tasks = tasks.then(() => API.deleteStyle(id));
|
2018-01-03 15:26:31 +00:00
|
|
|
const oldStyle = oldStylesById.get(id);
|
|
|
|
if (oldStyle) {
|
2018-11-07 06:09:29 +00:00
|
|
|
tasks = tasks.then(() => API.importStyle(oldStyle));
|
2018-01-03 15:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// taskUI is superfast and updates style list only in this page,
|
|
|
|
// which should account for 99.99999999% of cases, supposedly
|
2018-11-07 06:09:29 +00:00
|
|
|
return tasks.then(() => messageBox({
|
|
|
|
title: t('importReportUndoneTitle'),
|
|
|
|
contents: newIds.length + ' ' + t('importReportUndone'),
|
|
|
|
buttons: [t('confirmClose')],
|
|
|
|
}));
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-16 19:40:13 +00:00
|
|
|
function bindClick() {
|
2017-07-12 18:17:04 +00:00
|
|
|
const highlightElement = event => {
|
|
|
|
const styleElement = $('#style-' + event.target.dataset.id);
|
|
|
|
if (styleElement) {
|
|
|
|
scrollElementIntoView(styleElement);
|
|
|
|
animateElement(styleElement);
|
|
|
|
}
|
|
|
|
};
|
2017-12-11 05:44:41 +00:00
|
|
|
for (const block of $$('#message-box details')) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (block.dataset.id !== 'invalid') {
|
2017-07-12 18:17:04 +00:00
|
|
|
block.style.cursor = 'pointer';
|
|
|
|
block.onclick = highlightElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function limitString(s, limit = 100) {
|
|
|
|
return s.length <= limit ? s : s.substr(0, limit) + '...';
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportNameChange(oldStyle, newStyle) {
|
2017-07-16 18:02:00 +00:00
|
|
|
return newStyle.name !== oldStyle.name
|
2017-07-12 18:17:04 +00:00
|
|
|
? oldStyle.name + ' —> ' + newStyle.name
|
|
|
|
: oldStyle.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function exportToFile() {
|
|
|
|
API.getAllStyles().then(styles => {
|
2018-01-07 11:25:39 +00:00
|
|
|
// https://crbug.com/714373
|
2018-08-02 12:52:34 +00:00
|
|
|
document.documentElement.appendChild(
|
|
|
|
$create('iframe', {
|
|
|
|
onload() {
|
|
|
|
const text = JSON.stringify(styles, null, '\t');
|
|
|
|
const type = 'application/json';
|
|
|
|
this.onload = null;
|
|
|
|
this.contentDocument.body.appendChild(
|
|
|
|
$create('a', {
|
|
|
|
href: URL.createObjectURL(new Blob([text], {type})),
|
|
|
|
download: generateFileName(),
|
|
|
|
type,
|
|
|
|
})
|
|
|
|
).dispatchEvent(new MouseEvent('click'));
|
|
|
|
},
|
|
|
|
// we can't use display:none as some browsers are ignoring such iframes
|
|
|
|
style: `
|
|
|
|
all: unset;
|
|
|
|
width: 0;
|
|
|
|
height: 0;
|
|
|
|
position: fixed;
|
|
|
|
opacity: 0;
|
|
|
|
border: none;
|
|
|
|
`.replace(/;/g, '!important;'),
|
2018-01-07 11:25:39 +00:00
|
|
|
})
|
2018-08-02 12:52:34 +00:00
|
|
|
);
|
2018-08-02 11:19:03 +00:00
|
|
|
// we don't remove the iframe or the object URL because the browser may show
|
|
|
|
// a download dialog and we don't know how long it'll take until the user confirms it
|
|
|
|
// (some browsers like Vivaldi can't download if we revoke the URL)
|
2017-10-14 01:25:46 +00:00
|
|
|
});
|
|
|
|
|
2017-07-12 18:17:04 +00:00
|
|
|
function generateFileName() {
|
|
|
|
const today = new Date();
|
|
|
|
const dd = ('0' + today.getDate()).substr(-2);
|
|
|
|
const mm = ('0' + (today.getMonth() + 1)).substr(-2);
|
|
|
|
const yyyy = today.getFullYear();
|
|
|
|
return `stylus-${yyyy}-${mm}-${dd}${STYLUS_BACKUP_FILE_EXT}`;
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|