-
Notifications
You must be signed in to change notification settings - Fork 8
/
popup.js
270 lines (223 loc) · 7.64 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
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
document
.getElementById("restore")
.addEventListener("change", handleFileSelect, false);
document
.getElementById("dec-passwd-form")
.addEventListener("submit", handleDecPasswdSubmit, false);
document
.getElementById("enc-passwd-form")
.addEventListener("submit", handleEncPasswdSubmit, false);
document.getElementById("btn-backup").onclick = showEncPasswordInputBox;
document.getElementById("btn-upload-fallback").onclick = showFallbackCkzInput;
function handleEncPasswdSubmit(e) {
e.preventDefault();
const pass = getEncPasswd();
chrome.cookies.getAll({}, (cookies) => {
if (cookies.length > 0) {
const data = sjcl.encrypt(pass, JSON.stringify(cookies), { ks: 256 });
// only using en-GB because it puts the date first
const d = new Date()
const date = d.toLocaleDateString("en-GB").replace(/\//g, "-");
const time = d.toLocaleTimeString("en-GB").replace(/:/g, "-");
const filename = `cookies-${date}-${time}.ckz`;
downloadJson(data, filename)
backupSuccessAlert(cookies.length)
} else {
alert("No cookies to backup!");
}
});
}
let cookieFile;
function handleFileSelect(e) {
cookieFile = e.target.files[0];
if (!cookieFile || !cookieFile.name.endsWith(".ckz")) {
alert("Not a .ckz file. Please select again!");
hideDecPasswordInputBox()
return;
}
hideFallbackCkzButton()
showDecPasswordInputBox()
}
function handleDecPasswdSubmit(e) {
e.preventDefault();
const pass = getDecPasswd()
getCkzFileDataAsText(async (data) => {
let cookies;
try {
const decrypted = sjcl.decrypt(pass, data)
cookies = JSON.parse(decrypted);
} catch (error) {
console.log(error);
if (error instanceof sjcl.exception.corrupt) {
alert("Password incorrect!");
} else if (error instanceof sjcl.exception.invalid) {
alert("File is not a valid .ckz file!");
} else {
alert("Unknown error!");
}
return;
}
// initialize progress bar
initRestoreProgressBar(cookies.length)
let total = 0;
// lets save some syscalls by defining it once up here
// if i call it in the loop, its not gonna be very slow but hey,
// whose that concerned about that much accuracy of cookie expriation dates
const epoch = new Date().getTime() / 1000;
for (const cookie of cookies) {
let url =
"http" +
(cookie.secure ? "s" : "") +
"://" +
(cookie.domain.startsWith(".")
? cookie.domain.slice(1)
: cookie.domain) +
cookie.path;
if (epoch > cookie.expirationDate) {
expirationWarning(cookie.name, url)
continue;
}
if (cookie.hostOnly == true) {
// https://developer.chrome.com/extensions/cookies#method-set
// if the cookie is hostOnly, we don't
// supply the domain because that sets hostOnly to true
delete cookie.domain;
}
if (cookie.session == true) {
// if session is true, then expirationDate
// needs to be omitted
delete cookie.expirationDate;
}
// .set doesn't accepts these
delete cookie.hostOnly;
delete cookie.session;
// .set wants url
cookie.url = url;
let c = await new Promise((resolve, reject) => {
chrome.cookies.set(cookie, resolve);
});
if (c == null) {
console.error(
"Error while restoring the cookie for the URL " + cookie.url
);
console.error(JSON.stringify(cookie));
console.error(JSON.stringify(chrome.runtime.lastError));
unknownErrWarning(cookie.name, cookie.url)
} else {
total++;
updateRestoreProgressBar(total)
}
}
// update messages
restoreSuccessAlert(total, cookies.length)
// hide progress bar
hideRestoreProgressBar()
})
}
// NOTE: most of these methods are shallow, but i wanted to separate application logic from the DOM
function createWarning(text) {
const div = document.createElement("div");
div.classList.add("alert", "alert-warning");
div.innerHTML = text;
return div;
}
function createSuccessAlert(text) {
const div = document.createElement("div");
div.classList.add("alert", "alert-success");
div.innerHTML = text;
return div;
}
function unknownErrWarning(cookie_name, cookie_url) {
if (cookie_name && cookie_url) {
addToWarningMessageList(createWarning(`Cookie ${cookie_name} for the domain ${cookie_url} could not be restored`))
}
}
function expirationWarning(cookie_name, cookie_url) {
if (cookie_name && cookie_url) {
addToWarningMessageList(createWarning(`Cookie ${cookie_name} for the domain ${cookie_url} has expired`))
}
}
function backupSuccessAlert(totalCookies) {
addToSuccessMessageList(createSuccessAlert(`Successfully backed up <b>${totalCookies.toLocaleString()}</b> cookies!`))
}
function restoreSuccessAlert(restoredCookies, totalCookies) {
addToSuccessMessageList(createSuccessAlert(`Successfully restored <b>${restoredCookies.toLocaleString()}</b> cookies out of <b>${totalCookies.toLocaleString()}</b>`));
}
function hideBackupButton() {
document.getElementById("btn-backup").style.display = "none";
}
function showEncPasswordInputBox(e) {
hideBackupButton()
document.getElementById("enc-passwd").style.display = "flex";
// activate the input box
document.getElementById("inp-enc-passwd").focus();
}
function showDecPasswordInputBox(e) {
document.getElementById("dec-passwd").style.display = "flex";
document.getElementById("inp-dec-passwd").focus()
}
function hideDecPasswordInputBox(e) {
document.getElementById("dec-passwd").style.display = "none";
}
function addToSuccessMessageList(node) {
document.getElementById("messages").appendChild(node)
}
function addToWarningMessageList(node) {
document.getElementById("warnings").appendChild(node)
}
function getEncPasswd() {
return document.getElementById("inp-enc-passwd").value.trim();
}
function getDecPasswd() {
return document.getElementById("inp-dec-passwd").value.trim();
}
function initRestoreProgressBar(maxVal) {
document.getElementById("progress").style.display = "block";
document.getElementById("progressbar").setAttribute("max", maxVal);
}
function updateRestoreProgressBar(val) {
document.getElementById("progressbar").setAttribute("value", val);
}
function hideRestoreProgressBar() {
document.getElementById("progressbar").setAttribute("value", 0);
document.getElementById("progress").style.display = "none";
}
function hideFallbackCkzButton() {
document.getElementById("btn-upload-fallback").style.display = "none"
}
function showFallbackCkzInput() {
hideFallbackCkzButton()
document.getElementById("restore-upload-wrap").style.display = "none"
// show the fallback
document.getElementById("restore-using-text-wrap").style.display = "flex"
document.getElementById("dec-passwd").style.display = "flex";
}
function getCkzFileContentsFromTextarea() {
return document.getElementById("ckz-textarea").value.trim()
}
function downloadJson(data, filename) {
const blob = new Blob([data], { type: "application/ckz" });
const url = URL.createObjectURL(blob);
chrome.downloads.download({ url: url, filename: filename }, (id) => {
chrome.downloads.onChanged.addListener((delta) => {
if (delta?.state?.current == "complete") {
chrome.downloads.show(id)
}
})
});
}
function getCkzFileDataAsText(cb) {
if (cookieFile) {
const reader = new FileReader();
reader.readAsText(cookieFile);
reader.onload = (e) => {
cb(e.target.result);
}
reader.onerror = (e) => {
console.error(e);
alert("Unknown error while reading the .ckz file!");
}
} else {
cb(getCkzFileContentsFromTextarea())
}
}