TL;DR
Want your browser to open specific apps or perform actions when you click a special link? This guide shows you how to set up custom protocol handlers in Chrome, Firefox, and Edge. It’s useful for things like opening email clients, messaging apps, or even running local scripts.
Setting Up Custom Protocol Handlers
- Understand Protocol Handlers: A protocol handler tells your browser what to do when it encounters a link starting with something other than
http://orhttps://. For example,mailto:opens your email client. We’ll create our own. - Choose Your Protocol: Pick a unique prefix for your protocol (e.g.,
my-app://,customprotocol://). Avoid common names to prevent conflicts. - Create the Handler Application/Script: This is what will actually *do* something when the protocol is called. It could be a desktop application, a Python script, or anything else that can handle command-line arguments.
- Example (Python): A simple Python script to display a message:
import sys if __name__ == '__main__': if len(sys.argv) > 1: message = ' '.join(sys.argv[1:]) print(f'Handling custom protocol with message: {message}') else: print('No message provided.')
- Example (Python): A simple Python script to display a message:
- Register the Protocol Handler (Chrome/Edge): Chrome and Edge use a similar system. You can do this through the registry or via command line.
- Registry Method (Windows): This is more permanent but requires editing the Windows Registry (be careful!).
- Open Registry Editor (
regedit). - Navigate to
HKEY_CURRENT_USERSoftwareClasses. - Create a new key with your protocol name (e.g.,
my-app). - Within that key, create a subkey named
shell. - Within
shell, create a subkey namedopen. - Within
open, create a subkey namedcommand. - Set the default value of the
commandkey to the path of your application/script, including any arguments. For example:"C:Python39python.exe" "C:pathtoyourscript.py" "%1"
- Open Registry Editor (
- Command Line Method (Chrome/Edge): This is temporary and useful for testing.
chrome://flags/#enable-command-line-protocol-handlerEnable this flag, then restart Chrome. You can also use the command line when launching chrome:
chrome.exe --register-protocol-handler=my-app,YourHandlerName,C:pathtoyourscript.py
- Registry Method (Windows): This is more permanent but requires editing the Windows Registry (be careful!).
- Register the Protocol Handler (Firefox): Firefox uses a different approach.
- Create a
.desktopfile in your Firefox profile directory’s extensions folder. The path is typically something like:~/.mozilla/firefox/{profile_id}/extensions/your-extension-name/protocol-handler.desktop - The contents of the
.desktopfile should look similar to this (replace placeholders):[Desktop Entry] Type=Application Name=Your Handler Name Exec="C:Python39python.exe" "C:pathtoyourscript.py" %u MimeType=x-scheme-handler/my-app;name=My App Protocol
- Create a
- Test Your Handler: Open a new tab and type your protocol URL (e.g.,
my-app://hello world) into the address bar and press Enter. If everything is set up correctly, your application/script should run. - Troubleshooting:
- Permissions: Make sure your script has execute permissions.
- Pathing: Double-check that the path to your application/script in the registry or
.desktopfile is correct. - Quotes: Use quotes around paths with spaces.
- Restart Browser: Always restart your browser after making changes.