switch to local storage
All checks were successful
Jenkins
Ellpeck/Web/pipeline/head This commit looks good

This commit is contained in:
Ell 2023-06-13 17:29:52 +02:00
parent 2844b58dda
commit 11c00e85b3
7 changed files with 26 additions and 45 deletions

View file

@ -22,7 +22,10 @@ nav: nav/blognav.html
{% else %}
<script>
let style = dark ? "monokai" : "friendly";
addStyle(`https://cdn.jsdelivr.net/gh/richleland/pygments-css@master/${style}.css`);
$('head').append($("<link/>", {
rel: "stylesheet",
href: `https://cdn.jsdelivr.net/gh/richleland/pygments-css@master/${style}.css`
}));
</script>
{% endif %}

View file

@ -26,8 +26,6 @@
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="/scripts/util.js"></script>
<script src="/scripts/main.js"></script>
</head>

View file

@ -12,8 +12,6 @@
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="../scripts/util.js"></script>
<script data-ad-client="ca-pub-5754829579653773" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</head>

View file

@ -45,7 +45,16 @@ function populateManual(lang) {
entries.append(t);
}
forceToAnchor();
let anchor = undefined || location.hash;
// this is probably a terrible hack
if (anchor.startsWith("#")) {
let element = $(anchor);
if (element.length) {
$('html, body').animate({
scrollTop: element.offset().top
}, 0);
}
}
}
function replaceFormatting(text) {

View file

@ -1,7 +1,7 @@
if (getCookie("notification") !== "true") {
if (localStorage.getItem("notification") !== "true") {
$('#cookieinfo').html( /*html*/ `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
This site uses cookies to store information about your browsing activity.
This site uses your browser's local storage to store information about your browsing activity.
<br>For more information, check out the <a href="/privacy">privacy policy</a>.
<br>Have a nice day!
<button type="button" class="close" data-dismiss="alert" id="notif-close">
@ -10,6 +10,6 @@ if (getCookie("notification") !== "true") {
</div>
`);
$("#notif-close").on("click", function () {
setCookie("notification", "true", 365);
localStorage.setItem("notification", "true");
});
}
}

View file

@ -1,7 +1,10 @@
const darkCookie = getCookie("dark");
const dark = darkCookie == undefined ? window.matchMedia("(prefers-color-scheme: dark)").matches : darkCookie == "true";
const darkCookie = localStorage.getItem("dark");
const dark = darkCookie === null ? window.matchMedia("(prefers-color-scheme: dark)").matches : darkCookie === "true";
if (dark)
addStyle("/style/dark.css");
$('head').append($("<link/>", {
rel: "stylesheet",
href: "/style/dark.css"
}));
// redirect legacy anchors
if (/^#privacy\/?$/.test(location.hash))
@ -21,8 +24,8 @@ $(function () {
let mode = $('#dark-mode');
mode.prop('checked', dark);
mode.on('click', function () {
setCookie("dark", $(this).prop('checked'), 365);
localStorage.setItem("dark", $(this).prop('checked'));
location.hash = "";
location.reload();
});
});
});

View file

@ -1,30 +0,0 @@
function getCookie(key) {
let match = new RegExp(`${key}=([^;]+);?`, "g").exec(document.cookie);
return match ? match[1] : undefined;
}
function setCookie(key, value, days) {
let date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
document.cookie = `${key}=${value}; expires=${date.toUTCString()}; path=/`;
}
function forceToAnchor(customAnchor) {
let anchor = customAnchor || location.hash;
// this is probably a terrible hack
if (anchor.startsWith("#")) {
let element = $(anchor);
if (element.length) {
$('html, body').animate({
scrollTop: element.offset().top
}, 0);
}
}
}
function addStyle(path) {
$('head').append($("<link/>", {
rel: "stylesheet",
href: path
}));
}