From 633826cb895b84040f004bf148df15714dbe50bf Mon Sep 17 00:00:00 2001 From: kuaifan Date: Mon, 12 Jan 2026 07:27:18 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=BF=81=E7=A7=BB=E5=88=B0=20navig?= =?UTF-8?q?ationHistory=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将已废弃的 webContents 导航方法迁移到新的 navigationHistory API: - canGoBack() → navigationHistory.canGoBack() - canGoForward() → navigationHistory.canGoForward() - goBack() → navigationHistory.goBack() - goForward() → navigationHistory.goForward() --- electron/lib/navigation.js | 8 ++--- electron/lib/web-tab-manager.js | 53 ++++++++++++++++----------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/electron/lib/navigation.js b/electron/lib/navigation.js index ec7343249..4bf266e04 100644 --- a/electron/lib/navigation.js +++ b/electron/lib/navigation.js @@ -123,17 +123,17 @@ function navigateTopLevel(webContents, direction) { return false } if (direction === 'back') { - if (!webContents.canGoBack()) { + if (!webContents.navigationHistory.canGoBack()) { return false } - webContents.goBack() + webContents.navigationHistory.goBack() return true } if (direction === 'forward') { - if (!webContents.canGoForward()) { + if (!webContents.navigationHistory.canGoForward()) { return false } - webContents.goForward() + webContents.navigationHistory.goForward() return true } return false diff --git a/electron/lib/web-tab-manager.js b/electron/lib/web-tab-manager.js index 7e0dfa08b..1015abc34 100644 --- a/electron/lib/web-tab-manager.js +++ b/electron/lib/web-tab-manager.js @@ -1642,6 +1642,23 @@ function registerIPC() { event.returnValue = "ok" }) + /** + * 内置浏览器 - 延迟发送导航状态 + */ + function notifyNavigationState(item) { + setTimeout(() => { + const wd = webTabWindows.get(item.view.webTabWindowId) + if (wd && wd.window) { + utils.onDispatchEvent(wd.window.webContents, { + event: 'navigation-state', + id: item.id, + canGoBack: item.view.webContents.navigationHistory.canGoBack(), + canGoForward: item.view.webContents.navigationHistory.canGoForward() + }).then(_ => { }) + } + }, 100) + } + /** * 内置浏览器 - 后退 */ @@ -1652,19 +1669,9 @@ function registerIPC() { event.returnValue = "ok" return } - if (item.view.webContents.canGoBack()) { - item.view.webContents.goBack() - setTimeout(() => { - const wd = webTabWindows.get(item.view.webTabWindowId) - if (wd && wd.window) { - utils.onDispatchEvent(wd.window.webContents, { - event: 'navigation-state', - id: item.id, - canGoBack: item.view.webContents.canGoBack(), - canGoForward: item.view.webContents.canGoForward() - }).then(_ => { }) - } - }, 100) + if (item.view.webContents.navigationHistory.canGoBack()) { + item.view.webContents.navigationHistory.goBack() + notifyNavigationState(item) } event.returnValue = "ok" }) @@ -1679,19 +1686,9 @@ function registerIPC() { event.returnValue = "ok" return } - if (item.view.webContents.canGoForward()) { - item.view.webContents.goForward() - setTimeout(() => { - const wd = webTabWindows.get(item.view.webTabWindowId) - if (wd && wd.window) { - utils.onDispatchEvent(wd.window.webContents, { - event: 'navigation-state', - id: item.id, - canGoBack: item.view.webContents.canGoBack(), - canGoForward: item.view.webContents.canGoForward() - }).then(_ => { }) - } - }, 100) + if (item.view.webContents.navigationHistory.canGoForward()) { + item.view.webContents.navigationHistory.goForward() + notifyNavigationState(item) } event.returnValue = "ok" }) @@ -1735,8 +1732,8 @@ function registerIPC() { return } - const canGoBack = item.view.webContents.canGoBack() - const canGoForward = item.view.webContents.canGoForward() + const canGoBack = item.view.webContents.navigationHistory.canGoBack() + const canGoForward = item.view.webContents.navigationHistory.canGoForward() const wd = webTabWindows.get(item.view.webTabWindowId) if (wd && wd.window) {