-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
64 lines (50 loc) · 2.01 KB
/
background.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
const _browser = browser || chrome;
let _settings = {
'maxExpiryDays': 15,
};
_browser.cookies.onChanged.addListener(cookieChanged);
function onError(error) {
console.error(error);
}
function onGot(item) {
if (item.maxExpiryDays) {
_settings.maxExpiryDays = item.maxExpiryDays;
console.info("Changed _settings.maxExpiryDays:" + _settings.maxExpiryDays);
}
}
let getting = browser.storage.sync.get("maxExpiryDays");
getting.then(onGot, onError);
console.info('Fresh Cookies loaded.');
function isExpiredChangeEvent(changeInfo) {
return changeInfo.cause == "expired" || changeInfo.cause == "evicted" || changeInfo.removed;
}
function cookieChanged(changeInfo) {
if (isExpiredChangeEvent(changeInfo)) {
return;
}
const cookie = changeInfo.cookie;
const bufferTimeMinutes = 10;
const maxAllowedExpiration = Math.round((new Date).getTime() / 1000) + (_settings.maxExpiryDays * 3600 * 24);
if (!cookie.session && cookie.expirationDate != undefined && cookie.expirationDate > maxAllowedExpiration + (bufferTimeMinutes * 60)) {
// TODO can I just clone cookie and amend?
var newCookie = {};
newCookie.name = cookie.name;
newCookie.value = cookie.value;
newCookie.path = cookie.path;
newCookie.secure = cookie.secure;
newCookie.httpOnly = cookie.httpOnly;
newCookie.storeId = cookie.storeId;
newCookie.expirationDate = maxAllowedExpiration;
//If no real url is available use: "https://" : "http://" + domain + path
newCookie.url = "http" + ((cookie.secure) ? "s" : "") + "://" + cookie.domain.substring(1) + cookie.path;
if (!cookie.hostOnly) {
newCookie.domain = cookie.domain;
}
let thenCookieSet = _browser.cookies.set(newCookie).then(ck => {
console.info("Cookie Shortened! Name:'" + ck.name + "' to '" + ck.expirationDate + "'");
return ck;
}, reason => {
console.error(reason); // Error!
});
}
}