New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I open a url from <a> on default OS browser? #1344
Comments
On my setup, I tried both I ended up using |
On Linux, you can use xdg-open: |
I have actually decided to use node-open. You should give it a try if you don't want to mess with escaping and such. |
Guys, but your solutions are great, but I'm wondering how to intercept attempt to open url like "http://someurl.com" and then open in with |
Same question here as @havenchyk, is there a way to tell electron to open links external by default? |
If your app only uses one window, and you can guarantee that every external link in your app opens in a new window (via eg.
Where It'd be nice if there was an event fired when any link is clicked, so the app could decide if it should open in the browser, but I haven't found such an event if it exists. |
I found this code snippet on S.O.:
Dropped it in my main index file, it seems to be working as far as I can tell, even for dynamically generated links. I'm too noob at electron to know if there are any drawbacks to this I should watch out for. Thoughts? |
I'm using this piece of code: var handleRedirect = (e, url) => {
if(url != webContents.getURL()) {
e.preventDefault()
require('electron').shell.openExternal(url)
}
}
webContents.on('will-navigate', handleRedirect)
webContents.on('new-window', handleRedirect) |
First declare a spawn proccess `app.controller('GuideCtrl', ['$scope', ($scope)=>{
}])` and before call the method
|
Does |
Base on the code from @rubencodes , i used :
Then you just have to drop the 'open-in-browser' class to each elements you want to open in the browser. |
Here's one that doesn't require JQuery, in case anyone else is hunting for it. It will automatically open any link that starts with 'http' in the external browser. Put this in your renderer process: // Open all links in external browser
let shell = require('electron').shell
document.addEventListener('click', function (event) {
if (event.target.tagName === 'A' && event.target.href.startsWith('http')) {
event.preventDefault()
shell.openExternal(event.target.href)
}
}) |
If you want ALL
This assumes your have a single page app like me. If not, you'll need to do some extra sanitizing. |
I just want to reiterate that using this with user content without a whitelist is probably a gaping security hole. I don't know what all various schemes do and what their inputs are but just as a simple example a link like this
Will prompt the user in Chrome and Safari on macOS but with the code above Electron will just open the app. It almost feels like Electron itself should default to being more secure here rather than leave it to every individual programmer to figure out how to make it secure on their own. At a minimum you probably want something like
|
@greggman See the open external permission type in https://electronjs.org/docs/api/session#sessetpermissionrequesthandlerhandler You can block those 👍 |
Hi, I am using vue.js and solve this problem inspired by above discussion, in case some one like me using vue also have the same problem, I paste my code here.
|
Based on comment of @alangrainger - ts version:
|
What about data URLs? |
Angular 7 version (with live reloads):
The |
All the methods work fine but only on non root electron app, what can I use to open a external url on default OS browser on a root elevated process? |
Just wanted to post an answer for other Vue.js users.
|
Thank you. |
On
|
@travis5491811 This opens up another Electron window with the right page. Is this the expected behavior? |
No, when implemented properly, my post should answer the thread ticket "How do I open a url from on default OS browser", but for
|
Apparently this is not that trivial due to operating system differences, so I opted for using the `open` library. Details here: electron/electron#1344
Is there any way to set watcher for shell.openExternal(URL); I want to set watcher for opened page's specific URL and when I got that URL Need to close the browser window or tab and return? |
appWindow.webContents.on('new-window', evt) is now deprecated and setWindowOpenHandler should be used. how can i prevent defaults like i used to with new-window? |
https://www.electronjs.org/docs/api/window-open#browserwindowproxy-example |
My code snippet clue to deal with it accordingly to my Electron version ^12.0.0
|
|
To expand on @hanayashiki's answer, here is what was found after some google searching: https://benjamin-altpeter.de/shell-openexternal-dangers/ Given the above, @hanazuki would you say that the following may be appropriate? import url from "url";
app.on('web-contents-created', (event, contents) => {
contents.on('will-navigate', (event, navigationUrl) => {
event.preventDefault();
const parsedUrl = new url.URL(navigationUrl);
if (["https:", "http:", "mailto:"].includes(parsedUrl.protocol)) {
shell.openExternal(navigationUrl);
}
})
}) |
For anyone who stumbles upon this thread, I found the official Electron documentation to not be very clear. This achieved what I was looking for, which is simply to open an external link in the user's default browser and not open an Electron window:
|
@Canuckaholic Did you put that in your createWindow function? I have had no luck implementing any of the fixes above. Spent days on this now... |
In your main process after creating a const { BrowserWindow, shell } = require('electron');
const win = new BrowserWindow({ /* options */ });
win.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('https:')) {
shell.openExternal(url);
}
return { action: 'deny' };
}); |
i tried this BUT i doesen't work beacause my link is a div let me show you:
My html : |
I want to take an to an external website, but I do not want it to happen inside atom-shell, I want it to be in the default browser. How can I do that?
Thanks!
The text was updated successfully, but these errors were encountered: