Register client as browser's default magnet handler

This commit is contained in:
MentalBlank 2025-01-31 17:01:06 +11:00
parent 757d112d01
commit e28749e465
3 changed files with 29 additions and 0 deletions

View file

@ -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];

View file

@ -38,6 +38,12 @@
</span>
<span>Settings</span>
</a>
<button class="navbar-item" type="button" (click)="registerMagnetHandler()">
<span class="icon">
<i class="fas fa-magnet" aria-hidden="true"></i>
</span>
<span>Register Magnet Handler</span>
</button>
<a class="navbar-item" *ngIf="profile" href="{{ providerLink }}" target="_blank" rel="noopener">
<span class="icon">
<i class="fas fa-euro-sign" aria-hidden="true"></i>

View file

@ -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(
() => {