From fed6d022f8352edb2af91be43b00dcd91a75e936 Mon Sep 17 00:00:00 2001 From: MentalBlank Date: Sun, 4 May 2025 01:41:37 +1000 Subject: [PATCH] Improve magnet handler registration - Removed HTTPS protocol check in `registerMagnetHandler()`. Now using `this.canRegisterMagnetHandler = !!(window.isSecureContext && 'registerProtocolHandler' in navigator);` to determine support for protocol handler registration in a secure context. - Updated the registration button and help text to respond dynamically based on `canRegisterMagnetHandler` and provide better user experience and messaging when unsupported. - Wrapped `navigator.registerProtocolHandler()` in a try catch block to handle registration failures gracefully and avoid unhandled exceptions. --- client/src/app/navbar/navbar.component.html | 2 +- .../src/app/settings/settings.component.html | 26 ++++++++++++++----- client/src/app/settings/settings.component.ts | 22 +++++++--------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/client/src/app/navbar/navbar.component.html b/client/src/app/navbar/navbar.component.html index ebeeb4a..5a92264 100644 --- a/client/src/app/navbar/navbar.component.html +++ b/client/src/app/navbar/navbar.component.html @@ -37,7 +37,7 @@ Settings - + diff --git a/client/src/app/settings/settings.component.html b/client/src/app/settings/settings.component.html index 287c23c..7b3c537 100644 --- a/client/src/app/settings/settings.component.html +++ b/client/src/app/settings/settings.component.html @@ -142,13 +142,25 @@
- -
- -
-

This will register the client as your browser's default handler for magnet links. Clicking a magnet link will automatically open it in the new torrent screen for downloading.

+ +
+ +
+

+ {{ + canRegisterMagnetHandler + ? "This will attempt to register the client as your browser's default handler for magnet links and automatically open them in the new torrent screen for downloading." + : "Magnet link registration is unavailable because either your browser does not support it or the client is not being served to you in a secure context." + }} +

diff --git a/client/src/app/settings/settings.component.ts b/client/src/app/settings/settings.component.ts index 4f71f5e..1d8f946 100644 --- a/client/src/app/settings/settings.component.ts +++ b/client/src/app/settings/settings.component.ts @@ -28,10 +28,13 @@ export class SettingsComponent implements OnInit { public testAria2cConnectionError: string = null; public testAria2cConnectionSuccess: string = null; + public canRegisterMagnetHandler = false; + constructor(private settingsService: SettingsService) {} ngOnInit(): void { this.reset(); + this.canRegisterMagnetHandler = !!(window.isSecureContext && 'registerProtocolHandler' in navigator); } public reset(): void { @@ -139,20 +142,13 @@ export class SettingsComponent implements OnInit { }, ); } - + public registerMagnetHandler(): void { - if (window.location.protocol !== "https:") { - alert("Magnet link handler registration requires a secure connection. Please ensure the client is being served over HTTPS to enable this feature."); - return; - } - - const handlerUrl = `${window.location.origin}/add?magnet=%s`; - - if (navigator.registerProtocolHandler) { - navigator.registerProtocolHandler("magnet", handlerUrl); - alert("Your browser will display a prompt asking if you'd like to add the client as the default application for magnet links. Please confirm to complete the setup."); - } else { - alert("Magnet link registration failed. Your browser does not support registering custom protocol handlers."); + try { + navigator.registerProtocolHandler("magnet", `${window.location.origin}/add?magnet=%s`); + alert("Success! Your browser will now prompt you to confirm and add the client as the default handler for magnet links."); + } catch (error) { + alert("Magnet link registration failed."); } } }