mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-12 22:17:06 +08:00
72 lines
2.9 KiB
Plaintext
72 lines
2.9 KiB
Plaintext
script
|
|
| (function () {
|
|
|
|
if theme.darkmode.enable
|
|
| window.activateDarkMode = function () {
|
|
| document.documentElement.setAttribute('data-theme', 'dark')
|
|
| if (document.querySelector('meta[name="theme-color"]') !== null) {
|
|
| document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
|
|
| }
|
|
| }
|
|
| window.activateLightMode = function () {
|
|
| document.documentElement.setAttribute('data-theme', 'light')
|
|
| if (document.querySelector('meta[name="theme-color"]') !== null) {
|
|
| document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
|
|
| }
|
|
| }
|
|
|
|
| const autoChangeMode = '#{theme.darkmode.autoChangeMode}'
|
|
| const t = saveToLocal.get('theme')
|
|
| if (autoChangeMode === '1') {
|
|
| const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
| const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
|
|
| const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
|
|
| const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
|
|
|
|
| if (t === undefined) {
|
|
| if (isLightMode) activateLightMode()
|
|
| else if (isDarkMode) activateDarkMode()
|
|
| else if (isNotSpecified || hasNoSupport) {
|
|
| const now = new Date()
|
|
| const hour = now.getHours()
|
|
| const isNight = hour <= 6 || hour >= 18
|
|
| isNight ? activateDarkMode() : activateLightMode()
|
|
| }
|
|
| window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
|
|
| if (saveToLocal.get('theme') === undefined) {
|
|
| e.matches ? activateDarkMode() : activateLightMode()
|
|
| }
|
|
| })
|
|
| } else if (t === 'light') activateLightMode()
|
|
| else activateDarkMode()
|
|
| } else if (autoChangeMode === '2') {
|
|
| const now = new Date()
|
|
| const hour = now.getHours()
|
|
| const isNight = hour <= 6 || hour >= 18
|
|
| if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
|
|
| else if (t === 'light') activateLightMode()
|
|
| else activateDarkMode()
|
|
| } else {
|
|
| if (t === 'dark') activateDarkMode()
|
|
| else if (t === 'light') activateLightMode()
|
|
| }
|
|
|
|
if theme.aside.enable && theme.aside.button
|
|
| const asideStatus = saveToLocal.get('aside-status')
|
|
| if (asideStatus !== undefined) {
|
|
| if (asideStatus === 'hide') {
|
|
| document.documentElement.classList.add('hide-aside')
|
|
| } else {
|
|
| document.documentElement.classList.remove('hide-aside')
|
|
| }
|
|
| }
|
|
|
|
if theme.change_font_size
|
|
| const fontSizeVal = saveToLocal.get('global-font-size')
|
|
| if (fontSizeVal !== undefined) {
|
|
| document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
|
|
| }
|
|
|
|
| })()
|
|
|