forked from c3cbot/c3c-ufc-utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
95 lines (91 loc) · 3.98 KB
/
popup.js
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
var stringToBlob = function (str, mimetype) {
var raw = str;
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
for (var i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
var bb = new Blob([uInt8Array.buffer], {
type: mimetype
});
return bb;
};
window.onload = function () {
document.getElementById("import").onchange = function (e) {
if (e.currentTarget.files[0]) {
console.log(e.currentTarget.files[0]);
if (e.currentTarget.files[0].type != "application/json") {
alert("Invalid file given");
}
var fr = new FileReader();
fr.readAsText(e.currentTarget.files[0], "UTF-8");
fr.onload = function (evt) {
try {
var j = JSON.parse(evt.target.result);
if (Array.isArray(j)) {
chrome.cookies.getAll({
domain: "facebook.com"
}, async function (cookies) {
for (let i in cookies) {
await new Promise(resolve => {
chrome.cookies.remove({
url: `https://facebook.com${cookies[i].path}`,
name: cookies[i].name
}, resolve);
});
}
for (let i in j) {
if (j[i].domain == "facebook.com") {
await new Promise(resolve => {
chrome.cookies.set({
url: `https://facebook.com${j[i].path}`,
name: j[i].key,
value: j[i].value,
expirationDate: (Date.now() / 1000) + (84600 * 30),
domain: ".facebook.com"
}, resolve);
});
}
}
chrome.tabs.query({ active: true }, function (tabs) {
var { host } = new URL(tabs[0].url);
if (host.split(".")[1] == "facebook") {
chrome.tabs.update(tabs[0].id, { url: tabs[0].url });
}
});
});
} else {
alert("Invalid JSON file (not a FBState JSON file).");
}
} catch (_) {
alert("Failed to load JSON file (malformed?)");
}
}
}
}
document.getElementById("export").onclick = function () {
chrome.cookies.getAll({
domain: "facebook.com"
}, function (cookies) {
var cok = cookies.map(v => ({
key: v.name,
value: v.value,
domain: "facebook.com",
path: v.path,
hostOnly: v.hostOnly,
creation: new Date().toISOString(),
lastAccessed: new Date().toISOString()
}));
var fbstate = JSON.stringify(cok, null, 4);
var blob = stringToBlob(fbstate, "application/json");
var url = window.webkitURL || window.URL || window.mozURL || window.msURL;
var a = document.createElement('a');
a.download = 'fbstate.json';
a.href = url.createObjectURL(blob);
a.textContent = '';
a.dataset.downloadurl = ['json', a.download, a.href].join(':');
a.click();
a.remove();
});
}
}