Launch a webpage from my application on Linux

I have an application that launches a webpage in the current browser when the user selects it. This part of my application works fine in the Windows version, but I can’t figure out how to do this in a Linux build.

Currently, the Linux version is hard-coded for Firefox in a specific directory and each time it launches a new instance, and does not display the URL that I am passing. I would like it to NOT launch a new version every time, but just open a new page in the current open if it is already running.

For windows, I use:

ShellExecute(NULL,"open",filename,NULL,NULL,SW_SHOWNORMAL); 

I am currently using Linux:

 pid_t pid; char *args[2]; char *prog=0; char firefox[]={"/usr/bin/firefox"}; if(strstri(filename,".html")) prog=firefox; if(prog) { args[0]=(char *)filename; args[1]=0; pid=fork(); if(!pid) execvp(prog,args); } 
+4
source share
4 answers

If you are writing this for modern distributions, you can use xdg-open :

 $ xdg-open http://google.com/ 

If you are using an older version, you will have to use a command on the desktop, for example gnome-open or exo-open .

+6
source

xdg-open is a new standard, and you should use it whenever possible. However, if the distribution is more than a few years old, it may be absent, and alternative mechanisms include $ BROWSER (an older sample standard), gnome-open (Gnome), kfmclient exec (KDE), exo-open (Xfce), or mailcap (the text / html handler will most likely be a browser).

However, most applications do not care about such a lot of work - if they are created for a specific environment, they use the mechanisms for launching the environment. For example, Gnome has gnome_url_show, KDE has KRun, most terminal programs (like mutt) handle mail, etc. Hard browser encoding and permission to distribute, or the user to override the default is also normal.

I do not suggest hardcoding this, but if you really want to open a new tab in Firefox, you can use the "firefox -new-tab $ URL".

+2
source

Note for xdg-open: check http://portland.freedesktop.org/wiki/ , section "Using Xdg-utils"; it says that you can include xdg-open script in your application and use it as a backup if the target system is not already installed xdg-open.

+1
source

If you do not want to attract additional applications, just use the built-in firefox remote control commands. For instance:

 firefox -remote 'openurl(http://stackoverflow.com)' 

Detailed usage description http://www.mozilla.org/unix/remote.html

0
source

Source: https://habr.com/ru/post/1277343/


All Articles