refactor: 迁移到 navigationHistory API

将已废弃的 webContents 导航方法迁移到新的 navigationHistory API:
  - canGoBack() → navigationHistory.canGoBack()
  - canGoForward() → navigationHistory.canGoForward()
  - goBack() → navigationHistory.goBack()
  - goForward() → navigationHistory.goForward()
This commit is contained in:
kuaifan 2026-01-12 07:27:18 +00:00
parent cf6d180fc5
commit 633826cb89
2 changed files with 29 additions and 32 deletions

View File

@ -123,17 +123,17 @@ function navigateTopLevel(webContents, direction) {
return false return false
} }
if (direction === 'back') { if (direction === 'back') {
if (!webContents.canGoBack()) { if (!webContents.navigationHistory.canGoBack()) {
return false return false
} }
webContents.goBack() webContents.navigationHistory.goBack()
return true return true
} }
if (direction === 'forward') { if (direction === 'forward') {
if (!webContents.canGoForward()) { if (!webContents.navigationHistory.canGoForward()) {
return false return false
} }
webContents.goForward() webContents.navigationHistory.goForward()
return true return true
} }
return false return false

View File

@ -1642,6 +1642,23 @@ function registerIPC() {
event.returnValue = "ok" 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" event.returnValue = "ok"
return return
} }
if (item.view.webContents.canGoBack()) { if (item.view.webContents.navigationHistory.canGoBack()) {
item.view.webContents.goBack() item.view.webContents.navigationHistory.goBack()
setTimeout(() => { notifyNavigationState(item)
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)
} }
event.returnValue = "ok" event.returnValue = "ok"
}) })
@ -1679,19 +1686,9 @@ function registerIPC() {
event.returnValue = "ok" event.returnValue = "ok"
return return
} }
if (item.view.webContents.canGoForward()) { if (item.view.webContents.navigationHistory.canGoForward()) {
item.view.webContents.goForward() item.view.webContents.navigationHistory.goForward()
setTimeout(() => { notifyNavigationState(item)
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)
} }
event.returnValue = "ok" event.returnValue = "ok"
}) })
@ -1735,8 +1732,8 @@ function registerIPC() {
return return
} }
const canGoBack = item.view.webContents.canGoBack() const canGoBack = item.view.webContents.navigationHistory.canGoBack()
const canGoForward = item.view.webContents.canGoForward() const canGoForward = item.view.webContents.navigationHistory.canGoForward()
const wd = webTabWindows.get(item.view.webTabWindowId) const wd = webTabWindows.get(item.view.webTabWindowId)
if (wd && wd.window) { if (wd && wd.window) {