늦게 답변하지만, 아직 도움이 될 수 있습니다. WebBrowser ActiveX 컨트롤을 사용할 때 스크립트를 호출하는 데는 여러 가지 방법이 있습니다. 같은 기술은 (webBrowser.HtmlDocument.DomDocument를 통해) WebBrowser 컨트롤의 윈폼 버전 사용 (webBrowser.Document를 통해) WPF 버전 할 수 있습니다
void CallScript(SHDocVw.WebBrowser axWebBrowser)
{
//
// Using C# dynamics, which maps to COM's IDispatch::GetIDsOfNames,
// IDispatch::Invoke
//
dynamic htmlDocument = axWebBrowser.Document;
dynamic htmlWindow = htmlDocument.parentWindow;
// make sure the web page has at least one <script> tag for eval to work
htmlDocument.body.appendChild(htmlDocument.createElement("script"));
// can call any DOM window method
htmlWindow.alert("hello from web page!");
// call a global JavaScript function, e.g.:
// <script>function TestFunc(arg) { alert(arg); }</script>
htmlWindow.TestFunc("Hello again!");
// call any JavaScript via "eval"
var result = (bool)htmlWindow.eval("(function() { return confirm('Continue?'); })()");
MessageBox.Show(result.ToString());
//
// Using .NET reflection:
//
object htmlWindowObject = GetProperty(axWebBrowser.Document, "parentWindow");
// call a global JavaScript function
InvokeScript(htmlWindowObject, "TestFunc", "Hello again!");
// call any JavaScript via "eval"
result = (bool)InvokeScript(htmlWindowObject, "eval", "(function() { return confirm('Continue?'); })()");
MessageBox.Show(result.ToString());
}
static object GetProperty(object callee, string property)
{
return callee.GetType().InvokeMember(property,
BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public,
null, callee, new Object[] { });
}
static object InvokeScript(object callee, string method, params object[] args)
{
return callee.GetType().InvokeMember(method,
BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
null, callee, args);
}
하는 수, 작동하려면 JavaScript의 eval
위한 적어도 하나 개의 <script>
태그이 있어야한다 위와 같이 주입하십시오.
또는 자바 스크립트 엔진은 webBrowser.Document.InvokeScript("setTimer", new[] { "window.external.notifyScript()", "1" })
또는 webBrowser.Navigate("javascript:(window.external.notifyScript(), void(0))")
과 같은 형식으로 비동기 적으로 초기화 할 수 있습니다.