restyling backup/fileSaveLoad.js
This commit is contained in:
parent
92d0acfbe3
commit
b07cf590b9
|
@ -1,14 +1,20 @@
|
||||||
var STYLISH_DUMP_FILE_EXT = ".txt";
|
'use strict';
|
||||||
var STYLISH_DEFAULT_SAVE_NAME = "stylus-mm-dd-yyy" + STYLISH_DUMP_FILE_EXT;
|
|
||||||
|
|
||||||
function saveAsFile(text, fileName, dialog) {
|
var STYLISH_DUMP_FILE_EXT = '.txt';
|
||||||
fileName = fileName || STYLISH_DEFAULT_SAVE_NAME;
|
var STYLISH_DEFAULT_SAVE_NAME = 'stylus-mm-dd-yyy' + STYLISH_DUMP_FILE_EXT;
|
||||||
dialog = typeof dialog === "boolean" ? dialog : true;
|
|
||||||
|
|
||||||
return new Promise(function(resolve){
|
function saveAsFile (text, fileName, dialog) {
|
||||||
var fileContent = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text);
|
fileName = fileName || STYLISH_DEFAULT_SAVE_NAME;
|
||||||
chrome.downloads.download({filename: fileName, saveAs: true, url: fileContent}, resolve)
|
dialog = typeof dialog === 'boolean' ? dialog : true;
|
||||||
});
|
|
||||||
|
return new Promise(function (resolve) {
|
||||||
|
var fileContent = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text);
|
||||||
|
chrome.downloads.download({
|
||||||
|
filename: fileName,
|
||||||
|
saveAs: true,
|
||||||
|
url: fileContent
|
||||||
|
}, resolve);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,28 +25,28 @@ function saveAsFile(text, fileName, dialog) {
|
||||||
* gets it's path,
|
* gets it's path,
|
||||||
* gets content of it by ajax
|
* gets content of it by ajax
|
||||||
*/
|
*/
|
||||||
function loadFromFile(formatToFilter){
|
function loadFromFile (formatToFilter) {
|
||||||
return new Promise(function(resolve){
|
return new Promise(function (resolve) {
|
||||||
var fileInput = document.createElement('input');
|
var fileInput = document.createElement('input');
|
||||||
fileInput.style = "display: none;";
|
fileInput.style = 'display: none;';
|
||||||
fileInput.type = "file";
|
fileInput.type = 'file';
|
||||||
fileInput.accept = formatToFilter || STYLISH_DUMP_FILE_EXT;
|
fileInput.accept = formatToFilter || STYLISH_DUMP_FILE_EXT;
|
||||||
fileInput.acceptCharset = "utf8";
|
fileInput.acceptCharset = 'utf-8';
|
||||||
|
|
||||||
document.body.appendChild(fileInput);
|
document.body.appendChild(fileInput);
|
||||||
fileInput.initialValue = fileInput.value;
|
fileInput.initialValue = fileInput.value;
|
||||||
fileInput.addEventListener('change', changeHandler);
|
function changeHandler() {
|
||||||
function changeHandler(){
|
if (fileInput.value !== fileInput.initialValue) {
|
||||||
if (fileInput.value !== fileInput.initialValue){
|
var fReader = new FileReader();
|
||||||
var fReader = new FileReader();
|
fReader.onloadend = function (event) {
|
||||||
fReader.onloadend = function (event){
|
fileInput.removeEventListener('change', changeHandler);
|
||||||
fileInput.removeEventListener('change', changeHandler);
|
fileInput.remove();
|
||||||
fileInput.remove();
|
resolve(event.target.result);
|
||||||
resolve(event.target.result);
|
};
|
||||||
};
|
fReader.readAsText(fileInput.files[0], 'utf-8');
|
||||||
fReader.readAsText(fileInput.files[0], 'utf-8')
|
}
|
||||||
}
|
}
|
||||||
}
|
fileInput.addEventListener('change', changeHandler);
|
||||||
fileInput.click();
|
fileInput.click();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user