Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode dayStreak cookie with base64 #1741

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions intranet/static/themes/snow/snow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@
* Credits: dmorris, zyaro
* Modified for Ion (Intranet3) by: 2016jwoglom, 2025azhu
*/

function encodeBase64(str) {
return btoa(unescape(encodeURIComponent(str)));
}

function decodeBase64(str) {
try {
return decodeURIComponent(escape(atob(str)));
} catch (e) {
return null;
}
}

$(function () {
$("head").first().append("<script nomodule src='/static/js/vendor/js.cookie.min.js'></script>");
let enabled = Cookies.get("disable-theme") == "1" ? false : true;

if (enabled) {
// Snow streak - How many days in a row the user has seen the theme. Controls size and speed of snow.
let dayStreak = Cookies.get("dayStreak");
let dayStreak = decodeBase64(Cookies.get("dayStreak"));
if (dayStreak == null) {
Cookies.set("dayStreak", 1, { expires: 5 * 7 });
dayStreak = 1;
Cookies.set("dayStreak", encodeBase64(String(dayStreak)), { expires: 5 * 7 });
}

let day = new Date().getDate();
Expand All @@ -21,8 +34,8 @@ $(function () {
prevDay = day;
}
if (prevDay < day) {
Cookies.set("dayStreak", parseInt(dayStreak) + 1, { expires: 5 * 7 });
dayStreak = parseInt(dayStreak) + 1;
Cookies.set("dayStreak", encodeBase64(String(dayStreak)), { expires: 5 * 7 });
Cookies.set("prevDay", day, { expires: 5 * 7 });
}

Expand Down Expand Up @@ -538,12 +551,12 @@ function toggleTheme() {

function handleDecreaseSnowStreak() {
if(confirm("Are you sure you want to decrease your snow streak by 1?")) {
let dayStreak = Cookies.get("dayStreak");
let dayStreak = decodeBase64(Cookies.get("dayStreak"));
if(dayStreak <= 1) {
alert("Your snow streak is already 1 and cannot be decreased further.");
return;
}
Cookies.set("dayStreak", parseInt(dayStreak) - 1, { expires: 5 * 7 });
Cookies.set("dayStreak", encodeBase64(String(parseInt(dayStreak) - 1)), { expires: 5 * 7 });
location.reload();
}
}
Loading