Skip to content

Commit

Permalink
Merge branch 'BewlyBewly:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
starknt authored Jan 13, 2025
2 parents 4c3b618 + 57a38d1 commit 32b9484
Showing 22 changed files with 250 additions and 121 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"name": "bewly-bewly",
"displayName": "BewlyBewly",
"type": "module",
"version": "0.39.7",
"version": "0.40.0",
"private": true,
"packageManager": "pnpm@9.5.0",
"description": "Just make a few small changes to your Bilibili homepage.",
4 changes: 4 additions & 0 deletions src/_locales/cmn-CN.yml
Original file line number Diff line number Diff line change
@@ -149,6 +149,7 @@ settings:
# Desktop & Dock
use_old_top_bar: 使用旧版顶栏
auto_hide_top_bar: 自动隐藏顶栏
show_bewly_or_bili_top_bar_switcher: 显示 BewlyBewly/Bilibili 顶栏切换器
show_bewly_or_bili_page_switcher: 显示 BewlyBewly/Bilibili 页面切换器
top_bar_icon_badges: 图标角标
top_bar_icon_badges_opt:
@@ -287,6 +288,9 @@ auth:
auth_access_key_first: 在这之前请先授权 BewlyBewly 使用 access key

topbar:
switch_to_bewly_top_bar: 切换到 BewlyBewly 顶栏
switch_to_bili_top_bar: 切换到 Bilibili/Bilibili-Evolved 顶栏

sign_in: 登录
notifications: 通知
moments: 动态
4 changes: 4 additions & 0 deletions src/_locales/cmn-TW.yml
Original file line number Diff line number Diff line change
@@ -149,6 +149,7 @@ settings:
# Desktop and Dock
use_old_top_bar: 使用舊版頂欄
auto_hide_top_bar: 自動隱藏頂欄
show_bewly_or_bili_top_bar_switcher: 顯示 BewlyBewly/Bilibili 頂欄切換器
show_bewly_or_bili_page_switcher: 顯示 BewlyBewly/Bilibili 頁面切換器
top_bar_icon_badges: 圖示標記樣式
top_bar_icon_badges_opt:
@@ -288,6 +289,9 @@ auth:
auth_access_key_first: 在這之前請先授權 BewlyBewly 使用 access key

topbar:
switch_to_bewly_top_bar: 切換到 BewlyBewly 頂部欄
switch_to_bili_top_bar: 切換到 Bilibili/Bilibili-Evolved 頂部欄

sign_in: 登入
notifications: 通知
moments: 動態
4 changes: 4 additions & 0 deletions src/_locales/en.yml
Original file line number Diff line number Diff line change
@@ -149,6 +149,7 @@ settings:
# Desktop & Dock
use_old_top_bar: Use the old top bar
auto_hide_top_bar: Automatically hide the top bar
show_bewly_or_bili_top_bar_switcher: Show BewlyBewly/Bilibili top bar switcher
show_bewly_or_bili_page_switcher: Show BewlyBewly/Bilibili page switcher
top_bar_icon_badges: Icon badges
top_bar_icon_badges_opt:
@@ -290,6 +291,9 @@ auth:
auth_access_key_first: Before doing this, authorize BewlyBewly to use the access key.

topbar:
switch_to_bewly_top_bar: Switch to BewlyBewly top bar
switch_to_bili_top_bar: Switch to BiliBili/Bilibili-Evolved top bar

sign_in: Sign in
notifications: Notifications
moments: Moments
6 changes: 5 additions & 1 deletion src/_locales/jyut.yml
Original file line number Diff line number Diff line change
@@ -147,8 +147,9 @@ settings:
# Desktop and Dock
use_old_top_bar: 用返舊版頂部欄
auto_hide_top_bar: 自動收埋頂部欄
show_bewly_or_bili_top_bar_switcher: 顯示 BewlyBewly/Bilibili 頂部欄切換掣
show_bewly_or_bili_page_switcher: 顯示 BewlyBewly/Bilibili 頁面切換掣
top_bar_icon_badges: 圖示邊位標記
top_bar_icon_badges: 圖標邊位標記
top_bar_icon_badges_opt:
number: 數字
dot: 圓點
@@ -286,6 +287,9 @@ auth:
auth_access_key_first: 整之前記得先授權 BewlyBewly 去用 access key

topbar:
switch_to_bewly_top_bar: 切換到 BewlyBewly 頂部欄
switch_to_bili_top_bar: 切換到 Bilibili/Bilibili-Evolved 頂部欄

sign_in: 登入
notifications: 通知
moments: 動向
8 changes: 6 additions & 2 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import browser from 'webextension-polyfill'
import { setupApiMsgLstnrs } from './messageListeners/api'
import { setupTabMsgLstnrs } from './messageListeners/tabs'

browser.runtime.onInstalled.addListener((): void => {
browser.runtime.onInstalled.addListener(async () => {
// eslint-disable-next-line no-console
console.log('Extension installed')
})
@@ -15,7 +15,7 @@ function isExtensionUri(url: string) {
// eslint-disable-next-line node/prefer-global/process
if (process.env.FIREFOX) {
browser.webRequest.onBeforeSendHeaders.addListener(
(details: any) => {
async (details: any) => {
const requestHeaders: browser.WebRequest.HttpHeaders = []
if (details.documentUrl) {
const url = new URL(details.documentUrl)
@@ -26,6 +26,10 @@ if (process.env.FIREFOX) {
requestHeaders.push({ name: details.requestHeaders[i].name, value: extensionUri ? 'https://www.bilibili.com' : url.origin })
else
requestHeaders.push(details.requestHeaders[i])

if (details.requestHeaders[i].name === 'firefox-multi-account-cookie') {
requestHeaders.push({ name: 'cookie', value: details.requestHeaders[i].value })
}
}

return { ...details, requestHeaders }
112 changes: 66 additions & 46 deletions src/background/utils.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
// 1. 直接返回data
// 2. json化后返回data

import type Browser from 'webextension-polyfill'

type FetchAfterHandler = ((data: Response) => Promise<any>) | ((data: any) => any)

function toJsonHandler(data: Response): Promise<any> {
@@ -57,64 +59,82 @@ interface APIMAP {
}
// 工厂函数API_LISTENER_FACTORY
function apiListenerFactory(API_MAP: APIMAP) {
return (message: Message, sender?: any, sendResponse?: Function) => {
return async (message: Message, sender?: Browser.Runtime.MessageSender, sendResponse?: Function) => {
const contentScriptQuery = message.contentScriptQuery
// 检测是否有contentScriptQuery
if (!contentScriptQuery || !API_MAP[contentScriptQuery])
return console.error(`Cannot find this contentScriptQuery: ${contentScriptQuery}`)
if (API_MAP[contentScriptQuery] instanceof Function)
return (API_MAP[contentScriptQuery] as APIFunction)(message, sender, sendResponse)

try {
let { contentScriptQuery, ...rest } = message
// rest above two part body or params
rest = rest || {}
const api = API_MAP[contentScriptQuery] as API

// eslint-disable-next-line node/prefer-global/process
if (process.env.FIREFOX && sender && sender.tab && sender.tab.cookieStoreId) {
const cookies = await browser.cookies.getAll({ storeId: sender.tab.cookieStoreId })
return doRequest(message, api, sendResponse, cookies)
}

return doRequest(message, api, sendResponse)
}
}

let { _fetch, url, params = {}, afterHandle } = API_MAP[contentScriptQuery] as API
const { method, headers, body } = _fetch as _FETCH
const isGET = method.toLocaleLowerCase() === 'get'
// merge params and body
const targetParams = Object.assign({}, params)
let targetBody = Object.assign({}, body)
Object.keys(rest).forEach((key) => {
if (body && body[key] !== undefined)
targetBody[key] = rest[key]
else
targetParams[key] = rest[key]
})
function doRequest(message: Message, api: API, sendResponse?: Function, cookies?: Browser.Cookies.Cookie[]) {
try {
let { contentScriptQuery, ...rest } = message
// rest above two part body or params
rest = rest || {}

// generate params
if (Object.keys(targetParams).length) {
const urlParams = new URLSearchParams()
for (const key in targetParams)
targetParams[key] && urlParams.append(key, targetParams[key])
url += `?${urlParams.toString()}`
}
// generate body
if (!isGET) {
targetBody = (headers && headers['Content-Type'] && headers['Content-Type'].includes('application/x-www-form-urlencoded'))
? new URLSearchParams(targetBody)
: JSON.stringify(targetBody)
}
// get cant take body
const fetchOpt = { method, headers }
!isGET && Object.assign(fetchOpt, { body: targetBody })
let { _fetch, url, params = {}, afterHandle } = api
const { method, headers = {}, body } = _fetch as _FETCH
const isGET = method.toLocaleLowerCase() === 'get'
// merge params and body
const targetParams = Object.assign({}, params)
let targetBody = Object.assign({}, body)
Object.keys(rest).forEach((key) => {
if (body && body[key] !== undefined)
targetBody[key] = rest[key]
else
targetParams[key] = rest[key]
})

// fetch and after handle
let baseFunc = fetch(url, fetchOpt)
afterHandle.forEach((func) => {
if (func.name === sendResponseHandler.name && sendResponse)
// sendResponseHandler 是一个特殊的后处理函数,需要传入sendResponse
baseFunc = baseFunc.then(sendResponseHandler(sendResponse))
else
baseFunc = baseFunc.then(func)
})
baseFunc.catch(console.error)
return baseFunc
// generate params
if (Object.keys(targetParams).length) {
const urlParams = new URLSearchParams()
for (const key in targetParams)
targetParams[key] && urlParams.append(key, targetParams[key])
url += `?${urlParams.toString()}`
}
catch (e) {
console.error(e)
// generate body
if (!isGET) {
targetBody = (headers && headers['Content-Type'] && headers['Content-Type'].includes('application/x-www-form-urlencoded'))
? new URLSearchParams(targetBody)
: JSON.stringify(targetBody)
}
// generate cookies
if (cookies) {
const cookieStr = cookies.map(cookie => `${cookie.name}=${cookie.value}`).join('; ')
headers['firefox-multi-account-cookie'] = cookieStr
}
// get cant take body
const fetchOpt = { method, headers }
!isGET && Object.assign(fetchOpt, { body: targetBody })
// fetch and after handle
let baseFunc = fetch(url, {
...fetchOpt,
})
afterHandle.forEach((func) => {
if (func.name === sendResponseHandler.name && sendResponse)
// sendResponseHandler 是一个特殊的后处理函数,需要传入sendResponse
baseFunc = baseFunc.then(sendResponseHandler(sendResponse))
else
baseFunc = baseFunc.then(func)
})
baseFunc.catch(console.error)
return baseFunc
}
catch (e) {
console.error(e)
}
}

1 change: 1 addition & 0 deletions src/components/IframePage.vue
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ const currentUrl = ref<string>(props.url)
watch(() => isDark.value, (newValue) => {
iframeRef.value?.contentDocument?.documentElement.classList.toggle('dark', newValue)
iframeRef.value?.contentDocument?.body?.classList.toggle('dark', newValue)
})
onMounted(() => {
3 changes: 3 additions & 0 deletions src/components/Settings/DesktopAndDock/DesktopAndDock.vue
Original file line number Diff line number Diff line change
@@ -98,6 +98,9 @@ function handleToggleDockItem(dockItem: any) {
<SettingsItem :title="$t('settings.auto_hide_top_bar')">
<Radio v-model="settings.autoHideTopBar" />
</SettingsItem>
<SettingsItem :title="$t('settings.show_bewly_or_bili_top_bar_switcher')">
<Radio v-model="settings.showBewlyOrBiliTopBarSwitcher" />
</SettingsItem>
<SettingsItem :title="$t('settings.show_bewly_or_bili_page_switcher')">
<Radio v-model="settings.showBewlyOrBiliPageSwitcher" />
</SettingsItem>
42 changes: 42 additions & 0 deletions src/components/TopBar/BewlyOrBiliTopBarSwitcher.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<script setup lang="ts">
import { settings } from '~/logic'
function toggleBewlyTopBar() {
settings.value.useOriginalBilibiliTopBar = !settings.value.useOriginalBilibiliTopBar
settings.value.showTopBar = !settings.value.showTopBar
}
</script>

<template>
<div
class="group"
pos="fixed top-0 right-0"
z-10
w-full
flex="~ items-center justify-center"
p="t-20px"
m="t-[calc(var(--bew-top-bar-height)-20px)]"
>
<button
style="backdrop-filter: var(--bew-filter-glass-1);"
class="opacity-0 group-hover:opacity-100"
transform="translate-y--100% group-hover:translate-y-0"
flex="~ items-center gap-2"
text="$bew-text-2 sm"
bg="$bew-elevated" p="x-2 y-1"
rounded="full" shadow="$bew-shadow-1"
duration-300
@click="toggleBewlyTopBar"
>
<i i-mingcute:transfer-3-line text-xs />
<span>
<template v-if="settings.showTopBar">
{{ $t('topbar.switch_to_bili_top_bar') }}
</template>
<template v-else>
{{ $t('topbar.switch_to_bewly_top_bar') }}
</template>
</span>
</button>
</div>
</template>
2 changes: 2 additions & 0 deletions src/components/TopBar/components/BewlyOrBiliPageSwitcher.vue
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@ const options = readonly([
])
const showBewlyOrBiliPageSwitcher = computed(() => {
if (settings.value.useOriginalBilibiliHomepage)
return false
if (!isInIframe() && getDockItemByPage(activatedPage.value)?.hasBewlyPage && isHomePage())
return true
if (isInIframe() && getDockItemByPage(activatedPage.value)?.hasBewlyPage)
45 changes: 29 additions & 16 deletions src/contentScripts/index.ts
Original file line number Diff line number Diff line change
@@ -47,12 +47,11 @@ function isSupportedPages(): boolean {
|| /https?:\/\/(?:www\.)?bilibili\.com\/list\/ml.*/.test(currentUrl)
// search page
|| /https?:\/\/search\.bilibili\.com\.*/.test(currentUrl)
// moments
|| (
/https?:\/\/t\.bilibili\.com\.*/.test(currentUrl)
// https://github.com/BewlyBewly/BewlyBewly/issues/1256
&& !/https?:\/\/t\.bilibili\.com\/vote.*/.test(currentUrl)
)
// moments page
// https://github.com/BewlyBewly/BewlyBewly/issues/1246
// https://github.com/BewlyBewly/BewlyBewly/issues/1256
// https://github.com/BewlyBewly/BewlyBewly/issues/1266
|| /https?:\/\/t\.bilibili\.com(?!\/vote|\/share).*/.test(currentUrl)
// moment detail
|| /https?:\/\/(?:www\.)?bilibili\.com\/opus\/.*/.test(currentUrl)
// history page
@@ -100,14 +99,11 @@ export function isSupportedIframePages(): boolean {
|| /https?:\/\/space\.bilibili\.com\/\d+\/favlist.*/.test(currentUrl)
|| /https?:\/\/www\.bilibili\.com\/history.*/.test(currentUrl)
|| /https?:\/\/www\.bilibili\.com\/watchlater\/#\/list.*/.test(currentUrl)
|| (
/https?:\/\/t\.bilibili\.com.*/.test(currentUrl)
// https://github.com/BewlyBewly/BewlyBewly/issues/1246
&& !/https?:\/\/t\.bilibili\.com\/share\/card\/index.*/.test(currentUrl)
// https://github.com/BewlyBewly/BewlyBewly/issues/1256
&& !/https?:\/\/t\.bilibili\.com\/h5\/dynamic\/vote.*/.test(currentUrl)
)

// moments page
// https://github.com/BewlyBewly/BewlyBewly/issues/1246
// https://github.com/BewlyBewly/BewlyBewly/issues/1256
// https://github.com/BewlyBewly/BewlyBewly/issues/1266
|| /https?:\/\/t\.bilibili\.com(?!\/vote|\/share).*/.test(currentUrl)
// Since `Open in drawer` will open the video page within an iframe, so we need to support the following pages
// video page
|| /https?:\/\/(?:www\.)?bilibili\.com\/(?:video|list)\/.*/.test(currentUrl)
@@ -132,10 +128,20 @@ if (isSupportedPages() || isSupportedIframePages()) {
if (settings.value.adaptToOtherPageStyles)
useDark()

if (settings.value.adaptToOtherPageStyles)
if (settings.value.adaptToOtherPageStyles) {
document.documentElement.classList.add('bewly-design')
else

// Remove the Bilibili Evolved's dark mode style
runWhenIdle(async () => {
const darkModeStyle = document.head.querySelector('#dark-mode')
if (darkModeStyle)
document.head.removeChild(darkModeStyle)
})
}

else {
document.documentElement.classList.remove('bewly-design')
}
}

if (settings.value.adaptToOtherPageStyles && isHomePage()) {
@@ -187,6 +193,13 @@ async function onDOMLoaded() {
// Remove the original Bilibili homepage if in Bilibili homepage & useOriginalBilibiliHomepage is enabled
document.body.innerHTML = ''

// Remove the Bilibili Evolved homepage
injectCSS(`
.home-redesign-base {
display: none !important;
}
`)

if (originalTopBarInnerUselessContents)
originalTopBarInnerUselessContents.forEach(item => (item as HTMLElement).style.display = 'none')
if (originalTopBar)
Loading

0 comments on commit 32b9484

Please sign in to comment.