Skip to content

Commit 127c5b6

Browse files
committed
summarize import, export and delete data into one page: manage data, download json seems not to be compatible in Firefox
1 parent bcea4c6 commit 127c5b6

18 files changed

+196877
-152
lines changed

build/js/background.js

+37,444-17
Large diffs are not rendered by default.

build/js/content.js

+42,828-19
Large diffs are not rendered by default.

build/js/manage.js

+30,555
Large diffs are not rendered by default.

build/js/me.js

+64,981-33
Large diffs are not rendered by default.

build/js/options.js

+10,281-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/js/popup.js

+10,287-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/views/manage.html

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Data Selfie _ Manage Data</title>
7+
<style>
8+
body {
9+
background-color: #000;
10+
color: #FFF;
11+
font-family: "Menlo", monospace;
12+
line-height: 1.2;
13+
font-size: 0.8em;
14+
}
15+
16+
#data {
17+
width: calc(100% - 40px);
18+
padding: 20px;
19+
height: 350px;
20+
border: 1px solid #FFF;
21+
overflow-y: scroll;
22+
overflow-x: hidden;
23+
}
24+
25+
#actions {
26+
margin: 20px 0;
27+
}
28+
29+
a,
30+
a:focus {
31+
color: #FF00B1;
32+
padding: 0;
33+
margin: 2px 0;
34+
outline: 0;
35+
text-decoration: none;
36+
}
37+
38+
a:hover {
39+
cursor: pointer;
40+
}
41+
42+
button,
43+
button:focus {
44+
width: 100px;
45+
margin: 0 0 30px 0;
46+
background-color: #FFF;
47+
border: 1px solid #000;
48+
padding: 10px 0;
49+
outline: 0;
50+
}
51+
52+
button:hover {
53+
background-color: #000;
54+
color: #FFF;
55+
border: 1px solid #FFF;
56+
cursor: pointer;
57+
}
58+
59+
p {
60+
padding: 0;
61+
margin: 5px 0;
62+
}
63+
64+
</style>
65+
</head>
66+
67+
<body>
68+
<div id="data"></div>
69+
<div id="actions">
70+
<a href="">Import Data</a>
71+
<br>
72+
<input style="display:none;" type="file" id="choose-file" />
73+
<p>// e.g. if you have used Data Selfie in a different browser or computer before</p>
74+
<button id="import">Import</button>
75+
<br>
76+
<a href="">Export Your Data</a>
77+
<br>
78+
<p>// download your data as a json file</p>
79+
<button id="backup">Export</button>
80+
<br>
81+
<a href="">Delete Your Data</a>
82+
<br>
83+
<p>// remove any records from Data Selfie from your computer, this is final, there is no other place your data is stored</p>
84+
<button id="delete">Delete</button>
85+
<br>
86+
<script src="../js/manage.js"></script>
87+
</div>
88+
</body>
89+
90+
</html>

build/views/options.html

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<head>
55
<title>Data Selfie _ Options</title>
66
<style>
7+
body {
8+
font-family: sans-serif;
9+
}
710
.container {
811
width: 100%;
912
max-width: 400px;

build/views/popup.html

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
button:hover {
2424
background-color: #000;
2525
color: #FFF;
26+
border: 1px solid #FFF;
2627
cursor: pointer;
2728
}
2829

@@ -59,10 +60,7 @@
5960
</div>
6061
<p id="status">Collect and analyze data from your interactions on facebook.com.</p>
6162
<button id="me" inactive>Your Data Selfie</button>
62-
<button id="backup">Export Your Data</button>
63-
<button id="import">Import Data</button>
64-
<input style="display:none;" type="file" id="choose-file" />
65-
<button id="delete">Delete Your Data</button>
63+
<button id="manage">Manage Your Data</button>
6664
<button id="options">Options</button>
6765
<script src="../js/popup.js"></script>
6866
</body>

src/js/background.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,17 @@ function generalListeners() {
8585
});
8686
break;
8787
case "backup":
88-
console.log("test just callback")
89-
// helper.backup(db);
88+
helper.backup(db);
9089
break;
9190
case "import":
92-
console.log("import data");
9391
if (req.data.dataselfie != undefined) {
94-
helper.import(db, req.data.dataselfie);
92+
helper.import(db, req.data.dataselfie, sender.tab.id);
9593
} else {
96-
helper.importError();
94+
helper.importError(sender.tab.id);
9795
}
9896
break;
9997
case "delete":
100-
helper.resetDB(db, initDB);
98+
helper.resetDB(db, initDB, sender.tab.id);
10199
chrome.storage.local.clear(initOptions);
102100
break;
103101
case "saveLooked":

src/js/background_helpers.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ module.exports = {
3838
url: "data:text/json," + JSON.stringify(data, null, 2),
3939
filename: "./dataselfie_" + moment().format('YYYY-MM-DD') + ".json",
4040
conflictAction: "overwrite", // "uniquify" / "overwrite" / "prompt"
41-
saveAs: false, // true gives save-as dialogue
41+
saveAs: false // true gives save-as dialogue
4242
}, this.downloadBar());
4343
},
4444
backup: function(_db) {
45+
// FIX downloads permission might not be needed anymore, now in Manage Your Data page
4546
var obj = { dataselfie: {} };
4647
var self = this;
4748
_db.transaction('r', _db.tables, function() {
@@ -56,11 +57,12 @@ module.exports = {
5657
console.error(err.stack);
5758
});
5859
},
59-
importError: function() {
60+
importError: function(tabid) {
6061
console.log("%c[DB][<<] import (json) error", clog.magenta);
6162
alert("There has been error. Please make sure your file is a valid json and try importing again.");
63+
chrome.tabs.sendMessage(tabid, { displaydata: true });
6264
},
63-
import: function(_db, data) {
65+
import: function(_db, data, tabid) {
6466
var self = this;
6567
_db.transaction("rw", _db.looked, _db.clicked, _db.typed, _db.timespent, _db.pages, function() {
6668
for (var i = 0; i < data.looked.length; i++) {
@@ -81,19 +83,21 @@ module.exports = {
8183
}).then(function() {
8284
console.log("%c[DB][<<] import complete", clog.magenta);
8385
alert("Data was imported.");
86+
chrome.tabs.sendMessage(tabid, { displaydata: true });
8487
}).catch(function(err) {
8588
console.error(err.stack);
8689
self.importError();
8790
});
8891
},
89-
resetDB: function(_db, _callback) {
92+
resetDB: function(_db, _callback, tabid) {
9093
_db.delete().then(function() {
9194
console.log("%c[DB][<<] deleted", clog.magenta);
9295
}).catch(function(err) {
9396
console.error("%cCould not delete [DB][<<]", clog.magenta);
9497
}).finally(function() {
9598
if (_callback) { _callback(); };
9699
alert("Database was deleted.");
100+
chrome.tabs.sendMessage(tabid, { displaydata: true });
97101
});
98102
},
99103
checkCrash: function(_db, _table) {
@@ -129,4 +133,4 @@ module.exports = {
129133
// http://stackoverflow.com/a/1144788
130134
return str.replace(new RegExp(this.escapeString(find), 'g'), replace);
131135
}
132-
}
136+
}

src/js/libs/filesaver.js

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/* FileSaver.js
2+
* A saveAs() FileSaver implementation.
3+
* 1.3.2
4+
* 2016-06-16 18:25:19
5+
*
6+
* By Eli Grey, http://eligrey.com
7+
* License: MIT
8+
* See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
9+
*/
10+
11+
/*global self */
12+
/*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
13+
14+
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
15+
16+
var saveAs = saveAs || (function(view) {
17+
"use strict";
18+
// IE <10 is explicitly unsupported
19+
if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
20+
return;
21+
}
22+
var
23+
doc = view.document
24+
// only get URL when necessary in case Blob.js hasn't overridden it yet
25+
, get_URL = function() {
26+
return view.URL || view.webkitURL || view;
27+
}
28+
, save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
29+
, can_use_save_link = "download" in save_link
30+
, click = function(node) {
31+
var event = new MouseEvent("click");
32+
node.dispatchEvent(event);
33+
}
34+
, is_safari = /constructor/i.test(view.HTMLElement) || view.safari
35+
, is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent)
36+
, throw_outside = function(ex) {
37+
(view.setImmediate || view.setTimeout)(function() {
38+
throw ex;
39+
}, 0);
40+
}
41+
, force_saveable_type = "application/octet-stream"
42+
// the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
43+
, arbitrary_revoke_timeout = 1000 * 40 // in ms
44+
, revoke = function(file) {
45+
var revoker = function() {
46+
if (typeof file === "string") { // file is an object URL
47+
get_URL().revokeObjectURL(file);
48+
} else { // file is a File
49+
file.remove();
50+
}
51+
};
52+
setTimeout(revoker, arbitrary_revoke_timeout);
53+
}
54+
, dispatch = function(filesaver, event_types, event) {
55+
event_types = [].concat(event_types);
56+
var i = event_types.length;
57+
while (i--) {
58+
var listener = filesaver["on" + event_types[i]];
59+
if (typeof listener === "function") {
60+
try {
61+
listener.call(filesaver, event || filesaver);
62+
} catch (ex) {
63+
throw_outside(ex);
64+
}
65+
}
66+
}
67+
}
68+
, auto_bom = function(blob) {
69+
// prepend BOM for UTF-8 XML and text/* types (including HTML)
70+
// note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
71+
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
72+
return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
73+
}
74+
return blob;
75+
}
76+
, FileSaver = function(blob, name, no_auto_bom) {
77+
if (!no_auto_bom) {
78+
blob = auto_bom(blob);
79+
}
80+
// First try a.download, then web filesystem, then object URLs
81+
var
82+
filesaver = this
83+
, type = blob.type
84+
, force = type === force_saveable_type
85+
, object_url
86+
, dispatch_all = function() {
87+
dispatch(filesaver, "writestart progress write writeend".split(" "));
88+
}
89+
// on any filesys errors revert to saving with object URLs
90+
, fs_error = function() {
91+
if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
92+
// Safari doesn't allow downloading of blob urls
93+
var reader = new FileReader();
94+
reader.onloadend = function() {
95+
var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
96+
var popup = view.open(url, '_blank');
97+
if(!popup) view.location.href = url;
98+
url=undefined; // release reference before dispatching
99+
filesaver.readyState = filesaver.DONE;
100+
dispatch_all();
101+
};
102+
reader.readAsDataURL(blob);
103+
filesaver.readyState = filesaver.INIT;
104+
return;
105+
}
106+
// don't create more object URLs than needed
107+
if (!object_url) {
108+
object_url = get_URL().createObjectURL(blob);
109+
}
110+
if (force) {
111+
view.location.href = object_url;
112+
} else {
113+
var opened = view.open(object_url, "_blank");
114+
if (!opened) {
115+
// Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
116+
view.location.href = object_url;
117+
}
118+
}
119+
filesaver.readyState = filesaver.DONE;
120+
dispatch_all();
121+
revoke(object_url);
122+
}
123+
;
124+
filesaver.readyState = filesaver.INIT;
125+
126+
if (can_use_save_link) {
127+
object_url = get_URL().createObjectURL(blob);
128+
setTimeout(function() {
129+
save_link.href = object_url;
130+
save_link.download = name;
131+
click(save_link);
132+
dispatch_all();
133+
revoke(object_url);
134+
filesaver.readyState = filesaver.DONE;
135+
});
136+
return;
137+
}
138+
139+
fs_error();
140+
}
141+
, FS_proto = FileSaver.prototype
142+
, saveAs = function(blob, name, no_auto_bom) {
143+
return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
144+
}
145+
;
146+
// IE 10+ (native saveAs)
147+
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
148+
return function(blob, name, no_auto_bom) {
149+
name = name || blob.name || "download";
150+
151+
if (!no_auto_bom) {
152+
blob = auto_bom(blob);
153+
}
154+
return navigator.msSaveOrOpenBlob(blob, name);
155+
};
156+
}
157+
158+
FS_proto.abort = function(){};
159+
FS_proto.readyState = FS_proto.INIT = 0;
160+
FS_proto.WRITING = 1;
161+
FS_proto.DONE = 2;
162+
163+
FS_proto.error =
164+
FS_proto.onwritestart =
165+
FS_proto.onprogress =
166+
FS_proto.onwrite =
167+
FS_proto.onabort =
168+
FS_proto.onerror =
169+
FS_proto.onwriteend =
170+
null;
171+
172+
return saveAs;
173+
}(
174+
typeof self !== "undefined" && self
175+
|| typeof window !== "undefined" && window
176+
|| this.content
177+
));
178+
// `self` is undefined in Firefox for Android content script context
179+
// while `this` is nsIContentFrameMessageManager
180+
// with an attribute `content` that corresponds to the window
181+
182+
if (typeof module !== "undefined" && module.exports) {
183+
module.exports.saveAs = saveAs;
184+
} else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {
185+
define("FileSaver.js", function() {
186+
return saveAs;
187+
});
188+
}

0 commit comments

Comments
 (0)