Electron: 单击 URL 链接时如何防止在 webview 上导航

创建于 2016-01-22  ·  3评论  ·  资料来源: electron/electron

你好,
我在will-navigate事件(https://github.com/atom/electron/blob/master/docs/api/web-view-tag.md)文档中查看了以下句子。

调用 event.preventDefault() 没有任何效果。

那么我们如何防止 webview 导航到其他页面呢? 我打算在用户单击 webview 上的 URL 链接时启动默认浏览器。
这是我关于渲染器进程的代码。

 webview.addEventListener('will-navigate', function(e) {
        e.preventDefault();
        require('electron').shell.openExternal(e.url);
 });

使用此代码,当我单击 webview 上的 URL 链接时,webview 将导航到新页面,并且默认浏览​​器也打开显示新页面。 我希望 webview 停止导航到新页面。

最有用的评论

这对我有用,可以防止通过用户单击链接进行导航并通知应用程序有关请求的 url。 使用预加载并注入:

document.addEventListener('DOMContentLoaded', function () {
  document.addEventListener('click', function (e) {
    e.preventDefault();
    e.stopPropagation();
    setTimeout(function () {
      var path = e.target.href;
      ipcRenderer.sendToHost('element-clicked', path);
    }, 100);
    return false;
  }, true);
});

请注意document.addEventListener('click', fn, true)的第三个参数

参考: https ://stackoverflow.com/questions/6157486/jquery-trap-all-click-events-before-they-happen/6157660#6157660

所有3条评论

#1378 的副本。

我没有看到发布的解决方案?

这对我有用,可以防止通过用户单击链接进行导航并通知应用程序有关请求的 url。 使用预加载并注入:

document.addEventListener('DOMContentLoaded', function () {
  document.addEventListener('click', function (e) {
    e.preventDefault();
    e.stopPropagation();
    setTimeout(function () {
      var path = e.target.href;
      ipcRenderer.sendToHost('element-clicked', path);
    }, 100);
    return false;
  }, true);
});

请注意document.addEventListener('click', fn, true)的第三个参数

参考: https ://stackoverflow.com/questions/6157486/jquery-trap-all-click-events-before-they-happen/6157660#6157660

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

tenry92 picture tenry92  ·  3评论

cniaulin picture cniaulin  ·  3评论

christiangenco picture christiangenco  ·  3评论

reggi picture reggi  ·  3评论

ThorstenHans picture ThorstenHans  ·  3评论