1 minute read

I use many browsers and many profiles in one browser. I would like to open Project 1 related in browser A, other work to B, memes to C etc. But

There can be only one

So what do? Well simple enough, make a script that relays the openings to different browsers.

#!/usr/bin/env python3
# Link-Relay: Open some links in different browsers. Set the Browser Relay as your default browser.
import os
import re
import sys


def get_browser(link):
    mappings = {
        "work": {
            "browser": "google-chrome --profile-directory=Default",
            "urls": ["work", "docs\.google", "forms", "drive"]
        },
        "customer": {
            "browser": "google-chrome --profile-directory='Profile 1'",
            "urls": ["sap", "outlook", "portal", "github", "microsoftonline", "sharepoint", "customer"]
        }
    }
    default = "firefox"
    for (k, v) in mappings.items():
        for url in v["urls"]:
            if re.search(url, link):
                return v["browser"]
    return default


if __name__ == '__main__':
    link = sys.argv[1]
    app = get_browser(link)
    os.system("%s '%s'" % (app, " ".join(sys.argv[1:])))

Save this script to /usr/local/bin/link-relay.py (or wherever you want to store these) and make it executable. Update the Exec path below if you do.

Tha mappings variable contains urls that relay to which browser. Here, all google stuff to Chrome Default browser, MS related to customer profile, and everything else to Firefox.

Then you need a desktop file for the browser to register is at your default: So create ~/.local/share/applications/browser-relay.desktop:

[Desktop Entry]
Name=Browser Relay
Comment=Sends some applications to different browsers
Keywords=browser;
Type=Application
Exec=/usr/local/bin/link-relay.py %u
Terminal=false
Categories=Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml+xml;application/xml;application/rss+xml;application/rdf+xml;image/gif;image/jpeg;image/png;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;x-scheme-handler/chrome;video/webm;application/x-xpinstall;

Then run update-desktop-database, set “Browser Relay” as your default browser and your done! Simply updating the mappings table will update the configuration immediately.

Tagged under: linux

Updated: