How can I open links in the default browser via a <webview> in Electron?


#1

I’m trying to wrap an app by using a <webview> tag and it appears that the links from there are not clickable at all. What can I do to make them open in the default browser on click?


#2

I too need a solution to this one.


#3

you can watch an event “new-window” in the webview.
Hope this would help you.

webview.addEventListener('new-window', (e) => {
                  console.log('new window event called');
                const protocol = require('url').parse(e.url).protocol
                if (protocol === 'http:' || protocol === 'https:') {
                    shell.openExternal(e.url)
                }
  });

#4

will this work with iframes too?


#7

This worked for me:

const {shell} = require('electron').remote;
$('webview').each((i,webviewEl)=>{
webviewEl.addEventListener('new-window', (e) => {
                  console.log('new window event called');
                const protocol = require('url').parse(e.url).protocol
                if (protocol === 'http:' || protocol === 'https:') {
                    shell.openExternal(e.url)
                }
  });
});

Thanks.