2016-12-09 55 views
0

反正有防止或取消电子页面导航吗?电子:防止/取消页面导航

win.webContents.on('did-start-loading', function(event, url) { 
    if (event.sender.getURL().startsWith('http://xyz')) { 
     event.preventDefault(); 
    } 
}) 

上面的代码不起作用,因为事件处理程序在页面继续导航时被执行。

同样,我也想为事件'did-get-redirect-request'做同样的事情,以防止发生某些重定向。

非常感谢

回答

1

可以使用will-navigate情况下,停止导航。

您也可以使用webRequest.onBeforeRequest()防止请求(包括重定向):

const {session} = require('electron') 
const ses = session.defaultSession 
const urlToBlock = 'whatever' 
ses.webRequest.onBeforeRequest((details, callback) => { 
if (details.url === urlToBlock) // cancel the request 
    callback({ cancel: true }) 
else // let the request happen 
    callback({}) 
}) 

如果你想阻止所有重定向,你可以添加一个侦听webRequest.onBeforeRedirect()和重定向URL添加到阻止的URL列表,你然后可以检入您添加到webRequest.onBeforeRequest()的听众。