docs: navigation history example (#42980)
* docs: add fiddle example * docs: add tutorial guide * refactor: PR review comments * refactor: add eof * refactor: render navigation history and make demo better. * refactor: fix broken links * refactor: add eof newline * docs: review feedback :) * chore: lint add space around list * doc: transformURL
This commit is contained in:
parent
517d04de16
commit
ceea1225a2
7 changed files with 321 additions and 0 deletions
58
docs/fiddles/features/navigation-history/main.js
Normal file
58
docs/fiddles/features/navigation-history/main.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
const { app, BrowserWindow, BrowserView, ipcMain } = require('electron')
|
||||
const path = require('path')
|
||||
|
||||
function createWindow () {
|
||||
const mainWindow = new BrowserWindow({
|
||||
width: 1000,
|
||||
height: 800,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true
|
||||
}
|
||||
})
|
||||
|
||||
mainWindow.loadFile('index.html')
|
||||
|
||||
const view = new BrowserView()
|
||||
mainWindow.setBrowserView(view)
|
||||
view.setBounds({ x: 0, y: 100, width: 1000, height: 800 })
|
||||
view.setAutoResize({ width: true, height: true })
|
||||
|
||||
const navigationHistory = view.webContents.navigationHistory
|
||||
ipcMain.handle('nav:back', () =>
|
||||
navigationHistory.goBack()
|
||||
)
|
||||
|
||||
ipcMain.handle('nav:forward', () => {
|
||||
navigationHistory.goForward()
|
||||
})
|
||||
|
||||
ipcMain.handle('nav:canGoBack', () => navigationHistory.canGoBack())
|
||||
ipcMain.handle('nav:canGoForward', () => navigationHistory.canGoForward())
|
||||
ipcMain.handle('nav:loadURL', (_, url) =>
|
||||
view.webContents.loadURL(url)
|
||||
)
|
||||
ipcMain.handle('nav:getCurrentURL', () => view.webContents.getURL())
|
||||
ipcMain.handle('nav:getHistory', () => {
|
||||
return navigationHistory.getAllEntries()
|
||||
})
|
||||
|
||||
view.webContents.on('did-navigate', () => {
|
||||
mainWindow.webContents.send('nav:updated')
|
||||
})
|
||||
|
||||
view.webContents.on('did-navigate-in-page', () => {
|
||||
mainWindow.webContents.send('nav:updated')
|
||||
})
|
||||
}
|
||||
|
||||
app.whenReady().then(createWindow)
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') app.quit()
|
||||
})
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue