Merge pull request #806 from mentalblank/register-as-default-magnet-handler

Register client as browser's default magnet handler
This commit is contained in:
Cucumberrbob 2025-05-03 17:16:43 +01:00 committed by GitHub
commit 9319f099fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 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

@ -141,6 +141,28 @@
</div>
</div>
<div class="field" *ngIf="activeTab === 0">
<label class="label">Register client as magnet link handler</label>
<div class="control">
<button
class="button is-info"
type="button"
(click)="registerMagnetHandler()"
[disabled]="!canRegisterMagnetHandler">
<span>
{{ canRegisterMagnetHandler ? 'Register Handler' : 'Unavailable in This Browser' }}
</span>
</button>
</div>
<p class="help">
{{
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."
}}
</p>
</div>
<div class="field" *ngIf="activeTab < 99">
<div class="control">
<button class="button is-success" (click)="ok()" [disabled]="saving" [ngClass]="{ 'is-loading': saving }">

View file

@ -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,4 +142,13 @@ export class SettingsComponent implements OnInit {
},
);
}
public registerMagnetHandler(): void {
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.");
}
}
}