Skip to content

Commit 3ec3b58

Browse files
committed
feat(DarkMode): B站节日活动页面仅插件元素适配暗黑模式,其它界面元素保持不变 #192
1 parent fcef456 commit 3ec3b58

4 files changed

Lines changed: 97 additions & 22 deletions

File tree

src/composables/useDark.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { runWhenIdle } from '~/utils/lazyLoad'
66
import { setCookie } from '~/utils/main'
77
import { executeTimes } from '~/utils/timer'
88

9+
/**
10+
* Check if current page is festival page
11+
*/
12+
function isFestivalPage(): boolean {
13+
return /https?:\/\/(?:www\.)?bilibili\.com\/festival\/.*/.test(document.URL)
14+
}
15+
916
/**
1017
* 设置深色模式基准颜色
1118
*/
@@ -82,14 +89,22 @@ export function useDark() {
8289
if (themeChangeTimer)
8390
clearInterval(themeChangeTimer)
8491

92+
// Check if we should apply selective dark mode (plugin UI only) on festival pages
93+
const isSelectiveDark = isFestivalPage() && settings.value.adaptToOtherPageStyles
94+
8595
if (isDark.value) {
96+
// Always apply dark mode to plugin container
8697
document.querySelector('#bewly')?.classList.add('dark')
87-
document.documentElement.classList.add('dark')
88-
nextTick(() => {
89-
document.body?.classList.add('dark')
90-
})
91-
// bili_dark is bilibili's official dark mode class
92-
document.documentElement.classList.add('bili_dark')
98+
99+
// Only apply global dark mode if not on festival pages
100+
if (!isSelectiveDark) {
101+
document.documentElement.classList.add('dark')
102+
nextTick(() => {
103+
document.body?.classList.add('dark')
104+
})
105+
// bili_dark is bilibili's official dark mode class
106+
document.documentElement.classList.add('bili_dark')
107+
}
93108

94109
// 确保深色模式基准颜色被正确应用
95110
setDarkModeBaseColor(settings.value.darkModeBaseColor)
@@ -99,11 +114,15 @@ export function useDark() {
99114
}
100115
else {
101116
document.querySelector('#bewly')?.classList?.remove('dark')
102-
document.documentElement.classList.remove('dark')
103-
nextTick(() => {
104-
document.body?.classList.remove('dark')
105-
})
106-
document.documentElement.classList.remove('bili_dark')
117+
118+
// Only remove global classes if we're not in selective mode or if we applied them
119+
if (!isSelectiveDark) {
120+
document.documentElement.classList.remove('dark')
121+
nextTick(() => {
122+
document.body?.classList.remove('dark')
123+
})
124+
document.documentElement.classList.remove('bili_dark')
125+
}
107126

108127
setCookie('theme_style', 'light', 365 * 10)
109128
window.dispatchEvent(new CustomEvent('global.themeChange', { detail: 'light' }))

src/contentScripts/index.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ if (isFirefox) {
3636

3737
const currentUrl = document.URL
3838

39+
function isFestivalPage(): boolean {
40+
return /https?:\/\/(?:www\.)?bilibili\.com\/festival\/.*/.test(document.URL)
41+
}
42+
3943
function isSupportedPages(): boolean {
4044
if (isInIframe())
4145
return false
@@ -136,10 +140,12 @@ let lastUrl = location.href
136140
let hasAppliedPlayerMode = false // 添加标志变量
137141

138142
if (isSupportedPages() || isSupportedIframePages()) {
143+
// Always use dark mode if enabled, but let useDark() handle selective application
139144
if (settings.value.adaptToOtherPageStyles)
140145
useDark()
141146

142-
if (settings.value.adaptToOtherPageStyles) {
147+
const shouldApplyFullStyles = settings.value.adaptToOtherPageStyles && !isFestivalPage()
148+
if (shouldApplyFullStyles) {
143149
document.documentElement.classList.add('bewly-design')
144150

145151
// Remove the Bilibili Evolved's dark mode style
@@ -533,18 +539,38 @@ window.addEventListener('message', (event) => {
533539
const { type, isDark, darkModeBaseColor } = event.data
534540

535541
if (type === 'iframeDarkModeChange') {
542+
// Check if we should apply selective dark mode (plugin UI only) on festival pages
543+
const isSelectiveDark = isFestivalPage()
544+
536545
if (isDark) {
537-
document.documentElement.classList.add('dark')
538-
document.body?.classList.add('dark')
546+
// Always apply to plugin container if it exists
547+
const bewlyElement = document.querySelector('#bewly')
548+
if (bewlyElement) {
549+
bewlyElement.classList.add('dark')
550+
}
551+
552+
// Only apply global styles if not on festival pages
553+
if (!isSelectiveDark) {
554+
document.documentElement.classList.add('dark')
555+
document.body?.classList.add('dark')
556+
}
539557

540558
// 如果提供了深色模式基准颜色,则应用它
541559
if (darkModeBaseColor) {
542560
document.documentElement.style.setProperty('--bew-dark-base-color', darkModeBaseColor)
543561
}
544562
}
545563
else {
546-
document.documentElement.classList.remove('dark')
547-
document.body?.classList.remove('dark')
564+
const bewlyElement = document.querySelector('#bewly')
565+
if (bewlyElement) {
566+
bewlyElement.classList.remove('dark')
567+
}
568+
569+
// Only remove global classes if not in selective mode
570+
if (!isSelectiveDark) {
571+
document.documentElement.classList.remove('dark')
572+
document.body?.classList.remove('dark')
573+
}
548574
}
549575
}
550576
}, { passive: true })

src/contentScripts/views/App.vue

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import { onKeyStroke, useEventListener, useThrottleFn, useToggle } from '@vueuse/core'
33
import type { Ref } from 'vue'
4+
import { ref } from 'vue'
45
56
import type { BewlyAppProvider } from '~/composables/useAppProvider'
67
import { useDark } from '~/composables/useDark'
@@ -16,9 +17,26 @@ import emitter from '~/utils/mitt'
1617
1718
import { setupNecessarySettingsWatchers } from './necessarySettingsWatchers'
1819
20+
// Check if current page is festival page
21+
function isFestivalPage(): boolean {
22+
return /https?:\/\/(?:www\.)?bilibili\.com\/festival\/.*/.test(document.URL)
23+
}
24+
1925
const mainStore = useMainStore()
2026
const settingsStore = useSettingsStore()
21-
const { isDark } = useDark()
27+
28+
// Conditionally use dark mode (skip on festival pages)
29+
let isDark: Ref<boolean>
30+
// Always use dark mode if enabled, but let useDark() handle selective application
31+
const shouldUseDark = settings.value.adaptToOtherPageStyles
32+
33+
if (shouldUseDark) {
34+
const darkResult = useDark()
35+
isDark = darkResult.isDark
36+
}
37+
else {
38+
isDark = ref(false)
39+
}
2240
const [showSettings, toggleSettings] = useToggle(false)
2341
2442
// Get the 'page' query parameter from the URL
@@ -114,12 +132,20 @@ useEventListener(window, 'message', ({ data, source }) => {
114132
// 在iframe环境中,只更新DOM样式,不修改用户的主题设置
115133
// 避免覆盖用户设置的"auto"模式
116134
if (isInIframe()) {
135+
// Check if we should apply selective dark mode (plugin UI only) on festival pages
136+
const isSelectiveDark = isFestivalPage()
137+
117138
// 立即更新DOM样式,不修改settings.value.theme
118139
if (isDark) {
119-
document.documentElement.classList.add('dark')
120-
document.body?.classList.add('dark')
140+
// Always apply to plugin container
121141
document.querySelector('#bewly')?.classList.add('dark')
122142
143+
// Only apply global styles if not on festival pages
144+
if (!isSelectiveDark) {
145+
document.documentElement.classList.add('dark')
146+
document.body?.classList.add('dark')
147+
}
148+
123149
// 如果提供了深色模式基准颜色,则应用它(仅应用到DOM,不修改设置)
124150
if (darkModeBaseColor) {
125151
document.documentElement.style.setProperty('--bew-dark-base-color', darkModeBaseColor)
@@ -132,9 +158,13 @@ useEventListener(window, 'message', ({ data, source }) => {
132158
}
133159
}
134160
else {
135-
document.documentElement.classList.remove('dark')
136-
document.body?.classList.remove('dark')
137161
document.querySelector('#bewly')?.classList?.remove('dark')
162+
163+
// Only remove global classes if not in selective mode
164+
if (!isSelectiveDark) {
165+
document.documentElement.classList.remove('dark')
166+
document.body?.classList.remove('dark')
167+
}
138168
}
139169
140170
// 强制重新计算样式

src/utils/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export function isVideoOrBangumiPage(url: string = location.href): boolean {
176176
|| /https?:\/\/(?:www\.)?bilibili\.com\/list\/watchlater\?(?:bvid|avid).*/.test(url)
177177
// favorite playlist
178178
|| /https?:\/\/(?:www\.)?bilibili\.com\/list\/ml.*/.test(url)
179-
|| /https?:\/\/(?:www\.)?bilibili\.com\/festival\/.*\?(?:bvid|avid).*/.test(url)
179+
|| /https?:\/\/(?:www\.)?bilibili\.com\/festival\/.*/.test(url)
180180
) {
181181
return true
182182
}

0 commit comments

Comments
 (0)