From e28749e4656a5566cae91bae685ba5d178a927f5 Mon Sep 17 00:00:00 2001 From: MentalBlank Date: Fri, 31 Jan 2025 17:01:06 +1100 Subject: [PATCH 1/3] Register client as browser's default magnet handler --- .../add-new-torrent/add-new-torrent.component.ts | 7 +++++++ client/src/app/navbar/navbar.component.html | 6 ++++++ client/src/app/navbar/navbar.component.ts | 16 ++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/client/src/app/add-new-torrent/add-new-torrent.component.ts b/client/src/app/add-new-torrent/add-new-torrent.component.ts index d87829b..fb71bc2 100644 --- a/client/src/app/add-new-torrent/add-new-torrent.component.ts +++ b/client/src/app/add-new-torrent/add-new-torrent.component.ts @@ -3,6 +3,7 @@ import { Router } from '@angular/router'; import { TorrentService } from 'src/app/torrent.service'; import { Torrent, TorrentFileAvailability } from '../models/torrent.model'; import { SettingsService } from '../settings.service'; +import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-add-new-torrent', @@ -48,9 +49,15 @@ export class AddNewTorrentComponent implements OnInit { private router: Router, private torrentService: TorrentService, private settingsService: SettingsService, + private activatedRoute: ActivatedRoute ) {} ngOnInit(): void { + this.activatedRoute.queryParams.subscribe(params => { + if (params['magnet']) { + this.magnetLink = decodeURIComponent(params['magnet']); + } + }); this.settingsService.get().subscribe((settings) => { const providerSetting = settings.first((m) => m.key === 'Provider:Provider'); this.provider = providerSetting.enumValues[providerSetting.value as number]; diff --git a/client/src/app/navbar/navbar.component.html b/client/src/app/navbar/navbar.component.html index 5a92264..67a440f 100644 --- a/client/src/app/navbar/navbar.component.html +++ b/client/src/app/navbar/navbar.component.html @@ -38,6 +38,12 @@ Settings + diff --git a/client/src/app/navbar/navbar.component.ts b/client/src/app/navbar/navbar.component.ts index 92e596b..fb37c6e 100644 --- a/client/src/app/navbar/navbar.component.ts +++ b/client/src/app/navbar/navbar.component.ts @@ -57,6 +57,22 @@ export class NavbarComponent implements OnInit { }); } + public registerMagnetHandler(): void { + if (window.location.protocol !== "https:") { + alert("Magnet link registration requires a secure connection. Please ensure your site 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."); + } + } + public logout(): void { this.authService.logout().subscribe( () => { From 94e9a8ccd08fc5e8c7220af2f2d4639560a7d739 Mon Sep 17 00:00:00 2001 From: MentalBlank Date: Fri, 2 May 2025 18:08:33 +1000 Subject: [PATCH 2/3] Move magnet handler registration Moved magnet handler registration to general settings. --- client/src/app/navbar/navbar.component.html | 8 +------- client/src/app/navbar/navbar.component.ts | 16 ---------------- client/src/app/settings/settings.component.html | 10 ++++++++++ client/src/app/settings/settings.component.ts | 16 ++++++++++++++++ 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/client/src/app/navbar/navbar.component.html b/client/src/app/navbar/navbar.component.html index 67a440f..ebeeb4a 100644 --- a/client/src/app/navbar/navbar.component.html +++ b/client/src/app/navbar/navbar.component.html @@ -37,13 +37,7 @@ Settings - - + diff --git a/client/src/app/navbar/navbar.component.ts b/client/src/app/navbar/navbar.component.ts index fb37c6e..92e596b 100644 --- a/client/src/app/navbar/navbar.component.ts +++ b/client/src/app/navbar/navbar.component.ts @@ -57,22 +57,6 @@ export class NavbarComponent implements OnInit { }); } - public registerMagnetHandler(): void { - if (window.location.protocol !== "https:") { - alert("Magnet link registration requires a secure connection. Please ensure your site 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."); - } - } - public logout(): void { this.authService.logout().subscribe( () => { diff --git a/client/src/app/settings/settings.component.html b/client/src/app/settings/settings.component.html index e10d21b..287c23c 100644 --- a/client/src/app/settings/settings.component.html +++ b/client/src/app/settings/settings.component.html @@ -141,6 +141,16 @@ +
+ +
+ +
+

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.

+
+
- -
- -
-

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."); } } }