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..6f51e706a73 100644 --- a/UI/Qt/LocationEdit.cpp +++ b/UI/Qt/LocationEdit.cpp @@ -37,11 +37,34 @@ LocationEdit::LocationEdit(QWidget* parent) clearFocus(); + 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()) + if (Settings::the()->enable_search()) { search_engine_url = Settings::the()->search_engine().query_url; - auto query = ak_string_from_qstring(text()); + 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)); + } + } + 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; + 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()) set_url(url.release_value());