From e28749e4656a5566cae91bae685ba5d178a927f5 Mon Sep 17 00:00:00 2001 From: MentalBlank Date: Fri, 31 Jan 2025 17:01:06 +1100 Subject: [PATCH] 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( () => {