Opening a URI in an existing window of an app

Hi, I’m working on an app that logs in via a browser, so I’ll need to capture the return URI, but have that be forwarded to the existing, open window. From my understanding, I need to add the URI scheme to the MIME types and a %u to the Exec field of my desktop file, then that will open a new instance, which needs to forward the data to the existing instance with the window. I’m not quite sure how I’d send that data over, and I can’t find much online. Are there any docs or examples for this? Or is my understanding wrong, and I should do this differently? I am using GJS, if this is relevant.

Honestly, just host a local HTTP server and have the login redirect to localhost:some_port. Just far less hassle and things to go wrong.

You’re almost there, apart from the scheme and %u, you need to pass Gio.ApplicationFlags.HANDLES_OPEN to your Gio.Application#flags. Now you can use the open signal on the application to handle the URI scheme.

As you can see from the docs, the open signal provides files, but that includes opening URI schemes. You need to iterate over the files and call get_uri and handle the URI (check the scheme, parse the path etc.)

Two things to keep in mind are:

  1. the open signal can be called while the app is not running, which will start it, but the activate signal won’t be fired then. You need to check if the app has activated already and activate it yourself inside the open signal.
  2. your app will now also call the open signal when users provide files to it or by manually triggering the URI scheme you registered, so be liberal about it; if you don’t handle files, ignore files that have the file:// scheme (or rather ignore everything but your URI scheme), if the URI is missing the path or doesn’t follow your expected format, also ignore it

I don’t have a gjs example at hand, maybe junction? Junction/src/application.js at main · sonnyp/Junction · GitHub. I have one for Turntable, it’s a bigger commit but focus on the Application.vala changes Head name (i.e.

Thank you! Got this working, modeling after your examples ^^