Skip to content

Commit

Permalink
Fix newCookie.url
Browse files Browse the repository at this point in the history
  • Loading branch information
svandragt committed Mar 31, 2022
1 parent 57ff80c commit 57b5bf6
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _browser = browser || chrome;

_browser.cookies.onChanged.addListener(cookieChanged);
console.info('Fresh Cookies loaded.')

function cookieChanged(changeInfo) {
if (changeInfo.cause == "expired" || changeInfo.cause == "evicted" || changeInfo.removed) {
Expand All @@ -9,15 +10,24 @@ function cookieChanged(changeInfo) {

const cookie = changeInfo.cookie;
const maxCookieAge = 15; // days
const bufferTime = 60 * 10 // minutes

const maxAllowedExpiration = Math.round((new Date).getTime() / 1000) + (maxCookieAge * 3600 * 24);

if (cookie.expirationDate != undefined && cookie.expirationDate > maxAllowedExpiration + 600 && !cookie.session) {
if (!cookie.session && cookie.expirationDate != undefined && cookie.expirationDate > maxAllowedExpiration + bufferTime) {
// TODO can I just clone cookie and amend?
var newCookie = {};
//If no real url is available use: "https://" : "http://" + domain + path
newCookie.url = "http" + ((cookie.secure) ? "s" : "") + "://" + cookie.domain + cookie.path;
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) {
if (console) {
console.debug(cookie.hostOnly, cookie.domain, newCookie.url);
Expand All @@ -27,12 +37,7 @@ function cookieChanged(changeInfo) {
newCookie.domain = cookie.domain;
}
}
newCookie.path = cookie.path;
newCookie.secure = cookie.secure;
newCookie.httpOnly = cookie.httpOnly;
newCookie.storeId = cookie.storeId;

newCookie.expirationDate = maxAllowedExpiration;
let thenCookieSet = _browser.cookies.set(newCookie).then(cookie => {
console.info("Cookie Shortened! Name:'" + cookie.name + "' to '" + maxAllowedExpiration + "'");
return cookie;
Expand Down

0 comments on commit 57b5bf6

Please sign in to comment.