-
Notifications
You must be signed in to change notification settings - Fork 2
/
ffosexporter.html
340 lines (301 loc) · 15.2 KB
/
ffosexporter.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Firefox OS Data Exporter</title>
<style type="text/css">
div.tab, div.tab:target ~ #introduction { display: none }
div.tab:target, #introduction { display: block }
.menu { background: linear-gradient(to bottom, #FF9500 0%, #E66000 100%); padding: 0.4em 1em; border-radius: 1em; }
.menu a { background: linear-gradient(to bottom, #D4DDE4 0%, #EAEFF2 100%); padding: 0.2em 0.6em; border-radius: 0.2em; }
</style>
<script type="text/javascript">
<!-- Default header lines recommended by Mozilla -->
//prefixes of implementation that we want to test
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
//prefixes of window.IDB objects
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange
if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB.")
}
var processedId = 0;
var output = "[\n";
var count = 0;
var objectStoreNum = -1;
function processObjectStore(event) {
if (objectStoreNum < 0) {
document.getElementById("progress").textContent += "Done. ";
return "";
}
var objectStoreName = event.target.result.objectStoreNames.item(objectStoreNum);
document.getElementById("progress").textContent += "Processing " + objectStoreName + "... ";
var objectStore = db.transaction(objectStoreName).objectStore(objectStoreName);
request = objectStore.openCursor(IDBKeyRange.lowerBound(processedId));
var cursor;
var onsuccesshandler = function(event2) {
var cursor = event2.target.result;
if (cursor) {
output += JSON.stringify(cursor.value) + ",\n";
processedId = cursor.value.id;
// DEBUGGING: Real live proved that broken entries exist - it's not clear how these entries were created,
// but they have to be handled. It turned out that advancing beyond the end will trigger the same
// behaviour. This will simulate that every 20th entry is broken.
//if (processedId % 20 === 0)
// cursor.advance(200000);
cursor.continue();
} else {
output = output.substring(0, output.length - 2);
output += "\n]";
var form = document.getElementById('objectstores');
var textfield = document.createElement('textarea');
var p = document.createElement('p');
var heading = document.createElement('b');
var button_calendar2ics = document.createElement('button');
var button_sms2commhistory = document.createElement('button');
button_calendar2ics.setAttribute('type', 'button');
button_calendar2ics.setAttribute('onclick', 'makeResult("calendar2ics", "' + objectStoreName + '");');
button_calendar2ics.innerHTML = 'Convert Calendar data to ICS file';
button_sms2commhistory.setAttribute('type', 'button');
button_sms2commhistory.setAttribute('onclick', 'makeResult("sms2commhistory", "' + objectStoreName + '");');
button_sms2commhistory.setAttribute('style', 'margin-left:20px;');
button_sms2commhistory.innerHTML = 'Convert SMS data to commhistory-tools JSON file';
textfield.setAttribute('rows', 10);
textfield.setAttribute('style', 'width:100%;');
textfield.setAttribute('id', objectStoreName);
textfield.value = output;
heading.textContent = objectStoreName;
p.appendChild(heading);
form.appendChild(p);
form.appendChild(textfield);
form.appendChild(button_calendar2ics);
form.appendChild(button_sms2commhistory);
processedId = 0;
output = "[\n";
objectStoreNum--;
processObjectStore(event);
}
}
var onerrorhandler = function(event2) {
document.getElementById("progress").textContent += "Error decoding entry " + processedId + ". ";
console.log("error (" + event2.target.error.name + "): " + event2.target.error.message);
processedId++;
// On error the database has to be opened again - resume at the last processed ID.
processDatabase();
}
request.onsuccess = onsuccesshandler;
request.onerror = onerrorhandler;
}
function exportDatabase() {
var form = document.getElementById("objectstores");
while (form.firstChild)
form.removeChild(form.firstChild);
processDatabase();
}
function processDatabase() {
databaseName = document.getElementById("db").value;
var request = window.indexedDB.open(databaseName);
request.onerror = function(event) {
console.log("error: ");
document.getElementById("progress").textContent = "An error occured - see console log for details.";
};
request.onsuccess = function(event) {
db = request.result;
console.log("success: " + db);
if (objectStoreNum < 0)
objectStoreNum = event.target.result.objectStoreNames.length - 1;
processObjectStore(event);
};
}
function escapeIcs(s) {
var s = s.replace(/\\/g, "\\\\");
s = s.replace(/;/g, "\\;");
s = s.replace(/,/g, "\\,");
s = s.replace(/\n/g, "\\n");
return s;
}
function escapeJson(s) {
var s = s.replace(/\\/g, "\\\\");
s = s.replace(/\n/g, "\\n");
s = s.replace(/"/g, "\\\"");
return s;
}
function transform2ICS(jsonContent) {
var numEntries = 0;
var result = "";
result += "BEGIN:VCALENDAR\n";
result += "PRODID:-//digitalimagecorp.de//Firefox OS calendar converter//EN\n";
result += "VERSION:2.0\n";
for (var line in jsonContent) {
try {
// Skip all non-local entries - they have been synced from somewhere and need no backup
if (jsonContent[line]["calendarId"] != "local-first")
continue;
numEntries++;
result += "BEGIN:VEVENT\n";
var now = new Date();
result += "DTSTAMP:" + ("000" + now.getFullYear()).slice(-4) + ("0" + (now.getMonth() + 1)).slice(-2) + ("0" + now.getDate()).slice(-2) + "T" + ("0" + now.getHours()).slice(-2) + ("0" + now.getMinutes()).slice(-2) + ("0" + now.getSeconds()).slice(-2) + "Z\n";
result += "UID:" + jsonContent[line]["remote"]["id"] + "\n";
result += "SUMMARY:" + escapeIcs(jsonContent[line]["remote"]["title"]) + "\n";
if (jsonContent[line]["remote"]["location"])
result += "LOCATION:" + escapeIcs(jsonContent[line]["remote"]["location"]) + "\n";
if (jsonContent[line]["remote"]["description"])
result += "DESCRIPTION:" + escapeIcs(jsonContent[line]["remote"]["description"]) + "\n";
// Despite its name the timestamp is *not* UTC, so the offset has to be added / substracted
var start = new Date(jsonContent[line]["remote"]["start"]["utc"] - jsonContent[line]["remote"]["start"]["offset"]);
var end = new Date(jsonContent[line]["remote"]["end"]["utc"] - jsonContent[line]["remote"]["start"]["offset"]);
// "All day" events will either be saved with the corresponding attribute "isDate" (FFOS 1.1) or just span the whole day (FFOS 1.3)
if (jsonContent[line]["remote"]["start"]["isDate"] || ((start.getHours() + start.getMinutes() + start.getSeconds() + end.getHours() + end.getMinutes() + end.getSeconds()) == 0)) {
result += "DTSTART;VALUE=DATE:" + ("000" + start.getFullYear()).slice(-4) + ("0" + (start.getMonth() + 1)).slice(-2) + ("0" + start.getDate()).slice(-2) + "\n";
result += "DTEND;VALUE=DATE:" + ("000" + end.getFullYear()).slice(-4) + ("0" + (end.getMonth() + 1)).slice(-2) + ("0" + end.getDate()).slice(-2) + "\n";
} else {
result += "DTSTART:" + ("000" + start.getFullYear()).slice(-4) + ("0" + (start.getMonth() + 1)).slice(-2) + ("0" + start.getDate()).slice(-2) + "T" + ("0" + start.getHours()).slice(-2) + ("0" + start.getMinutes()).slice(-2) + ("0" + start.getSeconds()).slice(-2) + "\n";
result += "DTEND:" + ("000" + end.getFullYear()).slice(-4) + ("0" + (end.getMonth() + 1)).slice(-2) + ("0" + end.getDate()).slice(-2) + "T" + ("0" + end.getHours()).slice(-2) + ("0" + end.getMinutes()).slice(-2) + ("0" + end.getSeconds()).slice(-2) + "\n";
}
result += "TRANSP:OPAQUE\n";
for (var alarm in jsonContent[line]["remote"]["alarms"]) {
result += "BEGIN:VALARM\n";
result += "DESCRIPTION:\n";
result += "ACTION:DISPLAY\n";
if (jsonContent[line]["remote"]["alarms"][alarm]["trigger"] < 0)
jsonContent[line]["remote"]["alarms"][alarm]["trigger"] = "-PT" + -jsonContent[line]["remote"]["alarms"][alarm]["trigger"] + "S";
else
jsonContent[line]["remote"]["alarms"][alarm]["trigger"] = "PT" + jsonContent[line]["remote"]["alarms"][alarm]["trigger"] + "S";
result += "TRIGGER;VALUE=DURATION:" + jsonContent[line]["remote"]["alarms"][alarm]["trigger"] + "\n";
result += "END:VALARM\n";
}
result += "END:VEVENT\n";
} catch(e) {
return "Error transforming line " + numEntries + ":\n\t" + JSON.stringify(jsonContent[line]) +
"\n\nEither this wasn't the object store containing calendar data or you have found a bug.\n" +
"If you think you got the correct field please report a bug on\nhttps://github.com/laenion/Firefox-OS-Data-Exporter/issues.\n\n" +
"Error message: " + e;
}
}
result += "END:VCALENDAR\n";
if (numEntries)
return result;
else
return "Could not find any entries - probably this wasn't the object store containing the calendar data...";
}
function transform2commhistory(jsonContent) {
var isFirst = true;
var result = "";
result += "[\n";
for (var line in jsonContent) {
try {
var direction = "";
// commhistory-tool can't handle MMS messages, so they have to be skipped
if (jsonContent[line]["type"] == "mms")
continue;
if (! isFirst)
result += '\t},\n';
isFirst = false;
result += '\t{\n';
result += '\t\t"type": "' + jsonContent[line]["type"] + '",\n';
if (jsonContent[line]["sender"]) {
result += '\t\t"to": "' + jsonContent[line]["sender"] + '",\n';
direction = 'in';
}
if (jsonContent[line]["receiver"]) {
result += '\t\t"to": "' + jsonContent[line]["receiver"] + '",\n';
direction = 'out';
}
result += '\t\t"events": [\n';
result += '\t\t\t{\n';
result += '\t\t\t\t"direction": "' + direction + '",\n';
result += '\t\t\t\t"date": "' + new Date(jsonContent[line]["timestamp"]).toISOString() + '",\n';
if (jsonContent[line]["read"] == false)
result += '\t\t\t\t"unread": true,\n';
result += '\t\t\t\t"text": "' + escapeJson(jsonContent[line]["body"]) + '"\n';
result += '\t\t\t}\n';
result += '\t\t]\n';
} catch(e) {
return "Error transforming the following line:\n\t" + JSON.stringify(jsonContent[line]) +
"\n\nEither this wasn't the object store containing SMS data or you have found a bug.\n" +
"If you think you got the correct field please report a bug on\nhttps://github.com/laenion/Firefox-OS-Data-Exporter/issues.";
}
}
result += '\t}\n';
result += ']\n';
if (! isFirst)
return result;
else
return "Could not find any entries - probably this wasn't the object store containing the SMS data...";
}
function makeResult(type, fieldname) {
try {
jsonContent = JSON.parse(document.getElementById(fieldname).value);
} catch(e) {
document.getElementById("processfield").value = "Error parsing data - this should never happen if you didn't modify the data.\n" +
"Please report a bug on https://github.com/laenion/Firefox-OS-Data-Exporter/issues\n" +
"and attach the line mentioned in the following error if possible:\n\n" + e;
location.href = "#process";
return;
}
switch (type) {
case "calendar2ics":
result = transform2ICS(jsonContent);
extension = ".ics";
document.getElementById("processfield").mimetype = 'text/calendar';
break;
case "sms2commhistory":
result = transform2commhistory(jsonContent);
extension = ".json";
document.getElementById("processfield").mimetype = "application/json";
break;
default:
result = "Error: The specified converter was not found.";
}
document.getElementById("processfield").value = result;
location.href = "#process";
document.getElementById("hiddenSaveLink").download = fieldname + extension;
}
function save() {
var blob = new Blob([document.getElementById("processfield").value], {type:document.getElementById("processfield").mimetype});
document.getElementById("hiddenSaveLink").href = window.URL.createObjectURL(blob);
document.getElementById("hiddenSaveLink").click();
}
</script>
</head>
<body>
<h1>Firefox OS data exporter</h1>
<div class="menu"><a href="#introduction">Introduction</a> <a href="#database">Export database</a> <a href="#process">Result</a></div>
<div class="tab" id="process">
<h2>Result</h2>
<p>
<textarea id="processfield" rows=30 style="width:100%;" ></textarea>
<a id="hiddenSaveLink" style="display:none;">Download</a>
<button onclick="save()">Save</button>
</p>
</div>
<div class="tab" id="database">
<h2>Export database</h2>
<form onsubmit="exportDatabase()" action="javascript:void(0);">
Database name: <input type="text" id="db" value="b2g-calendar"/><br />
<button type="submit">Export</button><br />
</form>
<pre id="progress"></pre>
<form id="objectstores"></form>
</div>
<div class="tab" id="introduction">
<h2>Introduction</h2>
<p>This website will <u>help you back up your personal data of your Firefox OS device</u>. Technically this means: It will create human readable JSON output out of the binary IndexedDB databases.<p>
<p>In a second step the JSON output can be used to export your data into any other format. The following converters are available out of the box:
<ul>
<li><b>Calendar:</b> Create standard ICS files from Firefox OS offline calendar data</li>
<li><b>SMS:</b> Create JSON files processable by <i>commhistory-tool</i> (e.g. for the <a href="http://jolla.com">Jolla</a> phone) of Firefox OS SMS data</li>
</ul>
<p>While this website's intention is to help Firefox OS users, you can of course use it to export any IndexedDB data.</p>
<p>If you just want to <b>view</b> IndexedDB databases you don't need this website at all: Firefox has an integrated viewer called <a href="https://developer.mozilla.org/de/docs/Tools/Storage_Inspector">Storage Inspector</a> already, however you cannot export any data with it.</p>
<h2>System requirements</h2>
<ul>
<li><a href="https://www.mozilla.org/firefox/desktop/">Mozilla Firefox</a> (tested with versions 38esr & 45esr)</li>
<li><a href="http://digitalimagecorp.de/flatpress/index.php/category/firefox-os/">A backup of the userdata of your Firefox OS phone</a></li>
</ul>
<h2>Usage</h2>
<p>See <a href="http://digitalimagecorp.de/flatpress/index.php/category/firefox-os/">the Firefox OS category in my blog</a> for instructions how to use this website.</p>
<p>All data is processed locally on your PC, no data is sent to my server. You may also get the source code from <a href="https://github.com/laenion/Firefox-OS-Data-Exporter">https://github.com/laenion/Firefox-OS-Data-Exporter</a>.</p>
</div>
</body>
</html>