2017-04-20 14:00:43 +00:00
|
|
|
/* global messageBox, handleUpdate, applyOnMessage */
|
2017-02-03 09:24:47 +00:00
|
|
|
'use strict';
|
2017-01-30 19:02:50 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
const STYLISH_DUMP_FILE_EXT = '.txt';
|
|
|
|
const STYLUS_BACKUP_FILE_EXT = '.json';
|
2017-01-30 19:02:50 +00:00
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
|
2017-03-15 18:18:17 +00:00
|
|
|
function importFromFile({fileTypeFilter, file} = {}) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const fileInput = document.createElement('input');
|
|
|
|
if (file) {
|
|
|
|
readFile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fileInput.style.display = 'none';
|
2017-02-03 09:24:47 +00:00
|
|
|
fileInput.type = 'file';
|
2017-03-15 18:18:17 +00:00
|
|
|
fileInput.accept = fileTypeFilter || STYLISH_DUMP_FILE_EXT;
|
2017-02-03 09:24:47 +00:00
|
|
|
fileInput.acceptCharset = 'utf-8';
|
2017-01-30 19:02:50 +00:00
|
|
|
|
2017-02-03 09:24:47 +00:00
|
|
|
document.body.appendChild(fileInput);
|
|
|
|
fileInput.initialValue = fileInput.value;
|
2017-03-15 18:18:17 +00:00
|
|
|
fileInput.onchange = readFile;
|
|
|
|
fileInput.click();
|
|
|
|
|
|
|
|
function readFile() {
|
|
|
|
if (file || fileInput.value !== fileInput.initialValue) {
|
|
|
|
file = file || fileInput.files[0];
|
2017-04-11 05:16:54 +00:00
|
|
|
if (file.size > 100e6) {
|
2017-03-15 18:18:17 +00:00
|
|
|
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 => {
|
2017-02-03 09:24:47 +00:00
|
|
|
fileInput.remove();
|
2017-03-15 18:18:17 +00:00
|
|
|
importFromString(event.target.result).then(numStyles => {
|
|
|
|
document.body.style.cursor = '';
|
|
|
|
resolve(numStyles);
|
|
|
|
});
|
2017-02-03 09:24:47 +00:00
|
|
|
};
|
2017-03-15 18:18:17 +00:00
|
|
|
fReader.readAsText(file, 'utf-8');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
|
2017-03-15 18:18:17 +00:00
|
|
|
function importFromString(jsonString) {
|
2017-04-11 10:51:40 +00:00
|
|
|
if (!BG) {
|
|
|
|
onBackgroundReady().then(() => importFromString(jsonString));
|
|
|
|
return;
|
|
|
|
}
|
2017-04-17 21:22:44 +00:00
|
|
|
// create objects in background context
|
|
|
|
const json = BG.tryJSONparse(jsonString) || [];
|
2017-04-11 10:51:40 +00:00
|
|
|
if (typeof json.slice != 'function') {
|
|
|
|
json.length = 0;
|
|
|
|
}
|
|
|
|
const oldStyles = json.length && BG.deepCopy(BG.cachedStyles.list || []);
|
2017-03-24 02:51:44 +00:00
|
|
|
const oldStylesByName = json.length && new Map(
|
|
|
|
oldStyles.map(style => [style.name.trim(), style]));
|
2017-04-26 21:49:03 +00:00
|
|
|
|
|
|
|
let oldDigests;
|
|
|
|
chrome.storage.local.get(null, data => (oldDigests = data));
|
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
const stats = {
|
2017-04-17 21:22:44 +00:00
|
|
|
added: {names: [], ids: [], legend: 'importReportLegendAdded'},
|
|
|
|
unchanged: {names: [], ids: [], legend: 'importReportLegendIdentical'},
|
2017-04-13 11:51:43 +00:00
|
|
|
metaAndCode: {names: [], ids: [], legend: 'importReportLegendUpdatedBoth'},
|
2017-04-17 21:22:44 +00:00
|
|
|
metaOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedMeta'},
|
|
|
|
codeOnly: {names: [], ids: [], legend: 'importReportLegendUpdatedCode'},
|
|
|
|
invalid: {names: [], legend: 'importReportLegendInvalid'},
|
2017-03-24 02:51:44 +00:00
|
|
|
};
|
2017-04-26 21:49:03 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
let index = 0;
|
2017-04-12 15:31:52 +00:00
|
|
|
let lastRenderTime = performance.now();
|
|
|
|
const renderQueue = [];
|
|
|
|
const RENDER_NAP_TIME_MAX = 1000; // ms
|
|
|
|
const RENDER_QUEUE_MAX = 50; // number of styles
|
2017-04-17 21:22:44 +00:00
|
|
|
const SAVE_OPTIONS = {reason: 'import', notify: false};
|
2017-04-26 21:49:03 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
return new Promise(proceed);
|
2017-03-15 18:18:17 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function proceed(resolve) {
|
|
|
|
while (index < json.length) {
|
|
|
|
const item = json[index++];
|
2017-04-17 21:22:44 +00:00
|
|
|
const info = analyze(item);
|
|
|
|
if (info) {
|
|
|
|
// using saveStyle directly since json was parsed in background page context
|
|
|
|
return BG.saveStyle(Object.assign(item, SAVE_OPTIONS))
|
|
|
|
.then(style => account({style, info, resolve}));
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 15:31:52 +00:00
|
|
|
renderQueue.forEach(style => handleUpdate(style, {reason: 'import'}));
|
|
|
|
renderQueue.length = 0;
|
2017-03-24 02:51:44 +00:00
|
|
|
done(resolve);
|
2017-03-15 18:18:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 21:22:44 +00:00
|
|
|
function analyze(item) {
|
|
|
|
if (!item || !item.name || !item.name.trim() || typeof item != 'object'
|
|
|
|
|| (item.sections && typeof item.sections.slice != 'function')) {
|
|
|
|
stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
item.name = item.name.trim();
|
|
|
|
const byId = BG.cachedStyles.byId.get(item.id);
|
|
|
|
const byName = oldStylesByName.get(item.name);
|
|
|
|
const oldStyle = byId && byId.name.trim() == item.name || !byName ? byId : byName;
|
|
|
|
if (oldStyle == byName && byName) {
|
|
|
|
item.id = byName.id;
|
|
|
|
}
|
|
|
|
const oldStyleKeys = oldStyle && Object.keys(oldStyle);
|
|
|
|
const metaEqual = oldStyleKeys &&
|
|
|
|
oldStyleKeys.length == Object.keys(item).length &&
|
|
|
|
oldStyleKeys.every(k => k == 'sections' || oldStyle[k] === item[k]);
|
|
|
|
const codeEqual = oldStyle && BG.styleSectionsEqual(oldStyle, item);
|
|
|
|
if (metaEqual && codeEqual) {
|
|
|
|
stats.unchanged.names.push(oldStyle.name);
|
|
|
|
stats.unchanged.ids.push(oldStyle.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return {oldStyle, metaEqual, codeEqual};
|
|
|
|
}
|
|
|
|
|
|
|
|
function account({style, info, resolve}) {
|
|
|
|
renderQueue.push(style);
|
|
|
|
if (performance.now() - lastRenderTime > RENDER_NAP_TIME_MAX
|
|
|
|
|| renderQueue.length > RENDER_QUEUE_MAX) {
|
|
|
|
renderQueue.forEach(style => handleUpdate(style, {reason: 'import'}));
|
|
|
|
setTimeout(scrollElementIntoView, 0, $('#style-' + renderQueue.pop().id));
|
|
|
|
renderQueue.length = 0;
|
|
|
|
lastRenderTime = performance.now();
|
|
|
|
}
|
|
|
|
setTimeout(proceed, 0, resolve);
|
|
|
|
const {oldStyle, metaEqual, codeEqual} = info;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function done(resolve) {
|
|
|
|
const numChanged = stats.metaAndCode.names.length +
|
|
|
|
stats.metaOnly.names.length +
|
|
|
|
stats.codeOnly.names.length +
|
|
|
|
stats.added.names.length;
|
2017-04-20 14:00:43 +00:00
|
|
|
Promise.resolve(numChanged && refreshAllTabs()).then(() => {
|
2017-03-24 02:51:44 +00:00
|
|
|
const report = Object.keys(stats)
|
|
|
|
.filter(kind => stats[kind].names.length)
|
2017-04-17 20:25:32 +00:00
|
|
|
.map(kind => {
|
|
|
|
const {ids, names, legend} = stats[kind];
|
|
|
|
const listItemsWithId = (name, i) =>
|
|
|
|
$element({dataset: {id: ids[i]}, textContent: name});
|
|
|
|
const listItems = name =>
|
|
|
|
$element({textContent: name});
|
|
|
|
const block =
|
|
|
|
$element({tag: 'details', dataset: {id: kind}, appendChild: [
|
|
|
|
$element({tag: 'summary', appendChild:
|
|
|
|
$element({tag: 'b', textContent: names.length + ' ' + t(legend)})
|
|
|
|
}),
|
|
|
|
$element({tag: 'small', appendChild:
|
|
|
|
names.map(ids ? listItemsWithId : listItems)
|
|
|
|
}),
|
|
|
|
]});
|
|
|
|
return block;
|
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
scrollTo(0, 0);
|
2017-03-25 19:50:37 +00:00
|
|
|
messageBox({
|
2017-04-13 11:51:43 +00:00
|
|
|
title: t('importReportTitle'),
|
2017-04-17 20:25:32 +00:00
|
|
|
contents: report.length ? report : t('importReportUnchanged'),
|
2017-03-24 02:51:44 +00:00
|
|
|
buttons: [t('confirmOK'), numChanged && t('undo')],
|
2017-03-25 19:50:37 +00:00
|
|
|
onshow: bindClick,
|
|
|
|
}).then(({button, enter, esc}) => {
|
2017-04-11 05:16:54 +00:00
|
|
|
if (button == 1) {
|
|
|
|
undo();
|
|
|
|
}
|
|
|
|
});
|
2017-03-24 02:51:44 +00:00
|
|
|
resolve(numChanged);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function undo() {
|
|
|
|
const oldStylesById = new Map(oldStyles.map(style => [style.id, style]));
|
|
|
|
const newIds = [
|
|
|
|
...stats.metaAndCode.ids,
|
|
|
|
...stats.metaOnly.ids,
|
|
|
|
...stats.codeOnly.ids,
|
|
|
|
...stats.added.ids,
|
|
|
|
];
|
2017-04-21 10:57:01 +00:00
|
|
|
let resolve;
|
2017-03-24 02:51:44 +00:00
|
|
|
index = 0;
|
2017-04-21 10:57:01 +00:00
|
|
|
return new Promise(resolve_ => {
|
|
|
|
resolve = resolve_;
|
|
|
|
undoNextId();
|
|
|
|
}).then(BG.refreshAllTabs)
|
2017-03-24 02:51:44 +00:00
|
|
|
.then(() => messageBox({
|
2017-04-13 11:51:43 +00:00
|
|
|
title: t('importReportUndoneTitle'),
|
|
|
|
contents: newIds.length + ' ' + t('importReportUndone'),
|
2017-03-24 02:51:44 +00:00
|
|
|
buttons: [t('confirmOK')],
|
|
|
|
}));
|
2017-04-21 10:57:01 +00:00
|
|
|
function undoNextId() {
|
2017-03-24 02:51:44 +00:00
|
|
|
if (index == newIds.length) {
|
|
|
|
resolve();
|
|
|
|
return;
|
2017-02-03 09:24:47 +00:00
|
|
|
}
|
2017-03-24 02:51:44 +00:00
|
|
|
const id = newIds[index++];
|
2017-04-11 10:51:40 +00:00
|
|
|
deleteStyleSafe({id, notify: false}).then(id => {
|
2017-03-24 02:51:44 +00:00
|
|
|
const oldStyle = oldStylesById.get(id);
|
|
|
|
if (oldStyle) {
|
2017-04-26 21:49:03 +00:00
|
|
|
oldStyle.styleDigest = oldDigests[BG.DIGEST_KEY_PREFIX + id];
|
2017-04-21 10:57:01 +00:00
|
|
|
saveStyleSafe(Object.assign(oldStyle, SAVE_OPTIONS))
|
|
|
|
.then(undoNextId);
|
2017-03-24 02:51:44 +00:00
|
|
|
} else {
|
2017-04-21 10:57:01 +00:00
|
|
|
undoNextId();
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
});
|
2017-02-03 09:24:47 +00:00
|
|
|
}
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function bindClick(box) {
|
2017-04-11 05:16:54 +00:00
|
|
|
const highlightElement = event => {
|
|
|
|
const styleElement = $('#style-' + event.target.dataset.id);
|
|
|
|
if (styleElement) {
|
|
|
|
scrollElementIntoView(styleElement);
|
|
|
|
animateElement(styleElement, {className: 'highlight'});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (const block of $$('details')) {
|
2017-03-24 02:51:44 +00:00
|
|
|
if (block.dataset.id != 'invalid') {
|
|
|
|
block.style.cursor = 'pointer';
|
2017-04-11 05:16:54 +00:00
|
|
|
block.onclick = highlightElement;
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function limitString(s, limit = 100) {
|
|
|
|
return s.length <= limit ? s : s.substr(0, limit) + '...';
|
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function reportNameChange(oldStyle, newStyle) {
|
|
|
|
return newStyle.name != oldStyle.name
|
|
|
|
? oldStyle.name + ' —> ' + newStyle.name
|
|
|
|
: oldStyle.name;
|
|
|
|
}
|
2017-04-20 14:00:43 +00:00
|
|
|
|
|
|
|
function refreshAllTabs() {
|
|
|
|
return getActiveTab().then(activeTab => new Promise(resolve => {
|
|
|
|
// list all tabs including chrome-extension:// which can be ours
|
|
|
|
chrome.tabs.query({}, tabs => {
|
|
|
|
const lastTab = tabs[tabs.length - 1];
|
|
|
|
for (const tab of tabs) {
|
|
|
|
getStylesSafe({matchUrl: tab.url, enabled: true, asHash: true}).then(styles => {
|
|
|
|
const message = {method: 'styleReplaceAll', styles};
|
|
|
|
if (tab.id == activeTab.id) {
|
|
|
|
applyOnMessage(message);
|
|
|
|
} else {
|
|
|
|
chrome.tabs.sendMessage(tab.id, message);
|
|
|
|
}
|
|
|
|
BG.updateIcon(tab, styles);
|
|
|
|
if (tab == lastTab) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
|
|
|
|
$('#file-all-styles').onclick = () => {
|
2017-04-26 21:49:03 +00:00
|
|
|
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;
|
|
|
|
});
|
2017-03-24 02:51:44 +00:00
|
|
|
const text = JSON.stringify(styles, null, '\t');
|
|
|
|
const fileName = generateFileName();
|
2017-03-06 14:44:20 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
const url = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text);
|
2017-03-08 08:13:35 +00:00
|
|
|
// for long URLs; https://github.com/schomery/stylish-chrome/issues/13#issuecomment-284582600
|
|
|
|
fetch(url)
|
|
|
|
.then(res => res.blob())
|
|
|
|
.then(blob => {
|
2017-04-17 20:28:40 +00:00
|
|
|
const objectURL = URL.createObjectURL(blob);
|
|
|
|
Object.assign(document.createElement('a'), {
|
|
|
|
download: fileName,
|
|
|
|
href: objectURL,
|
|
|
|
type: 'application/json',
|
|
|
|
}).dispatchEvent(new MouseEvent('click'));
|
|
|
|
setTimeout(() => URL.revokeObjectURL(objectURL));
|
2017-03-08 08:13:35 +00:00
|
|
|
});
|
2017-02-05 06:25:44 +00:00
|
|
|
});
|
2017-03-24 02:51:44 +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-${mm}-${dd}-${yyyy}${STYLUS_BACKUP_FILE_EXT}`;
|
|
|
|
}
|
2017-03-15 18:18:17 +00:00
|
|
|
};
|
2017-02-05 06:25:44 +00:00
|
|
|
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
$('#unfile-all-styles').onclick = () => {
|
2017-03-24 02:51:44 +00:00
|
|
|
importFromFile({fileTypeFilter: STYLUS_BACKUP_FILE_EXT});
|
2017-03-15 18:18:17 +00:00
|
|
|
};
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
Object.assign(document.body, {
|
|
|
|
ondragover(event) {
|
2017-03-15 18:18:17 +00:00
|
|
|
const hasFiles = event.dataTransfer.types.includes('Files');
|
|
|
|
event.dataTransfer.dropEffect = hasFiles || event.target.type == 'search' ? 'copy' : 'none';
|
2017-03-24 02:51:44 +00:00
|
|
|
this.classList.toggle('dropzone', hasFiles);
|
2017-03-15 18:18:17 +00:00
|
|
|
if (hasFiles) {
|
|
|
|
event.preventDefault();
|
2017-03-24 02:51:44 +00:00
|
|
|
clearTimeout(this.fadeoutTimer);
|
|
|
|
this.classList.remove('fadeout');
|
2017-02-05 06:25:44 +00:00
|
|
|
}
|
2017-03-15 18:18:17 +00:00
|
|
|
},
|
2017-03-24 02:51:44 +00:00
|
|
|
ondragend(event) {
|
2017-03-25 05:54:58 +00:00
|
|
|
animateElement(this, {className: 'fadeout'}).then(() => {
|
2017-03-24 02:51:44 +00:00
|
|
|
this.style.animationDuration = '';
|
2017-03-25 05:54:58 +00:00
|
|
|
this.classList.remove('dropzone');
|
2017-03-24 02:51:44 +00:00
|
|
|
});
|
2017-03-15 18:18:17 +00:00
|
|
|
},
|
2017-03-24 02:51:44 +00:00
|
|
|
ondragleave(event) {
|
2017-03-15 18:18:17 +00:00
|
|
|
// Chrome sets screen coords to 0 on Escape key pressed or mouse out of document bounds
|
|
|
|
if (!event.screenX && !event.screenX) {
|
2017-03-24 02:51:44 +00:00
|
|
|
this.ondragend();
|
2017-03-15 18:18:17 +00:00
|
|
|
}
|
|
|
|
},
|
2017-03-24 02:51:44 +00:00
|
|
|
ondrop(event) {
|
|
|
|
this.ondragend();
|
2017-03-15 18:18:17 +00:00
|
|
|
if (event.dataTransfer.files.length) {
|
|
|
|
event.preventDefault();
|
2017-04-26 10:56:32 +00:00
|
|
|
if ($('#onlyUpdates input').checked) {
|
|
|
|
$('#onlyUpdates input').click();
|
|
|
|
}
|
2017-03-24 02:51:44 +00:00
|
|
|
importFromFile({file: event.dataTransfer.files[0]});
|
2017-03-15 18:18:17 +00:00
|
|
|
}
|
|
|
|
},
|
2017-02-05 06:25:44 +00:00
|
|
|
});
|