Implement RunScriptAsync for webkit2

This commit is contained in:
Tobias Taschner 2021-04-09 22:56:09 +02:00 committed by Tobias Taschner
parent e1bd17d883
commit 93f7df50d5
No known key found for this signature in database
GPG Key ID: AE6ECD71294F87FD
2 changed files with 42 additions and 47 deletions

View File

@ -27,6 +27,8 @@ typedef struct _WebKitWebView WebKitWebView;
// wxWebViewWebKit
//-----------------------------------------------------------------------------
class wxWebKitRunScriptParams;
class WXDLLIMPEXP_WEBVIEW wxWebViewWebKit : public wxWebView
{
public:
@ -119,13 +121,15 @@ public:
virtual wxString GetSelectedSource() const wxOVERRIDE;
virtual void ClearSelection() wxOVERRIDE;
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) const wxOVERRIDE;
#if wxUSE_WEBVIEW_WEBKIT2
virtual void RunScriptAsync(const wxString& javascript, void* clientData = NULL) const wxOVERRIDE;
virtual bool AddScriptMessageHandler(const wxString& name) wxOVERRIDE;
virtual bool RemoveScriptMessageHandler(const wxString& name) wxOVERRIDE;
virtual bool AddUserScript(const wxString& javascript,
wxWebViewUserScriptInjectionTime injectionTime = wxWEBVIEW_INJECT_AT_DOCUMENT_START) wxOVERRIDE;
virtual void RemoveAllUserScripts() wxOVERRIDE;
#else
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) const wxOVERRIDE;
#endif
//Virtual Filesystem Support
@ -151,6 +155,11 @@ public:
//create-web-view signal and so we need to send a new window event
bool m_creating;
#if wxUSE_WEBVIEW_WEBKIT2
// This methods needs to be public to make it callable from a callback
void ProcessJavaScriptResult(GAsyncResult *res, wxWebKitRunScriptParams* params) const;
#endif
protected:
virtual void DoSetPage(const wxString& html, const wxString& baseUrl) wxOVERRIDE;
@ -173,7 +182,6 @@ private:
bool CanExecuteEditingCommand(const gchar* command) const;
void SetupWebExtensionServer();
GDBusProxy *GetExtensionProxy() const;
bool RunScriptSync(const wxString& javascript, wxString* output = NULL) const;
#endif
WebKitWebView *m_web_view;

View File

@ -1231,6 +1231,13 @@ wxString wxWebViewWebKit::GetPageText() const
return wxString();
}
class wxWebKitRunScriptParams
{
public:
const wxWebViewWebKit* webKitCtrl;
void* clientData;
};
extern "C"
{
@ -1238,75 +1245,55 @@ static void wxgtk_run_javascript_cb(GObject *,
GAsyncResult *res,
void *user_data)
{
g_object_ref(res);
GAsyncResult** res_out = static_cast<GAsyncResult**>(user_data);
*res_out = res;
wxWebKitRunScriptParams* params = static_cast<wxWebKitRunScriptParams*>(user_data);
params->webKitCtrl->ProcessJavaScriptResult(res, params);
}
} // extern "C"
// Run the given script synchronously and return its result in output.
bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output) const
void wxWebViewWebKit::ProcessJavaScriptResult(GAsyncResult *res, wxWebKitRunScriptParams* params) const
{
GAsyncResult *result = NULL;
webkit_web_view_run_javascript(m_web_view,
javascript.utf8_str(),
NULL,
wxgtk_run_javascript_cb,
&result);
GMainContext *main_context = g_main_context_get_thread_default();
while ( !result )
g_main_context_iteration(main_context, TRUE);
wxGtkError error;
wxWebKitJavascriptResult js_result
(
webkit_web_view_run_javascript_finish
(
m_web_view,
result,
res,
error.Out()
)
);
// Match g_object_ref() in wxgtk_run_javascript_cb()
g_object_unref(result);
if ( !js_result )
if ( js_result )
{
if ( output )
*output = error.GetMessage();
return false;
wxString scriptResult;
if ( wxGetStringFromJSResult(js_result, &scriptResult) )
{
wxString scriptOutput;
bool success = wxJSScriptWrapper::ExtractOutput(scriptResult, &scriptOutput);
SendScriptResult(params->clientData, success, scriptOutput);
}
}
else
SendScriptResult(params->clientData, false, error.GetMessage());
return wxGetStringFromJSResult(js_result, output);
delete params;
}
bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output) const
void wxWebViewWebKit::RunScriptAsync(const wxString& javascript, void* clientData) const
{
wxJSScriptWrapper wrapJS(javascript, wxJSScriptWrapper::JS_OUTPUT_STRING);
bool success = false;
wxString result;
wxString scriptOutput;
if (RunScriptSync(wrapJS.GetWrappedCode(), &result))
{
success = wxJSScriptWrapper::ExtractOutput(result, &scriptOutput);
}
// Collect parameters for access from the callback
wxWebKitRunScriptParams* params = new wxWebKitRunScriptParams;
params->webKitCtrl = this;
params->clientData = clientData;
if (output)
output->assign(scriptOutput);
if (!success)
{
wxLogWarning(_("Error running JavaScript: %s"), scriptOutput);
return false;
}
else
return true;
webkit_web_view_run_javascript(m_web_view,
wrapJS.GetWrappedCode().utf8_str(),
NULL,
wxgtk_run_javascript_cb,
params);
}
bool wxWebViewWebKit::AddScriptMessageHandler(const wxString& name)