From e5c8bf7b18515770eadf5e686f15ee72c3a746b8 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 26 Dec 2023 02:36:51 +0000 Subject: [PATCH] one cookie set function, for consistent samesite/path/expires --- ext/blotter/script.js | 6 +++--- ext/handle_pixel/script.js | 8 ++++---- ext/index/script.js | 6 +++--- ext/pools/script.js | 2 +- ext/rule34/script.js | 4 ++-- ext/static_files/script.js | 11 +++++++++-- themes/lite/setup.theme.php | 6 +++--- themes/lite/user_config.theme.php | 6 +++--- themes/rule34v2/script.js | 12 ++++++------ 9 files changed, 34 insertions(+), 27 deletions(-) diff --git a/ext/blotter/script.js b/ext/blotter/script.js index 2623f3e45..664d8588e 100644 --- a/ext/blotter/script.js +++ b/ext/blotter/script.js @@ -4,14 +4,14 @@ document.addEventListener('DOMContentLoaded', () => { $(".shm-blotter2-toggle").click(function() { $(".shm-blotter2").slideToggle("slow", function() { if($(".shm-blotter2").is(":hidden")) { - Cookies.set("ui-blotter2-hidden", 'true'); + shm_cookie_set("ui-blotter2-hidden", 'true'); } else { - Cookies.set("ui-blotter2-hidden", 'false'); + shm_cookie_set("ui-blotter2-hidden", 'false'); } }); }); - if(Cookies.get("ui-blotter2-hidden") === 'true') { + if(shm_cookie_get("ui-blotter2-hidden") === 'true') { $(".shm-blotter2").hide(); } }); diff --git a/ext/handle_pixel/script.js b/ext/handle_pixel/script.js index 6cd83fbf3..d2d6284f4 100644 --- a/ext/handle_pixel/script.js +++ b/ext/handle_pixel/script.js @@ -24,7 +24,7 @@ document.addEventListener('DOMContentLoaded', () => { $(".shm-zoomer").val(zoom_type); if (save_cookie) { - Cookies.set("ui-image-zoom", zoom_type, {expires: 365}); + shm_cookie_set("ui-image-zoom", zoom_type, {expires: 365, samesite: "lax", path: "/"}); } } @@ -38,13 +38,13 @@ document.addEventListener('DOMContentLoaded', () => { }); $("img.shm-main-image").click(function(e) { - switch(Cookies.get("ui-image-zoom")) { + switch(shm_cookie_get("ui-image-zoom")) { case "full": zoom("width"); break; default: zoom("full"); break; } }); - if(Cookies.get("ui-image-zoom")) { - zoom(Cookies.get("ui-image-zoom")); + if(shm_cookie_get("ui-image-zoom")) { + zoom(shm_cookie_get("ui-image-zoom")); } }); diff --git a/ext/index/script.js b/ext/index/script.js index 0f17ce939..f2b4b02d8 100644 --- a/ext/index/script.js +++ b/ext/index/script.js @@ -1,7 +1,7 @@ /*jshint bitwise:false, curly:true, eqeqeq:true, evil:true, forin:false, noarg:true, noempty:true, nonew:true, undef:false, strict:false, browser:true, jquery:true */ document.addEventListener('DOMContentLoaded', () => { - var blocked_tags = (Cookies.get("ui-blocked-tags") || "").split(" "); + var blocked_tags = (shm_cookie_get("ui-blocked-tags") || "").split(" "); var needs_refresh = false; for(var i=0; i { }); function select_blocked_tags() { - var blocked_tags = prompt("Enter tags to ignore", Cookies.get("ui-blocked-tags") || "AI-generated"); + var blocked_tags = prompt("Enter tags to ignore", shm_cookie_get("ui-blocked-tags") || "AI-generated"); if(blocked_tags !== null) { - Cookies.set("ui-blocked-tags", blocked_tags.toLowerCase(), {expires: 365}); + shm_cookie_set("ui-blocked-tags", blocked_tags.toLowerCase()); location.reload(true); } } diff --git a/ext/pools/script.js b/ext/pools/script.js index 16119d328..19b8ea9ad 100644 --- a/ext/pools/script.js +++ b/ext/pools/script.js @@ -3,7 +3,7 @@ document.addEventListener('DOMContentLoaded', () => { $('#order_pool').change(function(){ var val = $("#order_pool option:selected").val(); - Cookies.set("shm_ui-order-pool", val, {path: '/', expires: 365}); //FIXME: This won't play nice if COOKIE_PREFIX is not "shm_". + shm_cookie_set("shm_ui-order-pool", val); //FIXME: This won't play nice if COOKIE_PREFIX is not "shm_". window.location.href = ''; }); }); diff --git a/ext/rule34/script.js b/ext/rule34/script.js index 9d22a5e69..c4afbe0dd 100644 --- a/ext/rule34/script.js +++ b/ext/rule34/script.js @@ -1,5 +1,5 @@ document.addEventListener('DOMContentLoaded', () => { - if(Cookies.get("ui-tnc-agreed") !== "true" && window.location.href.indexOf("/wiki/") == -1) { + if(shm_cookie_get("ui-tnc-agreed") !== "true" && window.location.href.indexOf("/wiki/") == -1) { $("BODY").addClass("censored"); $("BODY").append("
"); $("BODY").append(""+ @@ -13,7 +13,7 @@ document.addEventListener('DOMContentLoaded', () => { }); function tnc_agree() { - Cookies.set("ui-tnc-agreed", "true", {path: '/', expires: 365}); + shm_cookie_set("ui-tnc-agreed", "true"); $("BODY").removeClass("censored"); $(".tnc_bg").hide(); $(".tnc").hide(); diff --git a/ext/static_files/script.js b/ext/static_files/script.js index a2a8097ef..61eb16b33 100644 --- a/ext/static_files/script.js +++ b/ext/static_files/script.js @@ -1,5 +1,12 @@ /*jshint bitwise:false, curly:true, eqeqeq:true, evil:true, forin:false, noarg:true, noempty:true, nonew:true, undef:false, strict:false, browser:true */ +function shm_cookie_set(name, value) { + Cookies.set(name, value, {expires: 365, samesite: "lax", path: "/"}); +} +function shm_cookie_get(name) { + return Cookies.get(name); +} + document.addEventListener('DOMContentLoaded', () => { /** Load jQuery extensions **/ //Code via: https://stackoverflow.com/a/13106698 @@ -32,7 +39,7 @@ document.addEventListener('DOMContentLoaded', () => { /** Setup sidebar toggle **/ let sidebar_hidden = []; try { - sidebar_hidden = (Cookies.get("ui-sidebar-hidden") || "").split("|"); + sidebar_hidden = (shm_cookie_get("ui-sidebar-hidden") || "").split("|"); for (let i=0; i 0) { $(sidebar_hidden[i]+" .blockbody").hide(); @@ -55,7 +62,7 @@ document.addEventListener('DOMContentLoaded', () => { } } } - Cookies.set("ui-sidebar-hidden", sidebar_hidden.join("|"), {expires: 365}); + shm_cookie_set("ui-sidebar-hidden", sidebar_hidden.join("|")); }); }); diff --git a/themes/lite/setup.theme.php b/themes/lite/setup.theme.php index 2629eacd4..5aa3824e8 100644 --- a/themes/lite/setup.theme.php +++ b/themes/lite/setup.theme.php @@ -23,14 +23,14 @@ protected function sb_to_html(SetupBlock $block): string $(\"#$i-toggle\").click(function() { $(\"#$i\").slideToggle(\"slow\", function() { if($(\"#$i\").is(\":hidden\")) { - Cookies.set(\"$i-hidden\", 'true', {path: '/'}); + shm_cookie_set(\"$i-hidden\", 'true'); } else { - Cookies.set(\"$i-hidden\", 'false', {path: '/'}); + shm_cookie_set(\"$i-hidden\", 'false'); } }); }); - if(Cookies.get(\"$i-hidden\") == 'true') { + if(shm_cookie_get(\"$i-hidden\") == 'true') { $(\"#$i\").hide(); } }); diff --git a/themes/lite/user_config.theme.php b/themes/lite/user_config.theme.php index 2bd8c01d0..2ad74f786 100644 --- a/themes/lite/user_config.theme.php +++ b/themes/lite/user_config.theme.php @@ -23,14 +23,14 @@ protected function sb_to_html(SetupBlock $block): string $(\"#$i-toggle\").click(function() { $(\"#$i\").slideToggle(\"slow\", function() { if($(\"#$i\").is(\":hidden\")) { - Cookies.set(\"$i-hidden\", 'true', {path: '/'}); + shm_cookie_set(\"$i-hidden\", 'true'); } else { - Cookies.set(\"$i-hidden\", 'false', {path: '/'}); + shm_cookie_set(\"$i-hidden\", 'false'); } }); }); - if(Cookies.get(\"$i-hidden\") == 'true') { + if(shm_cookie_get(\"$i-hidden\") == 'true') { $(\"#$i\").hide(); } }); diff --git a/themes/rule34v2/script.js b/themes/rule34v2/script.js index 8c559ad7c..c58c4fab0 100644 --- a/themes/rule34v2/script.js +++ b/themes/rule34v2/script.js @@ -8,17 +8,17 @@ var navHidden = false; function toggleNav() { if(navHidden) { $('BODY').removeClass('navHidden'); - Cookies.set("ui-shownav", "true"); + shm_cookie_set("ui-shownav", "true"); } else { $('BODY').addClass('navHidden'); - Cookies.set("ui-shownav", "false"); + shm_cookie_set("ui-shownav", "false"); } navHidden = !navHidden; } document.addEventListener('DOMContentLoaded', () => { - if(Cookies.get("ui-shownav") === "false") { + if(shm_cookie_get("ui-shownav") === "false") { toggleNav(); } }); @@ -29,12 +29,12 @@ function toggleDesktop() { if(forceDesktop) { let viewport = document.querySelector("meta[name=viewport]"); viewport.setAttribute('content', 'width=512'); - Cookies.set("ui-desktop", "false"); + shm_cookie_set("ui-desktop", "false"); } else { let viewport = document.querySelector("meta[name=viewport]"); viewport.setAttribute('content', 'width=1024, initial-scale=0.4'); - Cookies.set("ui-desktop", "true"); + shm_cookie_set("ui-desktop", "true"); navHidden = true; toggleNav(); } @@ -42,7 +42,7 @@ function toggleDesktop() { } document.addEventListener('DOMContentLoaded', () => { - if(Cookies.get("ui-desktop") === "true") { + if(shm_cookie_get("ui-desktop") === "true") { toggleDesktop(); } });