2013-12-20 62 views
2

我一直在尝试从WP8的webbrowser获取元标签信息,但没有成功。WP8 - 从WebBrowser获取元标签信息

我tryed:

var myDesc = (string)myBrowser.InvokeScript("eval", " $('meta[name=description]').attr('content');"); 

这...

string jsString = ""; 
jsString += " var metas = document.getElementsByTagName('meta'); "; 
jsString += " var data = 'test'; "; 
jsString += " var mLen = metas.length; "; 
jsString += " for(var i=0;i<mLen;i++){ "; 
jsString += " if(metas[i].getAttribute('name').toLowerCase() == 'description'){ "; 
jsString += "  data = metas[i].getAttribute('content'); "; 
jsString += " } "; 
jsString += " } "; 

myBrowser.InvokeScript("eval", new string[] { jsString }); 
var myDesc = (string)myBrowser.InvokeScript("eval", "data;"); 

这...

myBrowser.InvokeScript("eval", new string[] { "var desc = document.getElementsByName('description')[0].getAttribute('content');" }); 

,并与所有的错误处理:

{System.SystemException: An unknown error has occurred. Error: 80020101. at Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr) at Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String scriptName, String[] args) at Microsoft.Phone.Controls.WebBrowser.InvokeScript(String scriptName, String[] args) ...

有人可以帮助我吗?

最好的问候!

回答

0

首先在xaml中添加WebBrowser并将IsScriptEnabled设置为true。

<phone:WebBrowser Name="webBrowser" Source="http://www.baidu.com/" IsScriptEnabled="True"/> 

第二寄存器中的事件:LoadedCompeted和ScriptNotify

webBrowser.LoadCompleted += webBrowser_LoadCompleted; 
webBrowser.ScriptNotify += webBrowser_ScriptNotify; 

第三注入的JavaScript代码

void webBrowser_LoadCompleted(object sender, NavigationEventArgs e) 
{ 
    webBrowser.InvokeScript("eval",@"window.Init=function(){var metas=document.getElementsByTagName('meta');var str='';for(i=0;i<metas.length;i++){str+='name:'+metas[i].getAttribute('name')+',content:'+metas[i].getAttribute('content')+';;}window.external.notify(str);}"); 
    webBrowser.InvokeScript("Init"); 
} 

最后你得到webBrowser_ScriptNotify事件的元信息

void webBrowser_ScriptNotify(object sender, NotifyEventArgs e) 
{ 
    System.Diagnostics.Debug.WriteLine(e.Value); 
} 

希望这会有帮助:D

+0

同样的错误: {System.SystemException:发生未知错误。错误:80020101. at Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr) at Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String scriptName,String [] args) at Microsoft.Phone.Controls.WebBrowser.InvokeScript (String scriptName,String [] args) at xxxxxxxx.myBrowser_LoadCompleted ... –

+0

@GabrielGómez';;}在脚本中删除一个; ...我可以得到meta的正确信息。 – AlexisXu

+0

不!同样的错误... :( –