From 619dc034afcf2ec41f4c6af94d789c85bc765252 Mon Sep 17 00:00:00 2001 From: Hakadao Date: Sun, 13 Oct 2024 19:49:14 +0800 Subject: [PATCH] refactor: adjust the directory structure of `messageListeners/` (#1066) --- src/background/index.ts | 6 ++- .../messageListeners/{ => api}/anime.ts | 4 +- .../messageListeners/{ => api}/auth.ts | 4 +- .../messageListeners/{ => api}/favorite.ts | 4 +- .../messageListeners/{ => api}/history.ts | 4 +- .../messageListeners/{ => api}/index.ts | 4 +- .../messageListeners/{ => api}/moment.ts | 4 +- .../{ => api}/notification.ts | 4 +- .../messageListeners/{ => api}/ranking.ts | 4 +- .../messageListeners/{ => api}/search.ts | 4 +- .../messageListeners/{ => api}/user.ts | 4 +- .../messageListeners/{ => api}/video.ts | 4 +- .../messageListeners/{ => api}/watchLater.ts | 4 +- src/background/messageListeners/tab.ts | 26 +++++++++++ src/components/SearchBar/SearchBar.vue | 3 +- src/components/TopBar/OldTopBar.vue | 4 +- src/components/TopBar/TopBar.vue | 4 +- .../TopBar/components/FavoritesPop.vue | 4 +- .../TopBar/components/HistoryPop.vue | 3 +- .../TopBar/components/MomentsPop.vue | 3 +- .../TopBar/components/NotificationsPop.vue | 3 +- .../TopBar/components/UserPanelPop.vue | 3 +- .../TopBar/components/WatchLaterPop.vue | 3 +- .../oldTopBarComponents/OldUserPanelPop.vue | 3 +- src/components/VideoCard/VideoCard.vue | 3 +- .../VideoCardContextMenu.vue | 44 +++++++++++-------- .../components/DislikeDialog.vue | 3 +- src/contentScripts/views/Anime/Anime.vue | 3 +- .../views/Anime/components/AnimeTimeTable.vue | 3 +- .../views/Favorites/Favorites.vue | 3 +- src/contentScripts/views/History/History.vue | 3 +- .../views/Home/components/Following.vue | 4 +- .../views/Home/components/ForYou.vue | 3 +- .../views/Home/components/Ranking.vue | 3 +- .../Home/components/SubscribedSeries.vue | 4 +- .../views/Home/components/Trending.vue | 3 +- .../views/WatchLater/WatchLater.vue | 3 +- src/manifest.ts | 1 + src/{composables => utils}/api.ts | 6 +-- src/utils/tab.ts | 10 +++++ 40 files changed, 114 insertions(+), 98 deletions(-) rename src/background/messageListeners/{ => api}/anime.ts (95%) rename src/background/messageListeners/{ => api}/auth.ts (94%) rename src/background/messageListeners/{ => api}/favorite.ts (94%) rename src/background/messageListeners/{ => api}/history.ts (97%) rename src/background/messageListeners/{ => api}/index.ts (93%) rename src/background/messageListeners/{ => api}/moment.ts (92%) rename src/background/messageListeners/{ => api}/notification.ts (87%) rename src/background/messageListeners/{ => api}/ranking.ts (90%) rename src/background/messageListeners/{ => api}/search.ts (78%) rename src/background/messageListeners/{ => api}/user.ts (92%) rename src/background/messageListeners/{ => api}/video.ts (97%) rename src/background/messageListeners/{ => api}/watchLater.ts (96%) create mode 100644 src/background/messageListeners/tab.ts rename src/{composables => utils}/api.ts (96%) create mode 100644 src/utils/tab.ts diff --git a/src/background/index.ts b/src/background/index.ts index c31a1a96..1348d052 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,6 +1,7 @@ import browser from 'webextension-polyfill' -import { setupAllMsgLstnrs } from './messageListeners' +import { setupApiMsgLstnrs } from './messageListeners/api' +import { setupTabMsgLstnrs } from './messageListeners/tab' browser.runtime.onInstalled.addListener((): void => { // eslint-disable-next-line no-console @@ -36,4 +37,5 @@ if (process.env.FIREFOX) { } // Setup all message listeners -setupAllMsgLstnrs() +setupApiMsgLstnrs() +setupTabMsgLstnrs() diff --git a/src/background/messageListeners/anime.ts b/src/background/messageListeners/api/anime.ts similarity index 95% rename from src/background/messageListeners/anime.ts rename to src/background/messageListeners/api/anime.ts index 9a33b98c..0a1bdee0 100644 --- a/src/background/messageListeners/anime.ts +++ b/src/background/messageListeners/api/anime.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_ANIME = { // https://github.com/SocialSisterYi/bilibili-API-collect/blob/36e250090800793b41b223b55eefdcbb9391b53e/user/space.md#%E6%9F%A5%E8%AF%A2%E7%94%A8%E6%88%B7%E8%BF%BD%E7%95%AA%E8%BF%BD%E5%89%A7%E6%98%8E%E7%BB%86 diff --git a/src/background/messageListeners/auth.ts b/src/background/messageListeners/api/auth.ts similarity index 94% rename from src/background/messageListeners/auth.ts rename to src/background/messageListeners/api/auth.ts index e8b6e50f..3f36034d 100644 --- a/src/background/messageListeners/auth.ts +++ b/src/background/messageListeners/api/auth.ts @@ -1,6 +1,6 @@ // 由于 sendResponse 复杂, 所以使用自定义的函数 -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_AUTH = { // biliJct 似乎没有使用 diff --git a/src/background/messageListeners/favorite.ts b/src/background/messageListeners/api/favorite.ts similarity index 94% rename from src/background/messageListeners/favorite.ts rename to src/background/messageListeners/api/favorite.ts index a704a491..9402bf2d 100644 --- a/src/background/messageListeners/favorite.ts +++ b/src/background/messageListeners/api/favorite.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_FAVORITE = { // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/fav/info.md#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E7%94%A8%E6%88%B7%E5%88%9B%E5%BB%BA%E7%9A%84%E6%89%80%E6%9C%89%E6%94%B6%E8%97%8F%E5%A4%B9%E4%BF%A1%E6%81%AF diff --git a/src/background/messageListeners/history.ts b/src/background/messageListeners/api/history.ts similarity index 97% rename from src/background/messageListeners/history.ts rename to src/background/messageListeners/api/history.ts index 68cd8b70..c717c254 100644 --- a/src/background/messageListeners/history.ts +++ b/src/background/messageListeners/api/history.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_HISTORY = { // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/history&toview/history.md#%E8%8E%B7%E5%8F%96%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95%E5%88%97%E8%A1%A8_web%E7%AB%AF diff --git a/src/background/messageListeners/index.ts b/src/background/messageListeners/api/index.ts similarity index 93% rename from src/background/messageListeners/index.ts rename to src/background/messageListeners/api/index.ts index 9d3f6ee0..c6457ff6 100644 --- a/src/background/messageListeners/index.ts +++ b/src/background/messageListeners/api/index.ts @@ -1,6 +1,6 @@ import browser from 'webextension-polyfill' -import { apiListenerFactory } from '../utils' +import { apiListenerFactory } from '../../utils' import API_ANIME from './anime' import API_AUTH from './auth' import API_FAVORITE from './favorite' @@ -36,7 +36,7 @@ const FullAPI = Object.assign({}, ...API_COLLECTION) // Create a message listener for each API const handleMessage = apiListenerFactory(FullAPI) -export function setupAllMsgLstnrs() { +export function setupApiMsgLstnrs() { browser.runtime.onConnect.removeListener(handleConnect) browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/moment.ts b/src/background/messageListeners/api/moment.ts similarity index 92% rename from src/background/messageListeners/moment.ts rename to src/background/messageListeners/api/moment.ts index 7fa80501..ad468553 100644 --- a/src/background/messageListeners/moment.ts +++ b/src/background/messageListeners/api/moment.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_MOMENT = { getTopBarNewMomentsCount: { diff --git a/src/background/messageListeners/notification.ts b/src/background/messageListeners/api/notification.ts similarity index 87% rename from src/background/messageListeners/notification.ts rename to src/background/messageListeners/api/notification.ts index e6138aff..ea816517 100644 --- a/src/background/messageListeners/notification.ts +++ b/src/background/messageListeners/api/notification.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_NOTIFICATION = { getUnreadMsg: { diff --git a/src/background/messageListeners/ranking.ts b/src/background/messageListeners/api/ranking.ts similarity index 90% rename from src/background/messageListeners/ranking.ts rename to src/background/messageListeners/api/ranking.ts index ba6874ac..56661fad 100644 --- a/src/background/messageListeners/ranking.ts +++ b/src/background/messageListeners/api/ranking.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_RANKING = { // https://github.com/SocialSisterYi/bilibili-API-collect/blob/7873a79022a5606e2391d93b411a05576a0df111/docs/video_ranking/ranking.md#%E8%8E%B7%E5%8F%96%E5%88%86%E5%8C%BA%E8%A7%86%E9%A2%91%E6%8E%92%E8%A1%8C%E6%A6%9C%E5%88%97%E8%A1%A8 diff --git a/src/background/messageListeners/search.ts b/src/background/messageListeners/api/search.ts similarity index 78% rename from src/background/messageListeners/search.ts rename to src/background/messageListeners/api/search.ts index cd882c9b..650b4854 100644 --- a/src/background/messageListeners/search.ts +++ b/src/background/messageListeners/api/search.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_SEARCH = { getSearchSuggestion: { diff --git a/src/background/messageListeners/user.ts b/src/background/messageListeners/api/user.ts similarity index 92% rename from src/background/messageListeners/user.ts rename to src/background/messageListeners/api/user.ts index e127ebad..7c039447 100644 --- a/src/background/messageListeners/user.ts +++ b/src/background/messageListeners/api/user.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_USER = { // https://github.com/SocialSisterYi/bilibili-API-collect/blob/e379d904c2753fa30e9083f59016f07e89d19467/docs/login/login_info.md#%E5%AF%BC%E8%88%AA%E6%A0%8F%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF diff --git a/src/background/messageListeners/video.ts b/src/background/messageListeners/api/video.ts similarity index 97% rename from src/background/messageListeners/video.ts rename to src/background/messageListeners/api/video.ts index 9d5e315e..2e19d474 100644 --- a/src/background/messageListeners/video.ts +++ b/src/background/messageListeners/api/video.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_VIDEO = { getRecommendVideos: { diff --git a/src/background/messageListeners/watchLater.ts b/src/background/messageListeners/api/watchLater.ts similarity index 96% rename from src/background/messageListeners/watchLater.ts rename to src/background/messageListeners/api/watchLater.ts index 62f422c0..42a3eda2 100644 --- a/src/background/messageListeners/watchLater.ts +++ b/src/background/messageListeners/api/watchLater.ts @@ -1,5 +1,5 @@ -import type { APIMAP } from '../utils' -import { AHS } from '../utils' +import type { APIMAP } from '../../utils' +import { AHS } from '../../utils' const API_WATCHLATER = { // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/history&toview/toview.md#%E8%A7%86%E9%A2%91%E6%B7%BB%E5%8A%A0%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B diff --git a/src/background/messageListeners/tab.ts b/src/background/messageListeners/tab.ts new file mode 100644 index 00000000..5e17384a --- /dev/null +++ b/src/background/messageListeners/tab.ts @@ -0,0 +1,26 @@ +import browser from 'webextension-polyfill' + +interface Message { + contentScriptQuery: string + [key: string]: any +} + +export enum TAB_MESSAGE { + OPEN_LINK_IN_BACKGROUND = 'openLinkInBackground', +} + +function handleMessage(message: Message) { + if (message.contentScriptQuery === TAB_MESSAGE.OPEN_LINK_IN_BACKGROUND) { + return browser.tabs.create({ url: message.url, active: false }) + } +} + +export function setupTabMsgLstnrs() { + browser.runtime.onMessage.removeListener(handleConnect) + browser.runtime.onMessage.addListener(handleConnect) +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} diff --git a/src/components/SearchBar/SearchBar.vue b/src/components/SearchBar/SearchBar.vue index fdc82811..188072bb 100644 --- a/src/components/SearchBar/SearchBar.vue +++ b/src/components/SearchBar/SearchBar.vue @@ -2,7 +2,7 @@ import { onKeyStroke, useDebounceFn } from '@vueuse/core' import DOMPurify from 'dompurify' -import { useApiClient } from '~/composables/api' +import api from '~/utils/api' import { findLeafActiveElement } from '~/utils/element' import type { HistoryItem, SuggestionItem, SuggestionResponse } from './searchHistoryProvider' @@ -19,7 +19,6 @@ defineProps<{ focusedCharacter?: string }>() -const api = useApiClient() const keywordRef = ref() const isFocus = ref(false) const keyword = ref('') diff --git a/src/components/TopBar/OldTopBar.vue b/src/components/TopBar/OldTopBar.vue index ff3204ef..970b6f81 100644 --- a/src/components/TopBar/OldTopBar.vue +++ b/src/components/TopBar/OldTopBar.vue @@ -2,12 +2,12 @@ import { useMouseInElement } from '@vueuse/core' import type { Ref, UnwrapNestedRefs } from 'vue' -import { useApiClient } from '~/composables/api' import { useBewlyApp } from '~/composables/useAppProvider' import { useDelayedHover } from '~/composables/useDelayedHover' import { OVERLAY_SCROLL_BAR_SCROLL, TOP_BAR_VISIBILITY_CHANGE } from '~/constants/globalEvents' import { AppPage } from '~/enums/appEnums' import { settings } from '~/logic' +import api from '~/utils/api' import { getUserID, isHomePage } from '~/utils/main' import emitter from '~/utils/mitt' @@ -43,8 +43,6 @@ const hideTopBar = ref(false) const headerTarget = ref(null) const { isOutside: isOutsideTopBar } = useMouseInElement(headerTarget) -const api = useApiClient() - // initially, assume the user is logged in cuz data retrieval is slow, which may show the login // button even after login. if the user is not logged in, the login button will show up later const isLogin = ref(true) diff --git a/src/components/TopBar/TopBar.vue b/src/components/TopBar/TopBar.vue index b288def0..29add03b 100644 --- a/src/components/TopBar/TopBar.vue +++ b/src/components/TopBar/TopBar.vue @@ -2,12 +2,12 @@ import { onClickOutside, useMouseInElement } from '@vueuse/core' import type { Ref, UnwrapNestedRefs } from 'vue' -import { useApiClient } from '~/composables/api' import { useBewlyApp } from '~/composables/useAppProvider' import { useDelayedHover } from '~/composables/useDelayedHover' import { OVERLAY_SCROLL_BAR_SCROLL, TOP_BAR_VISIBILITY_CHANGE } from '~/constants/globalEvents' import { AppPage } from '~/enums/appEnums' import { settings } from '~/logic' +import api from '~/utils/api' import { getUserID, isHomePage } from '~/utils/main' import emitter from '~/utils/mitt' @@ -43,8 +43,6 @@ const hideTopBar = ref(false) const headerTarget = ref(null) const { isOutside: isOutsideTopBar } = useMouseInElement(headerTarget) -const api = useApiClient() - // initially, assume the user is logged in cuz data retrieval is slow, which may show the login // button even after login. if the user is not logged in, the login button will show up later const isLogin = ref(true) diff --git a/src/components/TopBar/components/FavoritesPop.vue b/src/components/TopBar/components/FavoritesPop.vue index 9cb4c3c3..3be446ce 100644 --- a/src/components/TopBar/components/FavoritesPop.vue +++ b/src/components/TopBar/components/FavoritesPop.vue @@ -4,15 +4,13 @@ import { onMounted, reactive, ref, watch } from 'vue' import Empty from '~/components/Empty.vue' import Loading from '~/components/Loading.vue' -import { useApiClient } from '~/composables/api' +import api from '~/utils/api' import { calcCurrentTime } from '~/utils/dataFormatter' import { getUserID, removeHttpFromUrl, scrollToTop } from '~/utils/main' import type { FavoriteCategory, FavoriteResource } from '../types' import ALink from './ALink.vue' -const api = useApiClient() - const favoriteCategories = reactive>([]) const favoriteResources = reactive>([]) diff --git a/src/components/TopBar/components/HistoryPop.vue b/src/components/TopBar/components/HistoryPop.vue index b2767eec..b2b21283 100644 --- a/src/components/TopBar/components/HistoryPop.vue +++ b/src/components/TopBar/components/HistoryPop.vue @@ -6,16 +6,15 @@ import { useI18n } from 'vue-i18n' import Empty from '~/components/Empty.vue' import Loading from '~/components/Loading.vue' import Progress from '~/components/Progress.vue' -import { useApiClient } from '~/composables/api' import type { HistoryResult, List as HistoryItem } from '~/models/history/history' import { Business } from '~/models/history/history' +import api from '~/utils/api' import { calcCurrentTime } from '~/utils/dataFormatter' import { removeHttpFromUrl, scrollToTop } from '~/utils/main' import ALink from './ALink.vue' const { t } = useI18n() -const api = useApiClient() const historys = reactive>([]) const historyTabs = computed(() => [ { diff --git a/src/components/TopBar/components/MomentsPop.vue b/src/components/TopBar/components/MomentsPop.vue index 04a55d23..9db8f6a2 100644 --- a/src/components/TopBar/components/MomentsPop.vue +++ b/src/components/TopBar/components/MomentsPop.vue @@ -6,9 +6,9 @@ import { useI18n } from 'vue-i18n' import Empty from '~/components/Empty.vue' import Loading from '~/components/Loading.vue' import Tooltip from '~/components/Tooltip.vue' -import { useApiClient } from '~/composables/api' import type { TopBarLiveMomentResult } from '~/models/moment/topBarLiveMoment' import type { TopBarMomentResult } from '~/models/moment/topBarMoment' +import api from '~/utils/api' import { getCSRF, scrollToTop } from '~/utils/main' import ALink from './ALink.vue' @@ -28,7 +28,6 @@ interface MomentCard { } const { t } = useI18n() -const api = useApiClient() const moments = reactive([]) const addedWatchLaterList = reactive([]) diff --git a/src/components/TopBar/components/NotificationsPop.vue b/src/components/TopBar/components/NotificationsPop.vue index c18da9fc..dba1a393 100644 --- a/src/components/TopBar/components/NotificationsPop.vue +++ b/src/components/TopBar/components/NotificationsPop.vue @@ -1,12 +1,11 @@