在主进程中,通过webContents的did-attach-webview
事件监听webview是否加载,再通过webview的webContent去监听will-navigate
事件达到拦截的效果
import { BrowserWindow, shell } from 'electron' const win = new BrowserWindow({ width: 800, height: 600 }) win.webContents.on('did-attach-webview', (event, webContent) => { webContent.on('will-navigate', (e, url) => { //这里可以根据具体业务,判断是否需要拦截webview中的链接跳转 e.preventDefault() const protocol = require('url').parse(url).protocol if (protocol === 'http:' || protocol === 'https:') { shell.openExternal(url) } }) })