From ec19a8dc884309a228118bfe877b7a7e12bdba1c Mon Sep 17 00:00:00 2001 From: listen1 Date: Fri, 13 May 2016 13:12:02 +0800 Subject: [PATCH] add backup and recover function --- js/app.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ listen1.html | 14 +++++++++++++ manifest.json | 2 +- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/js/app.js b/js/app.js index d6822e2..8ece46a 100644 --- a/js/app.js +++ b/js/app.js @@ -366,8 +366,64 @@ }); }; + $scope.backupMySettings = function() { + var items = {}; + for ( var i = 0, len = localStorage.length; i < len; ++i ) { + var key = localStorage.key(i); + var value = localStorage.getObject(key); + items[key] = value; + } + + var result = JSON.stringify(items); + var url = 'data:application/json,' + result; + chrome.downloads.download({ + url: url, + filename: 'listen1_backup.json' + }); + } + + $scope.importMySettings = function(event) { + console.log('start'); + var fileObject = event.target.files[0]; + if (fileObject == null ){ + Notification.warning("请选择备份文件"); + return; + } + var reader = new FileReader(); + reader.onloadend = function(readerEvent) { + if (readerEvent.target.readyState == FileReader.DONE) { + var data_json = readerEvent.target.result; + // parse json + var data = null; + try{ + data = JSON.parse(data_json); + }catch(e){ + } + if(data == null) { + Notification.warning("备份文件格式错误,请重新选择"); + return; + } + for ( var key in data) { + var value = data[key]; + localStorage.setObject(key, value); + } + Notification.success("恢复我的歌单成功"); + } + }; + reader.readAsText(fileObject); + } }]); + app.directive('customOnChange', function() { + return { + restrict: 'A', + link: function (scope, element, attrs) { + var onChangeHandler = scope.$eval(attrs.customOnChange); + element.bind('change', onChangeHandler); + } + }; + }); + app.controller('PlayController', ['$scope', '$timeout','$log', '$anchorScroll', '$location', 'angularPlayer', '$http', '$httpParamSerializerJQLike','$rootScope', 'Notification','loWeb', diff --git a/listen1.html b/listen1.html index d5dee6e..2bbc2a1 100644 --- a/listen1.html +++ b/listen1.html @@ -220,6 +220,20 @@
豆瓣:
豆瓣:
--> +
数据备份
+
+

重装插件或清除缓存数据会导致我的歌单数据丢失,强烈建议在这些操作前,备份我的歌单。

+
+ +
+
+
数据恢复
+
+

选择备份文件,恢复我的歌单。注意:恢复我的歌单会覆盖现有的歌单。

+ +
关于

Listen 1 (Chrome Extension) 主页: https://github.com/listen1/listen1_chrome_extension

diff --git a/manifest.json b/manifest.json index d262409..c0ec40a 100644 --- a/manifest.json +++ b/manifest.json @@ -15,7 +15,7 @@ }, "manifest_version": 2, "name": "Listen 1", - "permissions": [ "notifications", "unlimitedStorage", "storage", "contextMenus", "tabs", "*://music.163.com/*", "*://*.xiami.com/*", "*://*.qq.com/*", "webRequest", "webRequestBlocking"], + "permissions": [ "notifications", "unlimitedStorage", "downloads", "storage", "contextMenus", "tabs", "*://music.163.com/*", "*://*.xiami.com/*", "*://*.qq.com/*", "webRequest", "webRequestBlocking"], "version": "1.0", "web_accessible_resources": [ "images/*" ] }