From 6587d92e5f067be98354dcbfde60e34c70ba2a62 Mon Sep 17 00:00:00 2001 From: Piyush Mishra Date: Thu, 13 May 2021 13:29:26 +0530 Subject: [PATCH] cookies --- static/js/app.js | 2 ++ static/js/onload.js | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/static/js/app.js b/static/js/app.js index dedebef..865602d 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -273,6 +273,8 @@ function vayaktiList() { function changeColor() { $('body').toggleClass('dark') + if($('body').hasClass('dark')) setCookie('theme', 'dark', 30); + else setCookie('theme', 'light', 30); $('#action_clip').addClass('is-hidden'); } diff --git a/static/js/onload.js b/static/js/onload.js index e586ceb..30fe433 100644 --- a/static/js/onload.js +++ b/static/js/onload.js @@ -1,4 +1,4 @@ -if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { +if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches || getCookie('theme') == 'dark') { $('body').toggleClass('dark'); } @@ -46,4 +46,26 @@ $(document).ready(function() { }); Images.setupImages(); -}); \ No newline at end of file +}); + +function setCookie(cname, cvalue, exdays) { + var d = new Date(); + d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); + var expires = "expires="+d.toUTCString(); + document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; +} + +function getCookie(cname) { + var name = cname + "="; + var ca = document.cookie.split(';'); + for(var i = 0; i < ca.length; i++) { + var c = ca[i]; + while (c.charAt(0) == ' ') { + c = c.substring(1); + } + if (c.indexOf(name) == 0) { + return c.substring(name.length, c.length); + } + } + return ""; +} \ No newline at end of file