【问题标题】:Electron opening every link in browser instead of electron itself电子在浏览器中打开每个链接而不是电子本身
【发布时间】:2020-08-13 17:50:40
【问题描述】:

我想在浏览器中而不是在电子中打开一个外部链接。因此我实现了这段代码:

document.addEventListener('click', function(event) {
   if (event.target.tagName === 'A' && event.target.href.startsWith('http')) {
   event.preventDefault();
   shell.openExternal(event.target.href);
 }
});

我现在的问题是,我创建的每个链接都在浏览器中打开。
例如

<a href="http://google.com">foo</a>

<a href="#">bar</a>

我怎样才能让它在浏览器中打开外部链接?

【问题讨论】:

    标签: javascript events electron


    【解决方案1】:

    <a href="#"></a> 实际上是href="http://localhost:X000/#" 所以 if 条件总是为真。 用这个替换你的代码,它应该可以工作:

        const { app, shell, BrowserWindow } = require("electron");
    
        // Create the browser window.
        mainWindow = new BrowserWindow({
          width: 1300,
          height: 800,
        });
    
        // Load your React app or any other HTML which creates your Electron app content
        mainWindow.loadFile("./build/index.html");
    
        mainWindow.webContents.on("new-window", function(event, url) {
          event.preventDefault();
          // This will open a new Electron window to load the url.
          shell.openExternal(url);
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-06
      • 2018-03-11
      • 2020-05-11
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多