mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2025-09-15 12:58:48 +08:00
feat: 增加 waline avatarCDN 配置(最新評論也會更新) feat: archives 的 標題 在 menu 配置中獲取 fix: 修復文章頁 mathjax 因字體小導致顯示重疊的 bug close #587 fix: 修復 mathjax 在列表下,導致 列表沒有樣式的 bug close #586 improvement: 減少卡頓,apple 裝置首頁背景 background-attachment 設為 scroll
162 lines
5.2 KiB
JavaScript
162 lines
5.2 KiB
JavaScript
/**
|
|
* Butterfly
|
|
* inject js to head
|
|
*/
|
|
|
|
'use strict'
|
|
|
|
hexo.extend.helper.register('inject_head_js', function () {
|
|
const { darkmode, aside, change_font_size, index_img, disable_top_img, pjax} = this.theme
|
|
|
|
const localStore = `
|
|
win.saveToLocal = {
|
|
set: function setWithExpiry(key, value, ttl) {
|
|
if (ttl === 0) return
|
|
const now = new Date()
|
|
const expiryDay = ttl * 86400000
|
|
const item = {
|
|
value: value,
|
|
expiry: now.getTime() + expiryDay,
|
|
}
|
|
localStorage.setItem(key, JSON.stringify(item))
|
|
},
|
|
|
|
get: function getWithExpiry(key) {
|
|
const itemStr = localStorage.getItem(key)
|
|
|
|
if (!itemStr) {
|
|
return undefined
|
|
}
|
|
const item = JSON.parse(itemStr)
|
|
const now = new Date()
|
|
|
|
if (now.getTime() > item.expiry) {
|
|
localStorage.removeItem(key)
|
|
return undefined
|
|
}
|
|
return item.value
|
|
}
|
|
}
|
|
`
|
|
|
|
// https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript
|
|
const getScript = `
|
|
win.getScript = url => new Promise((resolve, reject) => {
|
|
const script = document.createElement('script')
|
|
script.src = url
|
|
script.async = true
|
|
script.onerror = reject
|
|
script.onload = script.onreadystatechange = function() {
|
|
const loadState = this.readyState
|
|
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
|
script.onload = script.onreadystatechange = null
|
|
resolve()
|
|
}
|
|
document.head.appendChild(script)
|
|
})
|
|
`
|
|
|
|
let darkmodeJs = ''
|
|
if (darkmode.enable) {
|
|
darkmodeJs = `
|
|
win.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')
|
|
}
|
|
}
|
|
win.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 t = saveToLocal.get('theme')
|
|
`
|
|
|
|
const autoChangeMode = darkmode.autoChangeMode
|
|
|
|
if (autoChangeMode === 1) {
|
|
darkmodeJs += `
|
|
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) {
|
|
darkmodeJs += `
|
|
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 {
|
|
darkmodeJs += `
|
|
if (t === 'dark') activateDarkMode()
|
|
else if (t === 'light') activateLightMode()
|
|
`
|
|
}
|
|
}
|
|
|
|
let asideStatus = ''
|
|
if (aside.enable && aside.button) {
|
|
asideStatus = `
|
|
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')
|
|
}
|
|
}
|
|
`
|
|
}
|
|
|
|
let changFontAside = ''
|
|
if (change_font_size) {
|
|
changFontAside = `
|
|
const fontSizeVal = saveToLocal.get('global-font-size')
|
|
if (fontSizeVal !== undefined) {
|
|
document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
|
|
}
|
|
`
|
|
}
|
|
|
|
let detectApple = ''
|
|
if (!disable_top_img || index_img !== false) {
|
|
detectApple = `
|
|
const detectApple = () => {
|
|
if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
|
|
document.documentElement.classList.add('apple')
|
|
}
|
|
}
|
|
detectApple()
|
|
`
|
|
|
|
if (pjax.enable) {
|
|
detectApple += 'document.addEventListener(\'pjax:complete\', detectApple)'
|
|
}
|
|
}
|
|
|
|
return `<script>(win=>{${localStore + getScript + darkmodeJs + asideStatus + changFontAside + detectApple}})(window)</script>`
|
|
})
|