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

@ -37,7 +37,7 @@
<i class="fas fa-cog" aria-hidden="true"></i>
</span>
<span>Settings</span>
</a>
</a>
<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

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

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