【问题标题】:WP8 - Get meta tags info from WebBrowserWP8 - 从 WebBrowser 获取元标记信息
【发布时间】:2014-01-09 13:08:43
【问题描述】:

我一直在尝试从 WP8 上的网络浏览器获取元标记信息,但没有成功。

我试过了:

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:发生未知错误。错误: 80020101. 在 Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr) 在 Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(字符串 脚本名称,字符串 [] 参数)在 Microsoft.Phone.Controls.WebBrowser.InvokeScript(字符串脚本名称, 字符串[] args) ...

有人可以帮帮我吗?

最好的问候!!

【问题讨论】:

    标签: windows-phone-8 meta invokescript


    【解决方案1】:

    首先在 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

    【讨论】:

    • 同样的错误:{System.SystemException:发生未知错误。错误:80020101. 在 Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String scriptName, String[] args) 在 Microsoft.Phone.Controls.WebBrowser.InvokeScript(String scriptName) 的 Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr) , String[] args) 在 xxxxxxxx.myBrowser_LoadCompleted...
    • @GabrielGómez ';;} 在脚本中删除一个 ;... 我可以获得元数据的正确信息。
    猜你喜欢
    • 2011-11-23
    • 2011-07-06
    • 2011-11-30
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2016-10-05
    相关资源
    最近更新 更多