2016-02-15 48 views
0

我研究了Window Universal App这几天。在使用WebView时,我发现它可以在HTML文件中调用脚本,但是如何执行外部JavaScript脚本?UWP WebView调用动态JavaScript

WebView webview = new WebView(); 
webview.Navigate(new Uri("https://www.google.com")); 
string script = @" 
    function getBodyHTML() 
    { 
     var innerHTML = document.getElementsByTagName('body')[0].innerHTML; 
     return innerHTML; 
    } 
"; 

我想要得到的bodyHTML,可能使用这样的事情:

// pseudo code 
webview.IncludeScript(script); 
string bodyHTML = webview.ExecuteScript("getBodyHTML()"); 
+0

[在Windows 8.1存储HTML5应用检索来自网页流量数据]的可能的复制(http://stackoverflow.com/questions/19734973/retrieving-data-from -webview式窗口-8-1-STORE-HTML5的应用程序) – Razor

回答

0

哎呀,只是意识到类似的问题得到回答。

我可以做我的事:

string[] arguments = new string[] {@"document.getElementsByTagName('body')[0].innerHTML;"}; 

try 
{ 
    string bodyHTML = await webview.InvokeScriptAsync("eval", arguments); 
} 
catch (Exception ex) 
{ 

}