added webview scheme handler

This commit is contained in:
Armin Novak 2023-06-21 15:47:54 +02:00 committed by Martin Fleisz
parent 8b0f4ca106
commit 1b79b60634
2 changed files with 42 additions and 0 deletions

View File

@ -700,6 +700,18 @@ namespace webview
navigateCallback = std::move(callback);
}
void add_scheme_handler(const std::string& scheme,
std::function<void(const std::string&, void*)> callback,
void* arg)
{
auto view = WEBKIT_WEB_VIEW(m_webview);
auto context = webkit_web_view_get_context(view);
scheme_handlers.insert({ scheme, { .arg = arg, .fkt = callback } });
webkit_web_context_register_uri_scheme(context, scheme.c_str(), scheme_handler,
static_cast<gpointer>(this), nullptr);
}
void set_html(const std::string& html)
{
webkit_web_view_load_html(WEBKIT_WEB_VIEW(m_webview), html.c_str(), nullptr);
@ -724,6 +736,33 @@ namespace webview
private:
virtual void on_message(const std::string& msg) = 0;
struct handler_t
{
void* arg;
std::function<void(const std::string&, void*)> fkt;
};
std::map<std::string, handler_t> scheme_handlers;
void scheme_handler_call(const std::string& scheme, const std::string& url)
{
auto handler = scheme_handlers.find(scheme);
if (handler != scheme_handlers.end())
{
const auto& arg = handler->second;
arg.fkt(url, arg.arg);
}
}
static void scheme_handler(WebKitURISchemeRequest* request, gpointer user_data)
{
auto _this = static_cast<gtk_webkit_engine*>(user_data);
auto scheme = webkit_uri_scheme_request_get_scheme(request);
auto uri = webkit_uri_scheme_request_get_uri(request);
_this->scheme_handler_call(scheme, uri);
}
static char* get_string_from_js_result(WebKitJavascriptResult* r)
{
char* s;

View File

@ -68,9 +68,12 @@ static void fkt(const std::string& url, void* arg)
bool webview_impl_run(const std::string& title, const std::string& url, std::string& code)
{
webview::webview w(false, nullptr);
w.set_title(title);
w.set_size(640, 480, WEBVIEW_HINT_NONE);
std::string scheme;
w.add_scheme_handler("ms-appx-web", fkt, &scheme);
w.add_navigate_listener(fkt, &code);
w.navigate(url);
w.run();