From bc3b9d283cca7190d3b62d1b99f6faae4cce0a1d Mon Sep 17 00:00:00 2001 From: Hakadao Date: Tue, 10 Sep 2024 13:17:16 +0800 Subject: [PATCH] perf: improve BewlyBewly inject speed --- src/constants/globalEvents.ts | 1 + src/contentScripts/index.ts | 8 ++++++-- src/contentScripts/views/App.vue | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/constants/globalEvents.ts b/src/constants/globalEvents.ts index abb27a5411..5b9b8849f7 100644 --- a/src/constants/globalEvents.ts +++ b/src/constants/globalEvents.ts @@ -1,2 +1,3 @@ export const TOP_BAR_VISIBILITY_CHANGE = 'topBarVisibilityChange' export const OVERLAY_SCROLL_BAR_SCROLL = 'overlayScrollBarScroll' +export const BEWLY_MOUNTED = 'bewlyMounted' diff --git a/src/contentScripts/index.ts b/src/contentScripts/index.ts index 59cbb68403..a24037d356 100644 --- a/src/contentScripts/index.ts +++ b/src/contentScripts/index.ts @@ -4,6 +4,7 @@ import '~/styles' import { createApp } from 'vue' import { useDark } from '~/composables/useDark' +import { BEWLY_MOUNTED } from '~/constants/globalEvents' import { settings } from '~/logic' import { setupApp } from '~/logic/common-setup' import { runWhenIdle } from '~/utils/lazyLoad' @@ -113,6 +114,11 @@ if (settings.value.adaptToOtherPageStyles && isHomePage()) { `) } +window.addEventListener(BEWLY_MOUNTED, () => { + if (beforeLoadedStyleEl) + document.documentElement.removeChild(beforeLoadedStyleEl) +}) + // Set the original Bilibili top bar to `display: none` to prevent it from showing before the load // see: https://github.com/BewlyBewly/BewlyBewly/issues/967 let removeOriginalTopBar: HTMLStyleElement | null = null @@ -139,8 +145,6 @@ async function onDOMLoaded() { if (originalTopBar) document.body.appendChild(originalTopBar) } - if (beforeLoadedStyleEl) - document.documentElement.removeChild(beforeLoadedStyleEl) if (isSupportedPages()) { // Then inject the app diff --git a/src/contentScripts/views/App.vue b/src/contentScripts/views/App.vue index dbb90266ce..b9d699567b 100644 --- a/src/contentScripts/views/App.vue +++ b/src/contentScripts/views/App.vue @@ -6,7 +6,7 @@ import browser from 'webextension-polyfill' import type { BewlyAppProvider } from '~/composables/useAppProvider' import { useDark } from '~/composables/useDark' -import { OVERLAY_SCROLL_BAR_SCROLL } from '~/constants/globalEvents' +import { BEWLY_MOUNTED, OVERLAY_SCROLL_BAR_SCROLL } from '~/constants/globalEvents' import { AppPage, LanguageType } from '~/enums/appEnums' import { accessKey, settings } from '~/logic' import { getUserID, isHomePage, scrollToTop } from '~/utils/main' @@ -109,6 +109,7 @@ onBeforeMount(() => { }) onMounted(() => { + window.dispatchEvent(new CustomEvent(BEWLY_MOUNTED)) // openVideoPageIfBvidExists() if (isHomePage()) {