diff --git a/src/gtk/webview_webkit2.cpp b/src/gtk/webview_webkit2.cpp index a8c8cb1280..59fa602c63 100644 --- a/src/gtk/webview_webkit2.cpp +++ b/src/gtk/webview_webkit2.cpp @@ -1141,7 +1141,8 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output if ( !js_result ) { - wxLogWarning(_("Error running JavaScript: %s"), error.GetMessage()); + if ( output ) + *output = error.GetMessage(); return false; } @@ -1158,8 +1159,11 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output if ( exception ) { - wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL)); - wxLogWarning(_("Exception running JavaScript: %s"), ex_value.ToWxString()); + if ( output ) + { + wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL)); + *output = ex_value.ToWxString(); + } return false; } @@ -1172,23 +1176,31 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output) { - wxCHECK_MSG( m_web_view, false, - wxS("wxWebView must be created before calling RunScript()") ); - wxJSScriptWrapper wrapJS(javascript, &m_runScriptCount); + // This string is also used as an error indicator: it's cleared if there is + // no error or used in the warning message below if there is one. wxString result; - bool isValidJS = RunScriptSync(wrapJS.GetWrappedCode(), &result); - - if ( isValidJS && result == "true" ) + if ( RunScriptSync(wrapJS.GetWrappedCode(), &result) + && result == wxS("true") ) { - RunScriptSync(wrapJS.GetOutputCode(), output); + if ( RunScriptSync(wrapJS.GetOutputCode(), &result) ) + { + if ( output ) + *output = result; + result.clear(); + } + RunScriptSync(wrapJS.GetCleanUpCode()); - return true; } - wxLogWarning(_("JavaScript error: %s"), result); - return false; + if ( !result.empty() ) + { + wxLogWarning(_("Error running JavaScript: %s"), result); + return false; + } + + return true; } void wxWebViewWebKit::RegisterHandler(wxSharedPtr handler) diff --git a/src/msw/webview_ie.cpp b/src/msw/webview_ie.cpp index 66944cab0a..2234f51012 100644 --- a/src/msw/webview_ie.cpp +++ b/src/msw/webview_ie.cpp @@ -885,13 +885,7 @@ bool CallEval(const wxString& code, wxVariant* varResult) { wxVariant varCode(code); - if ( !scriptAO.Invoke("eval", DISPATCH_METHOD, *varResult, 1, &varCode) ) - { - wxLogWarning(_("Can't run JavaScript")); - return false; - } - - return true; + return scriptAO.Invoke("eval", DISPATCH_METHOD, *varResult, 1, &varCode); } bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output) @@ -906,7 +900,7 @@ bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output) IDispatch* scriptDispatch = NULL; if ( FAILED(document->get_Script(&scriptDispatch)) ) { - wxLogWarning(_("Can't get the JavaScript")); + wxLogWarning(_("Can't get the JavaScript object")); return false; } @@ -915,24 +909,35 @@ bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output) wxAutomationObject scriptAO(scriptDispatch); wxVariant varResult; + wxString err; if ( !CallEval(wrapJS.GetWrappedCode(), scriptAO, &varResult) ) - return false; - - if ( varResult.IsType("bool") && varResult.GetBool() ) { - if ( !CallEval(wrapJS.GetOutputCode(), scriptAO, &varResult) ) - return false; - + err = _("failed to evaluate"); + } + else if ( varResult.IsType("bool") && varResult.GetBool() ) + { if ( output != NULL ) - *output = varResult.MakeString(); + { + if ( CallEval(wrapJS.GetOutputCode(), scriptAO, &varResult) ) + *output = varResult.MakeString(); + else + err = _("failed to retrieve execution result"); + } - if ( !CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult) ) - return false; - return true; + CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult); + } + else // result available but not the expected "true" + { + err = varResult.MakeString(); } - wxLogWarning(_("JavaScript error: %s"), varResult.MakeString()); - return false; + if ( !err.empty() ) + { + wxLogWarning(_("Error running JavaScript: %s"), varResult.MakeString()); + return false; + } + + return true; } void wxWebViewIE::RegisterHandler(wxSharedPtr handler) diff --git a/src/osx/webview_webkit.mm b/src/osx/webview_webkit.mm index ea75162a9d..13b76e0626 100644 --- a/src/osx/webview_webkit.mm +++ b/src/osx/webview_webkit.mm @@ -417,26 +417,42 @@ bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output) wxJSScriptWrapper wrapJS(javascript, &m_runScriptCount); NSString* result = [m_webView stringByEvaluatingJavaScriptFromString: - wxCFStringRef( wrapJS.GetWrappedCode() ).AsNSString()]; + wxCFStringRef( wrapJS.GetWrappedCode() ).AsNSString()]; - if ( result != nil && [result isEqualToString:@"true"] ) + wxString err; + if ( result == nil ) + { + // This is not very informative, but we just don't have any other + // information in this case. + err = _("failed to evaluate"); + } + else if ( [result isEqualToString:@"true"] ) { result = [m_webView stringByEvaluatingJavaScriptFromString: - wxCFStringRef( wrapJS.GetOutputCode() ).AsNSString()]; + wxCFStringRef( wrapJS.GetOutputCode() ).AsNSString()]; [m_webView stringByEvaluatingJavaScriptFromString: - wxCFStringRef( wrapJS.GetCleanUpCode() ). - AsNSString()]; + wxCFStringRef( wrapJS.GetCleanUpCode() ).AsNSString()]; - if ( result != nil && output != NULL ) - *output = wxCFStringRef::AsString(result); + if ( output != NULL ) + { + if ( result ) + *output = wxCFStringRef::AsString(result); + else + err = _("failed to retrieve execution result"); + } } - else + else // result available but not the expected "true" { - if ( result != nil ) - wxLogWarning(_("JavaScript error: %s"), wxCFStringRef::AsString(result)); + err = wxCFStringRef::AsString(result); + } + + if ( !err.empty() ) + { + wxLogWarning(_("Error running JavaScript: %s"), err); return false; } + return true; }