Make URL schemes dialog more ergonomic (#4002)

(originally merged into a PR branch)

* Make URL schemes dialog more ergonomic

* add name to contributors list

* Title Case

* Tweak build instructions so Cursor picks them up

* Use a warning icon for the URL scheme pop-up

* Default to cancelling

(cherry picked from commit f96c8c2ac8)
This commit is contained in:
Adnane Taghi 2025-05-15 07:52:39 +02:00 committed by Damien Elmes
parent 172a4e9863
commit f161c9ce01
6 changed files with 32 additions and 8 deletions

View file

@ -0,0 +1,2 @@
- To build and check the project, use ./check in the root folder (or check.bat on Windows)
- This will format files, then run lints and unit tests.

View file

@ -1,2 +0,0 @@
- To build and check the project, use ./check(.bat)
- This will format files, then run lints and unit tests.

View file

@ -227,6 +227,7 @@ JL710
Matt Brubeck <mbrubeck@limpet.net>
Yaoliang Chen <yaoliang.ch@gmail.com>
KolbyML <https://github.com/KolbyML>
Adnane Taghi <dev@soleuniverse.me>
********************

View file

@ -89,6 +89,8 @@ preferences-url-scheme-prompt = Allowed { preferences-url-schemes } (space-separ
preferences-url-scheme-warning = Blocked attempt to open `{ $link }`, which may be a security issue.
If you trust the deck author and wish to proceed, you can add `{ $scheme }` to your allowed { preferences-url-schemes }.
preferences-url-scheme-allow-once = Allow Once
preferences-url-scheme-always-allow = Always Allow
## NO NEED TO TRANSLATE. This text is no longer used by Anki, and will be removed in the future.

View file

@ -742,3 +742,11 @@ create table if not exists profiles
def set_allowed_url_schemes(self, schemes: list[str]) -> None:
self.profile["allowedUrlSchemes"] = schemes
def always_allow_scheme(self, scheme: str) -> None:
schemes = self.allowed_url_schemes()
if scheme not in schemes:
schemes.append(scheme)
self.set_allowed_url_schemes(schemes)

View file

@ -5,8 +5,8 @@ from __future__ import annotations
from markdown import markdown
from aqt.qt import Qt, QUrl
from aqt.utils import ask_user_dialog, getText, openLink, tr
from aqt.qt import QMessageBox, Qt, QUrl
from aqt.utils import MessageBox, getText, openLink, tr
def show_url_schemes_dialog() -> None:
@ -32,6 +32,13 @@ def is_supported_scheme(url: QUrl) -> bool:
return scheme in allowed_schemes or scheme in ["http", "https"]
def always_allow_scheme(url: QUrl) -> None:
from aqt import mw
scheme = url.scheme().lower()
mw.pm.always_allow_scheme(scheme)
def open_url_if_supported_scheme(url: QUrl) -> None:
from aqt import mw
@ -41,18 +48,24 @@ def open_url_if_supported_scheme(url: QUrl) -> None:
def on_button(idx: int) -> None:
if idx == 0:
show_url_schemes_dialog()
openLink(url)
elif idx == 1:
always_allow_scheme(url)
openLink(url)
msg = markdown(
tr.preferences_url_scheme_warning(link=url.toString(), scheme=url.scheme())
)
ask_user_dialog(
MessageBox(
msg,
buttons=[
tr.actions_with_ellipsis(action=tr.preferences_url_schemes()),
tr.actions_close(),
tr.preferences_url_scheme_allow_once(),
tr.preferences_url_scheme_always_allow(),
(tr.actions_cancel(), QMessageBox.ButtonRole.RejectRole),
],
parent=mw,
callback=on_button,
textFormat=Qt.TextFormat.RichText,
default_button=2,
icon=QMessageBox.Icon.Warning,
)