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.
This commit is contained in:
MentalBlank 2025-05-04 01:41:37 +10:00
parent 94e9a8ccd0
commit fed6d022f8
3 changed files with 29 additions and 21 deletions

View file

@ -142,13 +142,25 @@
</div> </div>
<div class="field" *ngIf="activeTab === 0"> <div class="field" *ngIf="activeTab === 0">
<label class="label">Allow client to handle magnet links</label> <label class="label">Register client as magnet link handler</label>
<div class="control"> <div class="control">
<button class="button is-info" type="button" (click)="registerMagnetHandler()"> <button
<span>Register Handler</span> class="button is-info"
</button> type="button"
</div> (click)="registerMagnetHandler()"
<p class="help">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.</p> [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>
<div class="field" *ngIf="activeTab < 99"> <div class="field" *ngIf="activeTab < 99">

View file

@ -28,10 +28,13 @@ export class SettingsComponent implements OnInit {
public testAria2cConnectionError: string = null; public testAria2cConnectionError: string = null;
public testAria2cConnectionSuccess: string = null; public testAria2cConnectionSuccess: string = null;
public canRegisterMagnetHandler = false;
constructor(private settingsService: SettingsService) {} constructor(private settingsService: SettingsService) {}
ngOnInit(): void { ngOnInit(): void {
this.reset(); this.reset();
this.canRegisterMagnetHandler = !!(window.isSecureContext && 'registerProtocolHandler' in navigator);
} }
public reset(): void { public reset(): void {
@ -141,18 +144,11 @@ export class SettingsComponent implements OnInit {
} }
public registerMagnetHandler(): void { public registerMagnetHandler(): void {
if (window.location.protocol !== "https:") { try {
alert("Magnet link handler registration requires a secure connection. Please ensure the client is being served over HTTPS to enable this feature."); navigator.registerProtocolHandler("magnet", `${window.location.origin}/add?magnet=%s`);
return; 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.");
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.");
} }
} }
} }