How can I force external links from browser-window to open in a default browser from Electron?

后端 未结 9 1011
猫巷女王i
猫巷女王i 2020-12-13 06:10

I\'m using the BrowserWindow to display an app and I would like to force the external links to be opened in the default browser. Is that even possible or I have to approach

9条回答
  •  不知归路
    2020-12-13 06:25

    Check whether the requested url is an external link. If yes then use shell.openExternal.

    mainWindow.webContents.on('will-navigate', function(e, reqUrl) {
      let getHost = url=>require('url').parse(url).host;
      let reqHost = getHost(reqUrl);
      let isExternal = reqHost && reqHost != getHost(wc.getURL());
      if(isExternal) {
        e.preventDefault();
        electron.shell.openExternal(reqUrl);
      }
    }
    

提交回复
热议问题