梦中的梧桐 發表於 2021-1-27 23:19:00

CefSharp EvaluateScriptAsync执行js 返回对象、页面元素Node、对象数组

<p>正常情况下,返回单值是没有问题的,但是返回对象,Node这种对象,要想让C#正常接收 js 就得特殊处理一下了:</p>
<p>这里webview就是ChromiumWebBrowser浏览器对象</p>
<h3>返回对象</h3>
<div class="cnblogs_code">
<pre>webview.EvaluateScriptAsync(<span style="color: rgba(128, 0, 0, 1)">"</span><strong><span style="color: rgba(128, 0, 0, 1)">new Object({'name':'zhangsan','age':10})</span></strong><span style="color: rgba(128, 0, 0, 1)">"</span>).ContinueWith(<span style="color: rgba(0, 0, 255, 1)">new</span> Action&lt;Task&lt;JavascriptResponse&gt;&gt;((respA) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    JavascriptResponse resp </span>=<span style="color: rgba(0, 0, 0, 1)"> respA.Result;<br>    //respObj此时有两个属性: name、age
    </span><span style="color: rgba(0, 0, 255, 1)">dynamic</span> respObj =<span style="color: rgba(0, 0, 0, 1)"> resp.Result;
    Console.WriteLine(<strong>respObj.name </strong></span>+ <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"><strong> respObj.age</strong>);
}));</span></pre>
</div>
<p>JS返回的对象直接使用&nbsp;{'name':'zhangsan','age':10} 是不行的,需要用Object对象包起来由于见识太短,不知道什么原因,非常欢迎各位前辈指教,非常感谢!</p>
<h3><strong>数组对象</strong></h3>
<p>返回数组对象也一样,数组中不能直接使用{xx:xx}形式, 还是需要通过Object返回</p>
<div class="cnblogs_code">
<pre>webview.EvaluateScriptAsync(<strong><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"></span><span style="color: rgba(128, 0, 0, 1)">"</span></strong>).ContinueWith(<span style="color: rgba(0, 0, 255, 1)">new</span> Action&lt;Task&lt;JavascriptResponse&gt;&gt;((respA) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    JavascriptResponse resp </span>=<span style="color: rgba(0, 0, 0, 1)"> respA.Result;
    List</span>&lt;<span style="color: rgba(0, 0, 255, 1)">dynamic</span>&gt; respObj = (List&lt;<span style="color: rgba(0, 0, 255, 1)">dynamic</span>&gt;<span style="color: rgba(0, 0, 0, 1)">)resp.Result;
    Console.WriteLine(<strong>respObj[</strong></span><strong><span style="color: rgba(128, 0, 128, 1)">0</span></strong><span style="color: rgba(0, 0, 0, 1)"><strong>].name</strong> );
}));</span></pre>
</div>
<h3>页面对象</h3>
<p>页面元素对象有点特殊,直接使用new Object(对象) 是不行的,用到Node元素中哪个属性,需要手动添加:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="http://www.baidu.com" id = "a"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>A<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="http://i.cnblogs.com"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>B<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>单个Node对象时,暂时没找到好的转换成Object办法,如果各位前辈有更好的处理办法,请留言!</p>
<p>比如此处用到了A标签的href属性:</p>
<div class="cnblogs_code">
<pre>//获取A标签的href属性<br>webview.EvaluateScriptAsync(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">new Object({href: document.getElementById('a').href})</span><span style="color: rgba(128, 0, 0, 1)">"</span>).ContinueWith(<span style="color: rgba(0, 0, 255, 1)">new</span> Action&lt;Task&lt;JavascriptResponse&gt;&gt;((respA) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    JavascriptResponse resp </span>=<span style="color: rgba(0, 0, 0, 1)"> respA.Result;
    Console.WriteLine(resp.Result.href);
}));</span></pre>
</div>
<p>多个Node对象时,通过js的Array.from把一个个Node对象拼成了Object对象:</p>
<div class="cnblogs_code">
<pre>//获取多个A标签href属性<br>webview.EvaluateScriptAsync(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Array.from(document.querySelectorAll('a'), item =&gt; new Object({ href: item.href }) );</span><span style="color: rgba(128, 0, 0, 1)">"</span>).ContinueWith(<span style="color: rgba(0, 0, 255, 1)">new</span> Action&lt;Task&lt;JavascriptResponse&gt;&gt;((respA) =&gt;<span style="color: rgba(0, 0, 0, 1)">
{
    JavascriptResponse resp </span>=<span style="color: rgba(0, 0, 0, 1)"> respA.Result;
    List</span>&lt;<span style="color: rgba(0, 0, 255, 1)">dynamic</span>&gt; respObj = (List&lt;<span style="color: rgba(0, 0, 255, 1)">dynamic</span>&gt;<span style="color: rgba(0, 0, 0, 1)">)resp.Result;
    Console.WriteLine(respObj[</span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">].href);
}));</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/GengMingYan/p/14337552.html
頁: [1]
查看完整版本: CefSharp EvaluateScriptAsync执行js 返回对象、页面元素Node、对象数组