3

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.

4

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.

  • 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 at 21:38
  • See my answer below, thanks for your help Michael! – Melle Apr 26 at 10:18
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
  • 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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.