LibWebView+QT: Support bangs in the URL bar

This commit is contained in:
Saksham Goyal 2024-11-15 22:38:25 -05:00
parent c0ae3aa884
commit 33c5fd31c1
3 changed files with 41 additions and 17 deletions

View file

@ -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<SearchEngine> search_engines()
@ -65,6 +65,19 @@ Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_u
return *it;
}
Optional<SearchEngine const&> 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;

View file

@ -14,12 +14,14 @@ namespace WebView {
struct SearchEngine {
StringView name;
StringView query_url;
StringView bang;
};
ReadonlySpan<SearchEngine> search_engines();
SearchEngine const& default_search_engine();
Optional<SearchEngine const&> find_search_engine_by_name(StringView name);
Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_url);
Optional<SearchEngine const&> find_search_engine_by_bang(StringView bang);
String format_search_query_for_display(StringView query_url, StringView query);
}

View file

@ -37,12 +37,21 @@ LocationEdit::LocationEdit(QWidget* parent)
clearFocus();
Optional<StringView> 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<StringView> 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());
});