Use new JS wrapper with edge and IE
This commit is contained in:
committed by
Tobias Taschner
parent
2fe12ae6af
commit
cf6a947dab
@@ -113,8 +113,6 @@ private:
|
|||||||
|
|
||||||
void OnTopLevelParentIconized(wxIconizeEvent& event);
|
void OnTopLevelParentIconized(wxIconizeEvent& event);
|
||||||
|
|
||||||
bool RunScriptSync(const wxString& javascript, wxString* output = NULL) const;
|
|
||||||
|
|
||||||
wxDECLARE_DYNAMIC_CLASS(wxWebViewEdge);
|
wxDECLARE_DYNAMIC_CLASS(wxWebViewEdge);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -831,32 +831,37 @@ void wxWebViewEdge::MSWSetBrowserExecutableDir(const wxString & path)
|
|||||||
wxWebViewEdgeImpl::ms_browserExecutableDir = path;
|
wxWebViewEdgeImpl::ms_browserExecutableDir = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output) const
|
bool wxWebViewEdge::RunScript(const wxString& javascript, wxString* output) const
|
||||||
{
|
{
|
||||||
bool scriptExecuted = false;
|
|
||||||
if (!m_impl->m_webView)
|
if (!m_impl->m_webView)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
wxJSScriptWrapper wrapJS(javascript, wxJSScriptWrapper::JS_OUTPUT_STRING);
|
||||||
|
|
||||||
|
int scriptResult = -1;
|
||||||
|
wxString scriptOutput;
|
||||||
|
|
||||||
// Start script execution
|
// Start script execution
|
||||||
HRESULT executionResult = m_impl->m_webView->ExecuteScript(javascript.wc_str(), Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
|
HRESULT executionResult = m_impl->m_webView->ExecuteScript(wrapJS.GetWrappedCode().wc_str(), Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
|
||||||
[&scriptExecuted, &executionResult, output](HRESULT error, PCWSTR result) -> HRESULT
|
[&scriptResult, &executionResult, &scriptOutput](HRESULT error, PCWSTR result) -> HRESULT
|
||||||
{
|
{
|
||||||
// Handle script execution callback
|
// Handle script execution callback
|
||||||
if (error == S_OK)
|
if (error == S_OK)
|
||||||
{
|
{
|
||||||
if (output)
|
scriptOutput.assign(result);
|
||||||
output->assign(result);
|
scriptResult = 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
executionResult = error;
|
executionResult = error;
|
||||||
|
scriptResult = 0;
|
||||||
scriptExecuted = true;
|
}
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}).Get());
|
}).Get());
|
||||||
|
|
||||||
// Wait for script exection
|
// Wait for script exection
|
||||||
while (!scriptExecuted)
|
while (scriptResult == -1)
|
||||||
wxYield();
|
wxYield();
|
||||||
|
|
||||||
if (FAILED(executionResult))
|
if (FAILED(executionResult))
|
||||||
@@ -865,40 +870,22 @@ bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output)
|
|||||||
output->Printf("%s (0x%08lx)", wxSysErrorMsgStr(executionResult), executionResult);
|
output->Printf("%s (0x%08lx)", wxSysErrorMsgStr(executionResult), executionResult);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wxWebViewEdge::RunScript(const wxString& javascript, wxString* output) const
|
wxString scriptDecodedResult;
|
||||||
{
|
|
||||||
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;
|
|
||||||
if (RunScriptSync(wrapJS.GetWrappedCode(), &result)
|
|
||||||
&& result == wxS("true"))
|
|
||||||
{
|
|
||||||
if (RunScriptSync(wrapJS.GetUnwrappedOutputCode() + ";", &result))
|
|
||||||
{
|
|
||||||
if (output)
|
|
||||||
// Try to decode JSON string or return original
|
// Try to decode JSON string or return original
|
||||||
// result if it's not a valid JSON string
|
// result if it's not a valid JSON string
|
||||||
if (!wxJSON::DecodeString(result, output))
|
if (!wxJSON::DecodeString(scriptOutput, &scriptDecodedResult))
|
||||||
*output = result;
|
scriptDecodedResult = scriptOutput;
|
||||||
result.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
RunScriptSync(wrapJS.GetCleanUpCode());
|
wxString scriptExtractedOutput;
|
||||||
}
|
bool success = wxJSScriptWrapper::ExtractOutput(scriptDecodedResult, &scriptExtractedOutput);
|
||||||
|
if (!success)
|
||||||
|
wxLogWarning(_("Error running JavaScript: %s"), scriptExtractedOutput);
|
||||||
|
|
||||||
if (!result.empty())
|
if (output)
|
||||||
{
|
*output = scriptDecodedResult;
|
||||||
wxLogWarning(_("Error running JavaScript: %s"), result);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxWebViewEdge::AddScriptMessageHandler(const wxString& name)
|
bool wxWebViewEdge::AddScriptMessageHandler(const wxString& name)
|
||||||
|
@@ -1045,40 +1045,25 @@ bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
wxJSScriptWrapper wrapJS(javascript, &m_runScriptCount);
|
wxJSScriptWrapper wrapJS(javascript, wxJSScriptWrapper::JS_OUTPUT_IE);
|
||||||
|
|
||||||
wxAutomationObject scriptAO(scriptDispatch);
|
wxAutomationObject scriptAO(scriptDispatch);
|
||||||
wxVariant varResult;
|
wxVariant varResult;
|
||||||
|
|
||||||
wxString err;
|
bool success = false;
|
||||||
if ( !CallEval(wrapJS.GetWrappedCode(), scriptAO, &varResult) )
|
wxString scriptOutput;
|
||||||
{
|
if ( CallEval(wrapJS.GetWrappedCode(), scriptAO, &varResult) )
|
||||||
err = _("failed to evaluate");
|
success = wxJSScriptWrapper::ExtractOutput(varResult.MakeString(), &scriptOutput);
|
||||||
}
|
|
||||||
else if ( varResult.IsType("bool") && varResult.GetBool() )
|
|
||||||
{
|
|
||||||
if ( output != NULL )
|
|
||||||
{
|
|
||||||
if ( CallEval(wrapJS.GetOutputCode(), scriptAO, &varResult) )
|
|
||||||
*output = varResult.MakeString();
|
|
||||||
else
|
else
|
||||||
err = _("failed to retrieve execution result");
|
scriptOutput = _("failed to evaluate");
|
||||||
}
|
|
||||||
|
|
||||||
CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult);
|
if (!success)
|
||||||
}
|
wxLogWarning(_("Error running JavaScript: %s"), scriptOutput);
|
||||||
else // result available but not the expected "true"
|
|
||||||
{
|
|
||||||
err = varResult.MakeString();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !err.empty() )
|
if (success && output)
|
||||||
{
|
*output = scriptOutput;
|
||||||
wxLogWarning(_("Error running JavaScript: %s"), varResult.MakeString());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
||||||
|
Reference in New Issue
Block a user