gpt4 book ai didi

redirect - Electron:阻止/取消页面导航

转载 作者:行者123 更新时间:2023-12-03 12:21:07 24 4
gpt4 key购买 nike

有没有办法阻止或取消 Electron 页面导航?

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

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

同样,我也想对事件“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({})
})

如果你想阻止所有重定向,你可以添加一个监听器到 webRequest.onBeforeRedirect() 并将重定向 URL 添加到阻止 URL 列表中,然后您可以检查添加到 webRequest.onBeforeRequest() 的监听器。 .

关于redirect - Electron:阻止/取消页面导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41068295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com