Firefox, remote commands and Xfce

I replaced the default browser in Xfce (Settings → Preferred Applications) with a custom Python script to do some additional URL handling. To open a link in Firefox you just have to run firefox -remote OpenURL(<url>) and you can even use it with multiple instances (add -P <profile>). Using the script from Terminal worked perfectly fine, but as soon as Xfce handled the click on a link Firefox would say "Firefox is already running, but is not responding." After trying around for a while I ended up diff'ing the environments. Trying to add some of the missing keys to the launcher's env didn't help either. What finally made it work was removing the DESKTOP_STARTUP_ID key from the environment. (Mozilla Bug #478951)

Here's my custom launcher:

#!/usr/bin/env python3

import sys, os
from subprocess import call

    dsi = env['DESKTOP_STARTUP_ID']
    del env['DESKTOP_STARTUP_ID']
    from gi.repository import Gdk
    # get rid of the waiting cursor, requires python3 gobject
except KeyError:
except ImportError:

def exec_new(args):
    if os.fork() == 0:
        os.execvpe(args[0], args, env)

def openurl(url, instance="default"):
    if call(['firefox', '-P', instance, '-remote', 'OpenURL({})'.format(url)], env=env) != 0:
        # no instance running, start new
        exec_new(['firefox', '-P', instance, '-new-instance', url])

for url in sys.argv[1:]:
    if "" in url or "" in url:
        exec_new(["/bin/sh", "-c", "mplayer -fs $(youtube-dl -g '{}')".format(url)])

forking & execing in exec_new is for long-running processes so we're not stuck with a loading cursor forever. It's showing a loading cursor anyway for a while, gotta figure that out. Gdk.notify_startup_complete() fixed that.