-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.html
More file actions
executable file
·109 lines (82 loc) · 3.01 KB
/
Copy pathexport.html
File metadata and controls
executable file
·109 lines (82 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ライブをメモするLivelog</title>
</head>
<body>
<p><a href="index.html"><</a></p>
<hr>
<h1>EXPORT</h1>
<div id="form">
<p>以下のリンクからJSONファイルをダウンロード、またはテキストエリアからコピー&ペーストして保存してください。</p>
<p><a id="download" target="_blank">ダウンロード</a></p>
<p>
現在登録されているデータ内容:<br>
<textarea id="copyArea" cols="30" rows="10"></textarea>
</p>
</div>
<hr>
<p>Developed on <a href="https://github.com/livelogjp/livelog-alpha">GitHub</a>.</p>
<script>
function $(id){return document.getElementById(id);}
function getDate(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var date = date.getDate();
if (month < 10) {
month = "0" + month;
}
if (date < 10) {
date = "0" + date;
}
//取得した年、月、日と文字列に変換
var year = String(year) ;
var month = String(month) ;
var date = String(date) ;
var strDate = year + month + date;
return strDate;
}
//☆エラーの時にフォームを消してエラー文言を表示する関数
function displayError() {
var errorMessage = document.createElement('p');
errorMessage.innerText = 'データが正常に読み込まれませんでした。';
var errorLink = document.createElement('p');
errorLink.innerHTML = '<a href="index.html">ライブ一覧へ戻る</a>';
$('form').innerHTML = '';
$('form').appendChild(errorMessage);
$('form').appendChild(errorLink);
}
//☆localStorageにデータが登録されていたらエクスポート用のリンクを表示する処理
if(localStorage.length > 0) {
//localStorageのkeyを格納する配列を生成
var key = new Array();
//エクスポート用のデータを格納する連想配列を生成
var saveData = {};
//localStorageの全データをsaveDataオブジェクトに格納
for(var i=0; i<localStorage.length; i++) {
key[i] = localStorage.key(i);
saveData[key[i]] = JSON.parse(
localStorage.getItem(key[i])
);
}
//saveDataオブジェクトを整形済みのJSON文字列に変換
var saveData = JSON.stringify(saveData, null , "\t");
//テキストエリアにエクスポート用データを挿入
$('copyArea').value = saveData;
//ダウンロード用のBlobオブジェクトを生成
var blob = new Blob([saveData], {type: "text/plain;charset=utf-8"});
window.URL = window.URL || window.webkitURL;
//ダウンロード用のリンクに生成されたBlobオブジェクトを指定
$('download').setAttribute('href', window.URL.createObjectURL(blob));
//ダウンロード用のファイル名を指定
$('download').setAttribute('download', 'livelog_' + getDate() + '.json');
} else {
//localStorageにデータが登録されていなかったらエラー表示
displayError();
}
</script>
</body>
</html>