From 33c5fd31c10b3a2280489c062d3e1b1a55d3b638 Mon Sep 17 00:00:00 2001 From: Saksham Goyal Date: Fri, 15 Nov 2024 22:38:25 -0500 Subject: [PATCH 1/3] LibWebView+QT: Support bangs in the URL bar --- Libraries/LibWebView/SearchEngine.cpp | 39 ++++++++++++++++++--------- Libraries/LibWebView/SearchEngine.h | 2 ++ UI/Qt/LocationEdit.cpp | 17 +++++++++--- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/Libraries/LibWebView/SearchEngine.cpp b/Libraries/LibWebView/SearchEngine.cpp index 13b95be78e6..aea112ee858 100644 --- a/Libraries/LibWebView/SearchEngine.cpp +++ b/Libraries/LibWebView/SearchEngine.cpp @@ -11,19 +11,19 @@ namespace WebView { static constexpr auto builtin_search_engines = Array { - SearchEngine { "Bing"sv, "https://www.bing.com/search?q={}"sv }, - SearchEngine { "Brave"sv, "https://search.brave.com/search?q={}"sv }, - SearchEngine { "DuckDuckGo"sv, "https://duckduckgo.com/?q={}"sv }, - SearchEngine { "Ecosia"sv, "https://ecosia.org/search?q={}"sv }, - SearchEngine { "GitHub"sv, "https://github.com/search?q={}"sv }, - SearchEngine { "Google"sv, "https://www.google.com/search?q={}"sv }, - SearchEngine { "GoogleScholar"sv, "https://scholar.google.com/scholar?q={}"sv }, - SearchEngine { "Kagi"sv, "https://kagi.com/search?q={}"sv }, - SearchEngine { "Mojeek"sv, "https://www.mojeek.com/search?q={}"sv }, - SearchEngine { "Startpage"sv, "https://startpage.com/search?q={}"sv }, - SearchEngine { "Wikipedia"sv, "https://en.wikipedia.org/w/index.php?title=Special:Search&search={}"sv }, - SearchEngine { "Yahoo"sv, "https://search.yahoo.com/search?p={}"sv }, - SearchEngine { "Yandex"sv, "https://yandex.com/search/?text={}"sv }, + SearchEngine { "Bing"sv, "https://www.bing.com/search?q={}"sv, "!b"sv }, + SearchEngine { "Brave"sv, "https://search.brave.com/search?q={}"sv, "!brave"sv }, + SearchEngine { "DuckDuckGo"sv, "https://duckduckgo.com/?q={}"sv, "!ddg"sv }, + SearchEngine { "Ecosia"sv, "https://ecosia.org/search?q={}"sv, "!ec"sv }, + SearchEngine { "GitHub"sv, "https://github.com/search?q={}"sv, "!gh"sv }, + SearchEngine { "Google"sv, "https://www.google.com/search?q={}"sv, "!g"sv }, + SearchEngine { "GoogleScholar"sv, "https://scholar.google.com/scholar?q={}"sv, "!gscholar"sv }, + SearchEngine { "Kagi"sv, "https://kagi.com/search?q={}"sv, "!kagi"sv }, + SearchEngine { "Mojeek"sv, "https://www.mojeek.com/search?q={}"sv, "!mojeek"sv }, + SearchEngine { "Startpage"sv, "https://startpage.com/search?q={}"sv, "!startpage"sv }, + SearchEngine { "Wikipedia"sv, "https://en.wikipedia.org/w/index.php?title=Special:Search&search={}"sv, "!w"sv }, + SearchEngine { "Yahoo"sv, "https://search.yahoo.com/search?p={}"sv, "!y"sv }, + SearchEngine { "Yandex"sv, "https://yandex.com/search/?text={}"sv, "!ya"sv }, }; ReadonlySpan search_engines() @@ -65,6 +65,19 @@ Optional find_search_engine_by_query_url(StringView query_u return *it; } +Optional find_search_engine_by_bang(StringView bang) +{ + auto it = AK::find_if(builtin_search_engines.begin(), builtin_search_engines.end(), + [&](auto const& engine) { + return engine.bang == bang; + }); + + if (it == builtin_search_engines.end()) + return {}; + + return *it; +} + String format_search_query_for_display(StringView query_url, StringView query) { static constexpr auto MAX_SEARCH_STRING_LENGTH = 32; diff --git a/Libraries/LibWebView/SearchEngine.h b/Libraries/LibWebView/SearchEngine.h index c02354c5732..7ddc74a8cd1 100644 --- a/Libraries/LibWebView/SearchEngine.h +++ b/Libraries/LibWebView/SearchEngine.h @@ -14,12 +14,14 @@ namespace WebView { struct SearchEngine { StringView name; StringView query_url; + StringView bang; }; ReadonlySpan search_engines(); SearchEngine const& default_search_engine(); Optional find_search_engine_by_name(StringView name); Optional find_search_engine_by_query_url(StringView query_url); +Optional find_search_engine_by_bang(StringView bang); String format_search_query_for_display(StringView query_url, StringView query); } diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cpp index 2a02051f54c..53b149b5206 100644 --- a/UI/Qt/LocationEdit.cpp +++ b/UI/Qt/LocationEdit.cpp @@ -37,12 +37,21 @@ LocationEdit::LocationEdit(QWidget* parent) clearFocus(); - Optional search_engine_url; - if (Settings::the()->enable_search()) - search_engine_url = Settings::the()->search_engine().query_url; - auto query = ak_string_from_qstring(text()); + Optional search_engine_url; + if (Settings::the()->enable_search()) { + search_engine_url = Settings::the()->search_engine().query_url; + if (query.starts_with('!')) { + auto splits = MUST(query.split(' ')); // FIXME: some missing error handling here. + auto exist = WebView::find_search_engine_by_bang(splits[0]); + if (exist.has_value()) { + search_engine_url = exist->query_url; + query = MUST(query.substring_from_byte_offset(splits[0].bytes().size())); + } + } + } + if (auto url = WebView::sanitize_url(query, search_engine_url); url.has_value()) set_url(url.release_value()); }); From 992c80c355894f3ca9935b96c9dfa95db671b41c Mon Sep 17 00:00:00 2001 From: Saksham Goyal Date: Sun, 17 Nov 2024 17:43:55 -0500 Subject: [PATCH 2/3] LibWebView+QT: Support bangs at the end --- UI/Qt/LocationEdit.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cpp index 53b149b5206..9c689b2eb59 100644 --- a/UI/Qt/LocationEdit.cpp +++ b/UI/Qt/LocationEdit.cpp @@ -50,6 +50,18 @@ LocationEdit::LocationEdit(QWidget* parent) query = MUST(query.substring_from_byte_offset(splits[0].bytes().size())); } } + // FIXME: low quality code. this can be merged into the if branch + auto const temp = query.split(' '); + if (!temp.is_error()) { + auto const last = temp.value().last(); + if (temp.value().last().starts_with('!')) { + auto exist = WebView::find_search_engine_by_bang(last); + if (exist.has_value()) { + search_engine_url = exist->query_url; + query = MUST(query.substring_from_byte_offset(0, query.bytes().size() - last.bytes().size())); + } + } + } } if (auto url = WebView::sanitize_url(query, search_engine_url); url.has_value()) From 19551bfce554d6f6383d13afe33c878b89070c25 Mon Sep 17 00:00:00 2001 From: Saksham Goyal Date: Sun, 17 Nov 2024 18:02:31 -0500 Subject: [PATCH 3/3] LibWebView+QT: Cleanup code for bangs --- UI/Qt/LocationEdit.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cpp index 9c689b2eb59..6f51e706a73 100644 --- a/UI/Qt/LocationEdit.cpp +++ b/UI/Qt/LocationEdit.cpp @@ -39,22 +39,24 @@ LocationEdit::LocationEdit(QWidget* parent) auto query = ak_string_from_qstring(text()); + auto const bang_char = '!'; // should this be a setting? + Optional search_engine_url; if (Settings::the()->enable_search()) { search_engine_url = Settings::the()->search_engine().query_url; - if (query.starts_with('!')) { - auto splits = MUST(query.split(' ')); // FIXME: some missing error handling here. - auto exist = WebView::find_search_engine_by_bang(splits[0]); - if (exist.has_value()) { - search_engine_url = exist->query_url; - query = MUST(query.substring_from_byte_offset(splits[0].bytes().size())); + + auto const splits = query.split(' '); + if (!splits.is_error()) { + auto const first = splits.value().first(); + if (first.starts_with(bang_char)) { + auto exist = WebView::find_search_engine_by_bang(first); + if (exist.has_value()) { + search_engine_url = exist->query_url; + query = MUST(query.substring_from_byte_offset(first.bytes().size() + 1)); + } } - } - // FIXME: low quality code. this can be merged into the if branch - auto const temp = query.split(' '); - if (!temp.is_error()) { - auto const last = temp.value().last(); - if (temp.value().last().starts_with('!')) { + auto const last = splits.value().last(); + if (last.starts_with(bang_char)) { auto exist = WebView::find_search_engine_by_bang(last); if (exist.has_value()) { search_engine_url = exist->query_url;