From f8fc82cae7ba2679db77ef19d385a2d11a1e45f1 Mon Sep 17 00:00:00 2001 From: Jeremy Schomery Date: Sun, 5 Feb 2017 09:55:44 +0330 Subject: [PATCH] code cleanup; fileSaveLoad.js --- backup/fileSaveLoad.js | 54 ++++++++++++++++++++++++++++++++- backup/manage/fileSaveLoad.js | 57 ----------------------------------- manage.html | 1 - 3 files changed, 53 insertions(+), 59 deletions(-) delete mode 100644 backup/manage/fileSaveLoad.js diff --git a/backup/fileSaveLoad.js b/backup/fileSaveLoad.js index a2a971bd..b06becd5 100644 --- a/backup/fileSaveLoad.js +++ b/backup/fileSaveLoad.js @@ -1,6 +1,8 @@ +/* globals getStyles, saveStyle */ 'use strict'; -var STYLISH_DUMP_FILE_EXT = '.txt'; +var STYLISH_DUMP_FILE_EXT = '.txt'; +var STYLISH_DUMPFILE_EXTENSION = '.json'; var STYLISH_DEFAULT_SAVE_NAME = 'stylus-mm-dd-yyy' + STYLISH_DUMP_FILE_EXT; function saveAsFile (text, fileName, dialog) { @@ -50,3 +52,53 @@ function loadFromFile (formatToFilter) { fileInput.click(); }); } + +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').addEventListener('click', function () { + chrome.permissions.request({permissions: ['downloads']}, function (granted) { + if (granted) { + getStyles({}, function (styles) { + var text = JSON.stringify(styles); + saveAsFile(text, generateFileName()); + }); + } + }); +}); + +document.getElementById('unfile-all-styles').addEventListener('click', function () { + loadFromFile(STYLISH_DUMPFILE_EXTENSION).then(function (rawText) { + var json = JSON.parse(rawText); + var i = 0, nextStyle; + + function done() { + window.alert(i + ' styles installed/updated'); + location.reload(); + } + + function proceed() { + nextStyle = json[i++]; + if (nextStyle) { + saveStyle(nextStyle, proceed); + } + else { + i--; + done(); + } + } + + proceed(); + }); +}); diff --git a/backup/manage/fileSaveLoad.js b/backup/manage/fileSaveLoad.js deleted file mode 100644 index 40ef9f63..00000000 --- a/backup/manage/fileSaveLoad.js +++ /dev/null @@ -1,57 +0,0 @@ -var STYLISH_DUMPFILE_EXTENSION = ".json"; - -var saveButton = document.getElementById("file-all-styles"), - loadButton = document.getElementById("unfile-all-styles"); - -saveButton.addEventListener('click', onSaveToFileClick); -loadButton.addEventListener('click', onLoadFromFileClick); - -function onSaveToFileClick(){ - chrome.permissions.request({permissions: ['downloads']}, function(granted){ - if (granted){ - getStyles({}, function(styles){ - var text = JSON.stringify(styles); - saveAsFile(text, generateFileName()); - }); - } - }) -} - -function onLoadFromFileClick(){ - loadFromFile(STYLISH_DUMPFILE_EXTENSION).then(function(rawText){ - var json = JSON.parse(rawText); - - var i = 0, nextStyle; - - function proceed(){ - nextStyle = json[i++]; - if (nextStyle) { - saveStyle(nextStyle, proceed); - }else{ - i--; - done(); - } - } - - function done(){ - alert(i + " styles installed/updated"); - location.reload(); - } - - proceed(); - }); -} - -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 "stylish-" + today + STYLISH_DUMPFILE_EXTENSION; -} diff --git a/manage.html b/manage.html index c0133704..02a75955 100644 --- a/manage.html +++ b/manage.html @@ -215,6 +215,5 @@
-