2017-03-14 23:27:52 +00:00
|
|
|
/* globals getStyles, saveStyle, invalidateCache, refreshAllTabs, handleUpdate */
|
2017-02-03 09:24:47 +00:00
|
|
|
'use strict';
|
2017-01-30 19:02:50 +00:00
|
|
|
|
2017-02-05 06:25:44 +00:00
|
|
|
var STYLISH_DUMP_FILE_EXT = '.txt';
|
|
|
|
var STYLISH_DUMPFILE_EXTENSION = '.json';
|
2017-03-08 08:13:35 +00:00
|
|
|
var STYLISH_DEFAULT_SAVE_NAME = 'stylus-mm-dd-yyyy' + STYLISH_DUMP_FILE_EXT;
|
2017-01-30 19:02:50 +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];
|
|
|
|
if (file.size > 100*1000*1000) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function importFromString(jsonString) {
|
|
|
|
const json = runTryCatch(() => Array.from(JSON.parse(jsonString))) || [];
|
|
|
|
const numStyles = json.length;
|
|
|
|
|
|
|
|
if (numStyles) {
|
|
|
|
invalidateCache(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
proceed();
|
|
|
|
function proceed() {
|
|
|
|
const nextStyle = json.shift();
|
|
|
|
if (nextStyle) {
|
|
|
|
saveStyle(nextStyle, {notify: false}).then(style => {
|
2017-03-23 04:47:30 +00:00
|
|
|
handleUpdate(style, {reason: 'import'});
|
2017-03-15 18:18:17 +00:00
|
|
|
setTimeout(proceed, 0);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
refreshAllTabs().then(() => {
|
2017-03-21 01:32:38 +00:00
|
|
|
scrollTo(0, 0);
|
2017-03-15 18:18:17 +00:00
|
|
|
setTimeout(alert, 100, numStyles + ' styles installed/updated');
|
|
|
|
resolve(numStyles);
|
|
|
|
});
|
2017-02-03 09:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-02-03 09:17:26 +00:00
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
|
|
|
|
function generateFileName() {
|
|
|
|
var today = new Date();
|
|
|
|
var dd = '0' + today.getDate();
|
|
|
|
var mm = '0' + (today.getMonth() + 1);
|
|
|
|
var yyyy = today.getFullYear();
|
|
|
|
|
|
|
|
dd = dd.substr(-2);
|
|
|
|
mm = mm.substr(-2);
|
|
|
|
|
|
|
|
today = mm + '-' + dd + '-' + yyyy;
|
|
|
|
|
|
|
|
return 'stylus-' + today + STYLISH_DUMPFILE_EXTENSION;
|
|
|
|
}
|
|
|
|
|
2017-03-15 18:18:17 +00:00
|
|
|
document.getElementById('file-all-styles').onclick = () => {
|
2017-03-06 14:44:20 +00:00
|
|
|
getStyles({}, function (styles) {
|
2017-03-15 00:54:51 +00:00
|
|
|
let text = JSON.stringify(styles, null, '\t');
|
2017-03-06 14:44:20 +00:00
|
|
|
let fileName = generateFileName() || STYLISH_DEFAULT_SAVE_NAME;
|
|
|
|
|
2017-03-08 08:13:35 +00:00
|
|
|
let url = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text);
|
|
|
|
// for long URLs; https://github.com/schomery/stylish-chrome/issues/13#issuecomment-284582600
|
|
|
|
fetch(url)
|
|
|
|
.then(res => res.blob())
|
|
|
|
.then(blob => {
|
|
|
|
let a = document.createElement('a');
|
|
|
|
a.setAttribute('download', fileName);
|
|
|
|
a.setAttribute('href', URL.createObjectURL(blob));
|
|
|
|
a.dispatchEvent(new MouseEvent('click'));
|
|
|
|
});
|
2017-02-05 06:25:44 +00:00
|
|
|
});
|
2017-03-15 18:18:17 +00:00
|
|
|
};
|
2017-02-05 06:25:44 +00:00
|
|
|
|
|
|
|
|
2017-03-15 18:18:17 +00:00
|
|
|
document.getElementById('unfile-all-styles').onclick = () => {
|
|
|
|
importFromFile({fileTypeFilter: STYLISH_DUMPFILE_EXTENSION});
|
|
|
|
};
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-15 18:18:17 +00:00
|
|
|
const dropTarget = Object.assign(document.body, {
|
|
|
|
ondragover: event => {
|
|
|
|
const hasFiles = event.dataTransfer.types.includes('Files');
|
|
|
|
event.dataTransfer.dropEffect = hasFiles || event.target.type == 'search' ? 'copy' : 'none';
|
|
|
|
dropTarget.classList.toggle('dropzone', hasFiles);
|
|
|
|
if (hasFiles) {
|
|
|
|
event.preventDefault();
|
|
|
|
clearTimeout(dropTarget.fadeoutTimer);
|
|
|
|
dropTarget.classList.remove('fadeout');
|
2017-02-05 06:25:44 +00:00
|
|
|
}
|
2017-03-15 18:18:17 +00:00
|
|
|
},
|
|
|
|
ondragend: event => {
|
|
|
|
dropTarget.classList.add('fadeout');
|
|
|
|
// transitionend event may not fire if the user switched to another tab so we'll use a timer
|
|
|
|
clearTimeout(dropTarget.fadeoutTimer);
|
|
|
|
dropTarget.fadeoutTimer = setTimeout(() => {
|
|
|
|
dropTarget.classList.remove('dropzone', 'fadeout');
|
|
|
|
}, 250);
|
|
|
|
},
|
|
|
|
ondragleave: event => {
|
|
|
|
// Chrome sets screen coords to 0 on Escape key pressed or mouse out of document bounds
|
|
|
|
if (!event.screenX && !event.screenX) {
|
|
|
|
dropTarget.ondragend();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ondrop: event => {
|
|
|
|
if (event.dataTransfer.files.length) {
|
|
|
|
event.preventDefault();
|
|
|
|
importFromFile({file: event.dataTransfer.files[0]}).then(() => {
|
|
|
|
dropTarget.classList.remove('dropzone');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dropTarget.ondragend();
|
|
|
|
}
|
|
|
|
},
|
2017-02-05 06:25:44 +00:00
|
|
|
});
|