electron拦截webview中的url跳转

2021-11-26阅读(4051)评论(0)牵着狗狗看MM

苏州实时公交查询

在主进程中,通过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)
        }
    })
})

 

赞(0)
转载请注明来源:Web前端(W3Cways.com) - Web前端学习之路 » electron拦截webview中的url跳转
分享到: 更多 (0)