慕哥9229398
public static void ExecuteGlobalScript(WebBrowser browser, string script)
{ if (string.IsNullOrWhiteSpace(script)) return; if (browser.Document == null
|| browser.Document.Window == null
|| browser.Document.Window.DomWindow == null) return; try
{ var win = (IHTMLWindow2)browser.Document.Window.DomWindow;
win.execScript(script);
} catch (Exception ex)
{
Debug.Print("{0}\t执行脚本时发生错误:{1}", DateTime.Now, ex.Message);
}
} public static object ExecuteScript(WebBrowser browser, string script, object[] args = null)
{ if (string.IsNullOrWhiteSpace(script)) return null; if (browser.Document == null
|| browser.Document.Window == null
|| browser.Document.Window.DomWindow == null) return null; var win = (IHTMLWindow2)browser.Document.Window.DomWindow; var scriptName = string.Format("WebBrowserInjectedMethod{0}", Math.Abs(DateTime.Now.ToBinary()));
script = string.Format("function {0}(){{{1}}}", scriptName, script); try
{
win.execScript(script, "javascript"); var result = browser.Document.InvokeScript(scriptName, args); return result;
} catch (Exception ex)
{
Debug.Print("{0}\t执行脚本时发生错误:{1}", DateTime.Now, ex.Message); return null;
}
}