在WindowsPhone上阅读HTML“windows。*”值

时间:2013-06-29 22:57:50

标签: c# windows-phone-8

我想从wp8上的webbrowser控件中加载的网站中读取一些变量。 我对HTML脚本不是很安全。 所以我不确定是否可以使用“webBrowser.InvokeScript”或其他一些。

网站看起来像这样:

<html class="no-js mobile">
<head><script type="text/javascript">var NREUMQ=NREUMQ||[];NREUMQ.push(["mark","firstbyte",new  Date().getTime()]);</script>
<title>test</title><meta content="width=320, initial-scale=1.0, user-scalable=no" name="viewport">

<script>
  //<![CDATA[
    window.current_ip = '31.17.30.54';
  //]]>
</script>

<script>
  //<![CDATA[
    window.current_checkout = "{&quot;id&quot;:0,&quot;domestic&quot;:false,&quot;amount&quot;:0.0,&quot;updated_at&quot;:0,&quot;created_at&quot;:0}";
  //]]>
</script>

<script type="text/javascript">
  var _sift = _sift || [];
  _sift.push(['_setAccount', '637102']);
  _sift.push(['_setUserId', '1878617619']);
  _sift.push(['_trackPageview']);

  (function() {
    function loadSift() {
      var sift = document.createElement('script');
      sift.type = 'text/javascript';
      sift.async = true;
      sift.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'dtlilztwypawv.cloudfront.net/s.js';
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(sift, s);
    }
    if (window.attachEvent) {
      window.attachEvent('onload', loadSift);
    } else {
      window.addEventListener('load', loadSift, false);
    }
  })();
</script>

</head>

如果有人能阻止我,我会很高兴的!

编辑-------------------

我认为我离解决方案更近了一步。 我成功运行了这段代码:

webBrowser.InvokeScript("eval", "newfunc_getmyvalue = function() { return 'hello'; }");
string s1 = (string)webBrowser.InvokeScript("newfunc_getmyvalue");

我现在只需知道“window.current_checkout = ...”究竟是什么做的? 它真的设置变量吗?如何修改我的'hello'脚本以返回此值?

2 个答案:

答案 0 :(得分:0)

您可以使用HtmlAgilityPack库来解析html,然后使用xpath或linq-to-xml来查找变量。 使用webclient下载html,然后使用库进行解析。还有其他方法可以下载html。

答案 1 :(得分:0)

要从调用的脚本返回返回值,它必须调用windows.external.notify(),并且WebBrowser控件必须声明ScriptNotify事件处理程序以捕获数据(在e.Value中)。

所以这个:

webBrowser.InvokeScript("eval", "window.external.notify(window.current_checkout);");

应该从网页发出值,

private void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
    string response = e.Value;
    // do something wonderful with response
}

应该在您的应用中捕获它。