2017-01-30 19:02:50 +00:00
|
|
|
var STYLISH_DUMP_FILE_EXT = ".txt";
|
|
|
|
var STYLISH_DEFAULT_SAVE_NAME = "stylus-mm-dd-yyy" + STYLISH_DUMP_FILE_EXT;
|
|
|
|
|
|
|
|
function saveAsFile(text, fileName, dialog) {
|
|
|
|
fileName = fileName || STYLISH_DEFAULT_SAVE_NAME;
|
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* !!works only when page has representation - backgound page won't work
|
|
|
|
*
|
|
|
|
* opens open file dialog,
|
|
|
|
* gets selected file,
|
|
|
|
* gets it's path,
|
|
|
|
* gets content of it by ajax
|
|
|
|
*/
|
|
|
|
function loadFromFile(formatToFilter){
|
|
|
|
return new Promise(function(resolve){
|
|
|
|
var fileInput = document.createElement('input');
|
|
|
|
fileInput.style = "display: none;";
|
|
|
|
fileInput.type = "file";
|
|
|
|
fileInput.accept = formatToFilter || STYLISH_DUMP_FILE_EXT;
|
|
|
|
fileInput.acceptCharset = "utf8";
|
|
|
|
|
|
|
|
document.body.appendChild(fileInput);
|
|
|
|
fileInput.initialValue = fileInput.value;
|
|
|
|
fileInput.addEventListener('change', changeHandler);
|
|
|
|
function changeHandler(){
|
2017-02-03 09:17:26 +00:00
|
|
|
if (fileInput.value !== fileInput.initialValue){
|
2017-01-30 19:02:50 +00:00
|
|
|
var fReader = new FileReader();
|
2017-02-03 09:17:26 +00:00
|
|
|
fReader.onloadend = function (event){
|
2017-01-30 19:02:50 +00:00
|
|
|
fileInput.removeEventListener('change', changeHandler);
|
|
|
|
fileInput.remove();
|
2017-02-03 09:17:26 +00:00
|
|
|
resolve(event.target.result);
|
|
|
|
};
|
|
|
|
fReader.readAsText(fileInput.files[0], 'utf-8')
|
2017-01-30 19:02:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fileInput.click();
|
|
|
|
});
|
2017-02-03 09:17:26 +00:00
|
|
|
}
|