From 268206f010f9bdeddd52b7776cc9aaa76f4c9725 Mon Sep 17 00:00:00 2001 From: Hakadao Date: Mon, 6 Nov 2023 01:05:26 +0800 Subject: [PATCH] refactor: refactor some functions implementation --- src/components/Topbar/Topbar.vue | 21 +++++------ .../Topbar/components/FavoritesPop.vue | 36 +++++++++---------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/components/Topbar/Topbar.vue b/src/components/Topbar/Topbar.vue index 76831225..20d133dc 100644 --- a/src/components/Topbar/Topbar.vue +++ b/src/components/Topbar/Topbar.vue @@ -40,18 +40,7 @@ const newMomentsCount = ref(0) const logo = ref() as Ref const avatarImg = ref() as Ref const avatarShadow = ref() as Ref - -const isHomePage = computed(() => { - if ( - /https?:\/\/bilibili.com\/?$/.test(location.href) - || /https?:\/\/www.bilibili.com\/?$/.test(location.href) - || /https?:\/\/www.bilibili.com\/index.html$/.test(location.href) - || /https?:\/\/bilibili.com\/\?spm_id_from=.*/.test(location.href) - || /https?:\/\/www.bilibili.com\/\?spm_id_from=(.)*/.test(location.href) - ) - return true - return false -}) +const favoritesPopRef = ref() watch( showNotificationsPop, @@ -75,6 +64,13 @@ watch( }, ) +// watch(showFavoritesPop, (newVal, oldVal) => { +// if (newVal === oldVal) +// return +// if (newVal && favoritesPopRef.value) +// favoritesPopRef.value.refreshFavoriteResources() +// }) + onMounted(() => { initData() }) @@ -374,6 +370,7 @@ async function getTopbarNewMomentsCount() {
>([]) const activatedMediaId = ref(0) const activatedFavoriteTitle = ref() const currentPageNum = ref(1) -const keyword = ref() const isLoading = ref(false) // when noMoreContent is true, the user can't scroll down to load more content @@ -32,18 +31,14 @@ watch(activatedMediaId, (newVal: number, oldVal: number) => { if (favoriteVideosWrap.value) smoothScrollToTop(favoriteVideosWrap.value, 300) - getFavoriteResources(newVal, 1) + currentPageNum.value = 1 + getFavoriteResources() }) onMounted(async () => { await getFavoriteCategories() activatedMediaId.value = favoriteCategories[0].id activatedFavoriteTitle.value = favoriteCategories[0].title - getFavoriteResources( - activatedMediaId.value, - ++currentPageNum.value, - keyword.value, - ) if (favoriteVideosWrap.value) { favoriteVideosWrap.value.addEventListener('scroll', () => { @@ -56,8 +51,10 @@ onMounted(async () => { && favoriteResources.length > 0 && !isLoading.value ) { - if (activatedMediaId.value && !noMoreContent.value) - getFavoriteResources(activatedMediaId.value, ++currentPageNum.value) + if (activatedMediaId.value && !noMoreContent.value) { + currentPageNum.value++ + getFavoriteResources() + } } }) } @@ -80,22 +77,15 @@ async function getFavoriteCategories() { /** * Get favorite video resources - * @param mediaId - * @param pageNum - * @param keyword */ -function getFavoriteResources( - mediaId: number, - pageNum: number, - keyword = '' as string, -) { +function getFavoriteResources() { isLoading.value = true browser.runtime .sendMessage({ contentScriptQuery: 'getFavoriteResources', - mediaId, - pageNum, - keyword, + mediaId: activatedMediaId.value, + pageNum: currentPageNum.value, + keyword: '', }) .then((res) => { if (res.code === 0) { @@ -117,6 +107,12 @@ function getFavoriteResources( }) } +function refreshFavoriteResources() { + favoriteResources.length = 0 + currentPageNum.value = 1 + getFavoriteResources() +} + function changeCategory(categoryItem: FavoriteCategory) { activatedMediaId.value = categoryItem.id activatedFavoriteTitle.value = categoryItem.title