10

I am using both Firefox and Google Chrome with multiple windows (profiles).

When clicking a link in e.g. a terminal or another GUI app I'd like to choose which browser/window to load the URL in instead of running the default browser (e.g. Firefox). Does anyone know if such a program exists?

From my (limited) understanding I think it should replace the default browser and show a popup with known browsers and/or active windows where the link should be delegated to.

If found this answer, but it is only looking for existing processes and starts a default one if none is found.

Melle
  • 211
  • 2
  • 7

6 Answers6

11

A simple solution with zenity

enter image description here

  • create /usr/bin/select-browser
#!/bin/sh

BROWSER=$(zenity --list --radiolist --text '' --column='check' --column=browser --title='select your browser' TRUE "chromium" FALSE "firefox" FALSE "google-chrome-stable")
$BROWSER $* &

To configure OS (Manjaro for me):

  • create .local/share/applications/select-browser.desktop
[Desktop Entry]
Version=1.1
Type=Application
Name=Select browser
GenericName=Navigateur Web
Comment=Accéder à Internet
Icon=google-chrome
Exec=/usr/bin/select-browser %U
Actions=new-window;new-private-window;NewShortcut;NewShortcut1;
MimeType=text/html;text/xml;application/xhtml_xml;image/webp;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;
Categories=Network;WebBrowser;
StartupNotify=true
StartupWMClass=select-browser
  • modify .local/share/applications/defaults.list
  • run xdg-settings set default-web-browser select-browser.desktop
anthony.perron
  • 111
  • 1
  • 3
  • This one worked. Here is a bit more handy zenity UI BROWSER=$(zenity --info --title="Open With" --text=\"$1\" --ok-label="Cancel" --extra-button="google-chrome-stable" --extra-button="firefox" --extra-button="chromium") – EdgarK Mar 13 '23 at 15:04
7

You can use the browser environment variable:

export BROWSER=firefox
or
export BROWSER=/path/to/browser

Doing this changes the default browser to use from within your user session.

You could write a script that asks you what browser to open and then set it to your default browser. Then anytime you click a link it will ask you.

Just set the $1 argument so it is sent to the browser you chose, so it opens that link.

Could look something like:

#!/bin/bash
website=$1
echo "What browser do you want to use?  chrome firefox "
read browsera
$browsera $website

You could make it as fancy as you want. Make it so it catches invalid arguments and all sorts of stuff. You could even make it parse the input and depending on what the site is open a predetermined browser.

Michael Prokopec
  • 2,202
  • 7
  • 21
  • This basically outlines what I want. I need to build something which pops up a list of active and installed browsers and let's me redirect the url open action to the browser of my choosing. Put this on my fun-things-to-hack-on list. – Melle Jan 25 '19 at 21:38
  • See my answer below, thanks for your help Michael! – Melle Apr 26 '19 at 10:18
  • Expanding on this, take a look at: https://www.reddit.com/r/linux/comments/2lgokr/comment/clulfml/ – Masood Khaari Dec 22 '21 at 15:05
2

I stumbled upon this question a few years ago and saw there is no ready-made solution. So I made one. It has some extra features, like showing Chrome profiles, Firefox profiles, Firefox containers. And some rules (currently no UI for configuring those) in case you want to automatically open some browser for specific web sites.

It detects all browsers/profiles by itself. Zero-configuration needed. You can download it at https://browsers.software/ or https://github.com/Browsers-software/browsers (there's no difference, website links to github release).

There are no linux packages at this moment, so there's a tiny install script which installs it for your user only.

Madis
  • 121
  • 1
  • This is pretty good. There are some issues like the GUI being too tiny, and multiple profiles of Firefox not being detected. But otherwise this is the best solution for Linux Desktop environments – madacoda Jul 27 '23 at 01:23
1

You can use xdg-mime to set the default application to open URLs, without changing the default browser (xdg-settings is used to set the default browser):

Get the default application (in your case firefox):

xdg-mime query default x-scheme-handler/http
xdg-mime query default x-scheme-handler/https

To set google-chrome a default application use the following comand:

xdg-mime default google-chrome.desktop x-scheme-handler/http
xdg-mime default google-chrome.desktop x-scheme-handler/https
GAD3R
  • 63,407
  • 31
  • 131
  • 192
  • I'd like to do this per URL I click on / I'm redirected to. Setting the default scheme handlers would not work in this case I think. – Melle Dec 11 '18 at 20:37
1

Serving my own need, I hacked the following script together. It uses xdotool to get a list of running browsers (fixed to Firefox + Chrome right now). It displays the results in a list and allows you to pick the corresponding browser. It switches to the desktop (I'm using i3wm), activates the browser window and types the URL. This is definitely not the prettiest code... But it works :)

#!/usr/bin/env python3

import sys
import tkinter
import subprocess

URL = sys.argv[1] if len(sys.argv) > 1 else None
SEARCH_STRING = 'Mozilla Firefox|Google Chrome'

def get_options():
    cmd = ['xdotool','search','--name',SEARCH_STRING]
    result = subprocess.run(cmd, stdout=subprocess.PIPE)
    window_ids = result.stdout.decode('utf-8').rstrip().split("\n")

    options = []
    for id in window_ids:
        cmd = ['xdotool','getwindowname', id]
        result = subprocess.run(cmd, stdout=subprocess.PIPE)
        title = result.stdout.decode('utf-8').rstrip()
        options.append((title, id))

    return options

def kill_window(event = None):
    root.destroy()

def select_prev_option(event):
    val = curr_var.get()
    idx = [i for i, option in enumerate(OPTIONS) if option[1] == val][0]
    if idx > 0:
        curr_var.set(OPTIONS[idx-1][1])

def select_next_option(event):
    val = curr_var.get()
    idx = [i for i, option in enumerate(OPTIONS) if option[1] == val][0]
    if idx < len(OPTIONS)-1:
        curr_var.set(OPTIONS[idx+1][1])

def execute_option(e = None):
    window_id = curr_var.get()

    cmd = ['xdotool', 'get_desktop']
    result = subprocess.run(cmd, stdout=subprocess.PIPE)
    current_desktop = int(result.stdout.decode('utf-8').rstrip())

    cmd = ['xdotool', 'get_desktop_for_window', window_id]
    result = subprocess.run(cmd, stdout=subprocess.PIPE)
    window_desktop = int(result.stdout.decode('utf-8').rstrip())

    if current_desktop != window_desktop:
        cmd = ['xdotool', 'set_desktop', str(window_desktop)]
        result = subprocess.run(cmd, stdout=subprocess.PIPE)

    cmd = [ 'xdotool', 'windowactivate', '--sync', window_id ]
    result = subprocess.run(cmd, stdout=subprocess.PIPE)

    if URL:
        cmd = [
            'xdotool', 'key','--clearmodifiers','--window', window_id, 'ctrl+t',
            'sleep', '.1',
            'type', '--clearmodifiers', URL
        ]
        result = subprocess.run(cmd, stdout=subprocess.PIPE)

        cmd = ['xdotool','key','--clearmodifiers','--window', window_id, 'Return']
        result = subprocess.run(cmd, stdout=subprocess.PIPE)

    kill_window()


root = tkinter.Tk()
root.tk.call('tk', 'scaling', 4.0)
root.attributes('-type', 'dialog')

OPTIONS = get_options()

curr_var = tkinter.StringVar()
curr_var.set(OPTIONS[0][1])

max_len = max([len(t) for t, i in OPTIONS])

for text, mode in OPTIONS:
    b = tkinter.Radiobutton(
        root,
        text=text,
        variable=curr_var,
        value=mode,
        indicatoron=0,
        font=("Arial", 12),
        width=max_len,
        anchor=tkinter.W,
        command=execute_option
    )
    b.pack(anchor=tkinter.W)


root.bind("<j>", select_next_option)
root.bind("<Down>", select_next_option)
root.bind("<k>", select_prev_option)
root.bind("<Up>", select_prev_option)
root.bind("<Return>", execute_option)

root.bind("<Control-q>", kill_window)
root.bind("<Control-w>", kill_window)
root.protocol("WM_DELETE_WINDOW", kill_window)

root.mainloop()

I created a desktop file pointing to this script and set the default browser using:

xdg-settings set default-web-browser browserpicker.desktop
xdg-mime default browserpicker.desktop x-scheme-handler/https
xdg-mime default browser.desktop x-scheme-handler/http
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Melle
  • 211
  • 2
  • 7
0

I am posting this just for reference - in case you ever happen to use Plasma/KDE and still have the same need, or other people do.

Thus, in Plasma, we have the clipboard action list with applications in which to open a file or link. The list should pop up when a certain type of file, or link, is copied.

enter image description here

enter image description here

In some cases that can be a little buggy, so that in Kubuntu 22.10 I had to disable, re-enable, log out, etc, until the list started popping up.

enter image description here

I don't like it to appear for any copied file in the file manager (for which it gives nothing more than the "open with" list already does), so I have disabled that for Dolphin, as said here. On the other hand, as it wasn't appearing in Firefox at all, I have enabled it for Firefox, as said here: it is just a matter of editing an exclusion list in the clipboard settings.

enter image description here

After that, when copying a link, which corresponds to a html document, a list of applications that can open it should appear. To populate the list as desired, just edit the file associations settings for the html MIME type.

enter image description here

cipricus
  • 1,386
  • 13
  • 42