我是靠谱客的博主 纯真刺猬,这篇文章主要介绍GeckoWebBrowser 想使用jquery 但是目标网页没有加载jquery (Winfrom Gecko动态附加jqueryjs),现在分享给大家,希望可以做个参考。

复制代码
1
2
本人测试了3种方法,只有第三种有效(间接append) 注:皆在Gecko_DocumentCompleted 中使用


第一种(无效,直接报document is not defined) 估计是加载问题

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
try { using (AutoJSContext context = new AutoJSContext(browser.Window.JSContext)) { MessageBox.Show("开始加载"); var theAppendJQ = @"var aoq_script=document.createElement(""script""); aoq_script.type = ""text/javascript""; aoq_script.src = ""https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js""; document.getElementsByTagName('head')[0].appendChild(aoq_script); "; context.EvaluateScript(theAppendJQ); string result; context.EvaluateScript(theAppendJQ, out result); MessageBox.Show(result); } } catch (Exception ex) { MessageBox.Show("方式1:"+ ex.Message); }



第二种(无效,没报错,但是在运行jquery时提示$ is not defined) 就是没加载上jquery

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
try { GeckoHtmlElement scriptJQ = (GeckoHtmlElement)browser.Document.CreateElement("div"); scriptJQ.InnerHtml = @"<script> var aoq_script=document.createElement(""script""); aoq_script.type = ""text/javascript""; aoq_script.src = ""https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js""; document.getElementsByTagName('head')[0].appendChild(aoq_script); </script> "; var bodyDC = browser.Document.GetElementsByTagName("body")[0]; bodyDC.AppendChild(scriptJQ); } catch (Exception ex) { MessageBox.Show("方式2:" + ex.Message); }




第三种(有效) 通过查找资料只能曲线救国,使用SetAttribute设置click,并且通过自动点击来使页面动态引入jquery,后续就可以通过jquery 脚本操作了

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
try { string jqFun= @"var aoq_script=document.createElement('script'); aoq_script.type = 'text/javascript'; aoq_script.src = 'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js'; document.getElementsByTagName('head')[0].appendChild(aoq_script);"; var bodyDC = browser.Document.GetElementsByTagName("body")[0]; bodyDC.SetAttribute("onclick", ""+ jqFun); bodyDC.Click(); } catch (Exception ex) { MessageBox.Show("方式3:" + ex.Message); } 经过第三种附加jquery后,就可以使用自己的jquery脚本了 var executor = new Gecko.JQuery.JQueryExecutor(browser.Window); if (!string.IsNullOrEmpty(backJqsrcipt)) { executor.ExecuteJQuery(backJqsrcipt); }

最后

以上就是纯真刺猬最近收集整理的关于GeckoWebBrowser 想使用jquery 但是目标网页没有加载jquery (Winfrom Gecko动态附加jqueryjs)的全部内容,更多相关GeckoWebBrowser内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部