Bin*_*iao 2 navigation events redirect electron
反正有没有阻止或取消电子页面导航?
win.webContents.on('did-start-loading', function(event, url) {
if (event.sender.getURL().startsWith('http://xyz')) {
event.preventDefault();
}
})
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,因为事件处理程序在页面继续导航时执行.
同样,我也想对事件'did-get-redirect-request'做同样的事情,以防止发生某些重定向.
非常感谢
您可以使用该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({})
})
Run Code Online (Sandbox Code Playgroud)
如果要阻止所有重定向,可以添加侦听器webRequest.onBeforeRedirect()并将重定向URL添加到阻止的URL列表中,然后可以在添加到的侦听器中进行检查webRequest.onBeforeRequest().