openfl / lime

A foundational Haxe framework for cross-platform development
https://lime.openfl.org/
MIT License
760 stars 375 forks source link

Open links using external browser (Electron) #1426

Open hoangdung-qbt opened 4 years ago

hoangdung-qbt commented 4 years ago

Could we add these lines of code to Electron template in order to force the external links to be opened in default browser (not using built-in webview). I did a small test and it worked really well!

ElectronSetup.window.webContents.on('new-window', function(e, url) {
    e.preventDefault();
    require('electron').shell.openExternal(url);
});
ElectronSetup.window.loadURL("file://" + __dirname + "/index.html");    
player-03 commented 4 years ago

Submit a pull request, and someone will take a look.

It would help if you could include a list of pros and cons for the built-in webview. Do people use it often? For people who want to use it, is there another option that lets them continue to use it, and how easy is it to use? (For instance, maybe the answer is to stick to internal links if you don't want to leave the built-in webview. Simple solution, but would it cover all use cases?) The more concerns you address, the easier it'll be to process your pull request.

If you think developers would want both options, consider adding a compiler flag that toggles this block of code.

::if DEFINE_ELECTRON_USE_EXTERNAL_BROWSER::
ElectronSetup.window.webContents.on('new-window', function(e, url) {
    e.preventDefault();
    require('electron').shell.openExternal(url);
});
ElectronSetup.window.loadURL("file://" + __dirname + "/index.html");
::end::

This corresponds to <haxedef name="electronUseExternalBrowser" /> in project.xml. Bit wordy, but at least it's unambiguous.