Implement script message for GTK backend
This commit is contained in:
@@ -31,6 +31,37 @@
|
||||
#include <JavaScriptCore/JSValueRef.h>
|
||||
#include <JavaScriptCore/JSStringRef.h>
|
||||
|
||||
// Helper function to get string from Webkit JS result
|
||||
bool wxGetStringFromJSResult(WebKitJavascriptResult* js_result, wxString* output)
|
||||
{
|
||||
JSGlobalContextRef context = webkit_javascript_result_get_global_context(js_result);
|
||||
JSValueRef value = webkit_javascript_result_get_value(js_result);
|
||||
|
||||
JSValueRef exception = NULL;
|
||||
wxJSStringRef js_value
|
||||
(
|
||||
JSValueIsObject(context, value)
|
||||
? JSValueCreateJSONString(context, value, 0, &exception)
|
||||
: JSValueToStringCopy(context, value, &exception)
|
||||
);
|
||||
|
||||
if ( exception )
|
||||
{
|
||||
if ( output )
|
||||
{
|
||||
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
|
||||
*output = ex_value.ToWxString();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( output != NULL )
|
||||
*output = js_value.ToWxString();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// GTK callbacks
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -290,6 +321,21 @@ wxgtk_webview_webkit_leave_fullscreen(WebKitWebView *WXUNUSED(web_view),
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
wxgtk_webview_webkit_script_message_received(WebKitUserContentManager *WXUNUSED(content_manager),
|
||||
WebKitJavascriptResult *js_result,
|
||||
wxWebViewWebKit *webKitCtrl)
|
||||
{
|
||||
wxWebViewEvent event(wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED,
|
||||
webKitCtrl->GetId(),
|
||||
webKitCtrl->GetCurrentURL(),
|
||||
"");
|
||||
wxString msgStr;
|
||||
if (wxGetStringFromJSResult(js_result, &msgStr))
|
||||
event.SetString(msgStr);
|
||||
webKitCtrl->HandleWindowEvent(event);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
wxgtk_webview_webkit_decide_policy(WebKitWebView *web_view,
|
||||
WebKitPolicyDecision *decision,
|
||||
@@ -1221,32 +1267,7 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output
|
||||
return false;
|
||||
}
|
||||
|
||||
JSGlobalContextRef context = webkit_javascript_result_get_global_context(js_result);
|
||||
JSValueRef value = webkit_javascript_result_get_value(js_result);
|
||||
|
||||
JSValueRef exception = NULL;
|
||||
wxJSStringRef js_value
|
||||
(
|
||||
JSValueIsObject(context, value)
|
||||
? JSValueCreateJSONString(context, value, 0, &exception)
|
||||
: JSValueToStringCopy(context, value, &exception)
|
||||
);
|
||||
|
||||
if ( exception )
|
||||
{
|
||||
if ( output )
|
||||
{
|
||||
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
|
||||
*output = ex_value.ToWxString();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( output != NULL )
|
||||
*output = js_value.ToWxString();
|
||||
|
||||
return true;
|
||||
return wxGetStringFromJSResult(js_result, output);
|
||||
}
|
||||
|
||||
bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output) const
|
||||
@@ -1278,6 +1299,24 @@ bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output) co
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wxWebViewWebKit::AddScriptMessageHandler(const wxString& name)
|
||||
{
|
||||
if (!m_web_view)
|
||||
return false;
|
||||
|
||||
WebKitUserContentManager *ucm = webkit_web_view_get_user_content_manager(m_web_view);
|
||||
g_signal_connect(ucm, wxString::Format("script-message-received::%s", name).utf8_str(),
|
||||
G_CALLBACK(wxgtk_webview_webkit_script_message_received), this);
|
||||
return webkit_user_content_manager_register_script_message_handler(ucm, name.utf8_str());
|
||||
}
|
||||
|
||||
bool wxWebViewWebKit::RemoveScriptMessageHandler(const wxString& name)
|
||||
{
|
||||
WebKitUserContentManager *ucm = webkit_web_view_get_user_content_manager(m_web_view);
|
||||
webkit_user_content_manager_unregister_script_message_handler(ucm, name.utf8_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
||||
{
|
||||
m_handlerList.push_back(handler);
|
||||
|
Reference in New Issue
Block a user