From 55273b770beb8248475b9c20bdc5df0e2d2574a2 Mon Sep 17 00:00:00 2001 From: apades <36807043+apades@users.noreply.github.com> Date: Mon, 11 Nov 2024 01:53:10 +0800 Subject: [PATCH] fix(iframe-drawer): cannot change url when video changes (#1072) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 抽屉页面内点击其他链接跳转后,“在新标签页打开“按钮无法正确导向至当前链接 #1068 * chore: update * chore: update --------- Co-authored-by: Hakadao --- src/components/IframeDrawer.vue | 32 ++++++++++----------- src/contentScripts/views/App.vue | 26 ++++++++--------- src/inject/README.md | 3 ++ src/inject/index.js | 48 ++++++++++++++++++++++++++++++++ src/manifest.ts | 19 +++++++++++++ tsup.config.ts | 10 ++++++- 6 files changed, 108 insertions(+), 30 deletions(-) create mode 100644 src/inject/README.md create mode 100644 src/inject/index.js diff --git a/src/components/IframeDrawer.vue b/src/components/IframeDrawer.vue index 0eafaf55..6b5fb189 100644 --- a/src/components/IframeDrawer.vue +++ b/src/components/IframeDrawer.vue @@ -26,9 +26,10 @@ const inIframe = computed((): boolean => { }) useEventListener(window, 'popstate', updateIframeUrl) -// nextTick(() => { -// useEventListener(iframeRef.value?.contentWindow, 'pushstate', updateCurrentUrl) -// }) +nextTick(() => { + useEventListener(iframeRef.value?.contentWindow, 'historyChange', updateCurrentUrl) + useEventListener(iframeRef.value?.contentWindow, 'popstate', updateCurrentUrl) +}) onMounted(async () => { history.pushState(null, '', props.url) @@ -46,17 +47,17 @@ onUnmounted(() => { history.replaceState(null, '', 'https://www.bilibili.com') }) -// function updateCurrentUrl() { -// if (iframeRef.value?.contentWindow) { -// try { -// currentUrl.value = iframeRef.value.contentWindow.location.href.replace(/\/$/, '') -// history.pushState(null, '', currentUrl.value.replace(/\/$/, '')) -// } -// catch (error) { -// console.error('Unable to access iframe URL:', error) -// } -// } -// } +function updateCurrentUrl() { + if (iframeRef.value?.contentWindow) { + try { + currentUrl.value = iframeRef.value.contentWindow.location.href.replace(/\/$/, '') + history.pushState(null, '', currentUrl.value.replace(/\/$/, '')) + } + catch (error) { + console.error('Unable to access iframe URL:', error) + } + } +} async function updateIframeUrl() { if (isHomePage()) { @@ -98,7 +99,6 @@ async function releaseIframeResources() { } function handleOpenInNewTab() { - // window.open(props.url, '_blank') if (iframeRef.value) window.open(iframeRef.value.contentWindow?.location.href.replace(/\/$/, ''), '_blank') } @@ -236,7 +236,7 @@ watchEffect(() => { >