From 6aff27547a55d2c5c67c632dad5ed2017bd0cff8 Mon Sep 17 00:00:00 2001 From: Hakadao Date: Thu, 24 Oct 2024 00:07:44 +0800 Subject: [PATCH] feat: remember last opened page when returning to homepage --- src/contentScripts/index.ts | 2 -- src/contentScripts/views/App.vue | 15 ++++++++++++++- src/utils/main.ts | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/contentScripts/index.ts b/src/contentScripts/index.ts index 2d0bcae3..23e8ddf9 100644 --- a/src/contentScripts/index.ts +++ b/src/contentScripts/index.ts @@ -34,8 +34,6 @@ function isSupportedPages(): boolean { if ( // homepage isHomePage() - // fix #166 https://github.com/hakadao/BewlyBewly/issues/166 - || /https?:\/\/www\.bilibili\.com\/\?bvid=.*$/.test(currentUrl) // video page || /https?:\/\/(?:www\.)?bilibili\.com\/(?:video|list)\/.*/.test(currentUrl) diff --git a/src/contentScripts/views/App.vue b/src/contentScripts/views/App.vue index 2c2942aa..e3366362 100644 --- a/src/contentScripts/views/App.vue +++ b/src/contentScripts/views/App.vue @@ -15,7 +15,16 @@ import { setupNecessarySettingsWatchers } from './necessarySettingsWatchers' const { isDark } = useDark() const [showSettings, toggleSettings] = useToggle(false) -const activatedPage = ref(settings.value.dockItemVisibilityList.find(e => e.visible === true)?.page ?? AppPage.Home) +// Get the 'page' query parameter from the URL +function getPageParam(): AppPage | null { + const urlParams = new URLSearchParams(window.location.search) + const result = urlParams.get('page') as AppPage | null + if (result && Object.values(AppPage).includes(result)) + return result + return null +} + +const activatedPage = ref(getPageParam() || (settings.value.dockItemVisibilityList.find(e => e.visible === true)?.page || AppPage.Home)) const pages = { [AppPage.Home]: defineAsyncComponent(() => import('./Home/Home.vue')), [AppPage.Search]: defineAsyncComponent(() => import('./Search/Search.vue')), @@ -53,6 +62,10 @@ const showBewlyPage = computed((): boolean => { watch( () => activatedPage.value, () => { + // Update the URL query parameter when activatedPage changes + const url = new URL(window.location.href) + url.searchParams.set('page', activatedPage.value) + window.history.replaceState({}, '', url.toString()) const osInstance = scrollbarRef.value.osInstance() osInstance.elements().viewport.scrollTop = 0 }, diff --git a/src/utils/main.ts b/src/utils/main.ts index e70846ab..a039350f 100644 --- a/src/utils/main.ts +++ b/src/utils/main.ts @@ -154,6 +154,7 @@ export function isHomePage(url: string = location.href): boolean { /https?:\/\/(?:www\.)?bilibili.com\/?(?:#\/?)?$/.test(url) || /https?:\/\/(?:www\.)?bilibili.com\/index\.html$/.test(url) || /https?:\/\/(?:www\.)?bilibili.com\/\?spm_id_from=.*/.test(url) + || /https?:\/\/www\.bilibili\.com\/\?.*$/.test(url) ) { return true }