Changed darkreader CDN to local copy.

This commit is contained in:
donfiguerres 2022-06-09 15:51:16 +08:00
parent 1c465dc73e
commit c6fe04df72
4 changed files with 43 additions and 15 deletions

View file

@ -1,7 +1,7 @@
<li class="nav-item dropdown"> <li class="nav-item dropdown">
<!-- This is a 'hack' for initializing the color scheme selection. See /js/darkmode.js --> <!-- This is a 'hack' for initializing the color scheme selection. See /js/darkmode.js -->
<div id="themeInitialization" style="display: none;" <div id="themeInitialization" style="display: none;"
default-theme=" {{ site.Params.darkTheme.default }} "></div> default-theme="{{ site.Params.darkTheme.default }}"></div>
<a class="nav-link dropdown-toggle" href="#" id="themeSelector" role="button" <a class="nav-link dropdown-toggle" href="#" id="themeSelector" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Theme</a> data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Theme</a>
<div class="dropdown-menu" aria-labelledby="themeSelector"> <div class="dropdown-menu" aria-labelledby="themeSelector">

View file

@ -8,7 +8,7 @@
{{ if site.Params.darkTheme.enable }} {{ if site.Params.darkTheme.enable }}
{{ if eq site.Params.darkTheme.provider "darkreader" }} {{ if eq site.Params.darkTheme.provider "darkreader" }}
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/darkreader@4.9.51/darkreader.js"></script> <script type="text/javascript" src="{{ "/js/darkreader.js" | relURL }}"></script>
<script type="text/javascript" src="{{ "/js/darkmode-darkreader.js" | relURL }}"></script> <script type="text/javascript" src="{{ "/js/darkmode-darkreader.js" | relURL }}"></script>
{{ end }} {{ end }}
{{ end }} {{ end }}

View file

@ -1,5 +1,9 @@
const DARK = "dark";
const LIGHT = "light";
const SYSTEM = "system";
function enableDarkTheme() { function enableDarkTheme() {
localStorage.setItem('color-scheme', "dark"); localStorage.setItem('color-theme', DARK);
DarkReader.enable({ DarkReader.enable({
brightness: 100, brightness: 100,
contrast: 100, contrast: 100,
@ -8,12 +12,12 @@ function enableDarkTheme() {
} }
function enableLightTheme() { function enableLightTheme() {
localStorage.setItem('color-scheme', "light"); localStorage.setItem('color-theme', LIGHT);
DarkReader.disable(); DarkReader.disable();
} }
function useSystemTheme() { function useSystemTheme() {
localStorage.setItem('color-scheme', "system"); localStorage.setItem('color-theme', SYSTEM);
DarkReader.auto({ DarkReader.auto({
brightness: 100, brightness: 100,
contrast: 100, contrast: 100,
@ -22,20 +26,22 @@ function useSystemTheme() {
} }
function initializeColorScheme() { function initializeColorScheme() {
// We're using the themeInitialization attributes as a 'hack' for setting up the // We're using the themeInitialization attributes as a 'hack' for setting up
// default color scheme becauase we don't want to complicate this further by // the default color scheme because we don't want to complicate this further
// creating custom javascript output in Hugo. // by creating custom javascript output in Hugo.
themeInitialization = document.getElementById("themeInitialization"); let themeInitialization = document.getElementById("themeInitialization");
defaultColorScheme = themeInitialization.getAttribute('default-theme'); let defaultColorScheme = themeInitialization.getAttribute('default-theme');
// If the user has already selected a preferred theme then use that instead // If the user has already selected a preferred theme then use that instead
// of the default theme. Also, the default theme gets loaded to localStorage // of the default theme. Also, the default theme gets loaded to localStorage
// on the first visit. // on the first visit.
if (!localStorage.getItem('color-scheme')) let colorTheme = localStorage.getItem('color-theme');
localStorage.setItem('color-scheme', defaultColorScheme); if (colorTheme == null || colorTheme.length == 0) {
storedColorScheme = localStorage.getItem('color-scheme'); colorTheme = defaultColorScheme;
if (storedColorScheme == "dark") { }
// Enable the color theme
if (colorTheme == DARK) {
enableDarkTheme(); enableDarkTheme();
} else if (storedColorScheme == "system") { } else if (colorTheme == SYSTEM) {
useSystemTheme(); useSystemTheme();
} else { } else {
// We use light theme for the two conditions below. // We use light theme for the two conditions below.

22
static/js/darkreader.js Normal file

File diff suppressed because one or more lines are too long