stylus/backup/fileSaveLoad.js

149 lines
4.6 KiB
JavaScript
Raw Normal View History

/* 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';
var STYLISH_DEFAULT_SAVE_NAME = 'stylus-mm-dd-yyyy' + STYLISH_DUMP_FILE_EXT;
2017-01-30 19:02:50 +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';
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;
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();
importFromString(event.target.result).then(numStyles => {
document.body.style.cursor = '';
resolve(numStyles);
});
2017-02-03 09:24:47 +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 => {
handleUpdate(style, {reason: 'import'});
setTimeout(proceed, 0);
});
} else {
refreshAllTabs().then(() => {
scrollTo(0, 0);
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;
}
document.getElementById('file-all-styles').onclick = () => {
2017-03-06 14:44:20 +00:00
getStyles({}, function (styles) {
let text = JSON.stringify(styles, null, '\t');
2017-03-06 14:44:20 +00:00
let fileName = generateFileName() || STYLISH_DEFAULT_SAVE_NAME;
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-02-05 06:25:44 +00:00
document.getElementById('unfile-all-styles').onclick = () => {
importFromFile({fileTypeFilter: STYLISH_DUMPFILE_EXTENSION});
};
2017-02-05 06:25:44 +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
}
},
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
});