Skip to content

Commit

Permalink
Preference to change maximum cookie expiry from 15 to 1-30 days.
Browse files Browse the repository at this point in the history
  • Loading branch information
svandragt committed Mar 31, 2022
1 parent 7753991 commit 93fe106
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
21 changes: 19 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
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) {
Expand All @@ -14,10 +32,9 @@ function cookieChanged(changeInfo) {
}

const cookie = changeInfo.cookie;
const maxCookieAgeDays = 15;
const bufferTimeMinutes = 10;

const maxAllowedExpiration = Math.round((new Date).getTime() / 1000) + (maxCookieAgeDays * 3600 * 24);
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?
Expand Down
16 changes: 10 additions & 6 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
"manifest_version": 2,
"name": "Fresh Cookies",
"description": "Limit the maximum age of web cookies for your privacy benefit.",
"version": "0.3.1",
"version": "0.4.0",

"applications": {
"gecko": {
"strict_min_version": "54.0a1"
}
"options_ui": {
"page": "options.html"
},

"background": {
"scripts": ["background.js"]
},

"permissions": [ "cookies", "http://*/*", "https://*/*" ]
"permissions": ["cookies", "http://*/*", "https://*/*", "storage"],

"browser_specific_settings": {
"gecko": {
"id": "[email protected]"
}
}
}
24 changes: 24 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
</head>

<body>

<form style="padding-top:0.5em">
<label><strong>Maximum cookie expiry:</strong><br>
<input type="range" step="1" min="1" max="30" value="15" id="maxExpiryDays" style="width:70%">
<input type="text" id="cc" type="number" size=1 min="1" max="30"> days
</label>
<div style="margin-top: 0.5em">
<button type="submit">Save</button>
</div>
</form>

<script src="options.js"></script>

</body>

</html>
36 changes: 36 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function saveOptions(e) {
e.preventDefault();
browser.storage.sync.set({
maxExpiryDays: document.querySelector("#maxExpiryDays").value
});
browser.runtime.reload();
}

function restoreOptions() {

function setCurrentChoice(result) {
document.querySelector("#maxExpiryDays").value = result.maxExpiryDays || "15";
document.querySelector("#cc").value = result.maxExpiryDays || "15";
}

function onError(error) {
console.log(`Error: ${error}`);
}

let getting = browser.storage.sync.get("maxExpiryDays");
getting.then(setCurrentChoice, onError);
document.querySelector("#maxExpiryDays")
}

document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);
let myinput = document.querySelector("#maxExpiryDays");
myinput.addEventListener("input", () => {
document.getElementById("cc").value = myinput.value;
});

let cc = document.querySelector("#cc");
cc.addEventListener("input", () => {
document.getElementById("maxExpiryDays").value = cc.value;
});
document.addEventListener("DOMContentLoaded", restoreOptions);

0 comments on commit 93fe106

Please sign in to comment.