C#的WEBBROWSER与JS交互小结
程序员文章站
2023-12-15 08:56:46
本文实例总结了c#的webbrowser与js交互的方法。分享给大家供大家参考。具体实现方法如下:
一、实现webbrowser内部跳转,阻止默认打开ie
1、引用封装...
本文实例总结了c#的webbrowser与js交互的方法。分享给大家供大家参考。具体实现方法如下:
一、实现webbrowser内部跳转,阻止默认打开ie
1、引用封装好的webbrowserlinkself.dll实现
复制代码 代码如下:
public partial class mainwindow : window
{
private webbrowser webbrowser = new webbrowser();
public mainwindow()
{
initializecomponent();
this.webbrowser.loadcompleted += new loadcompletedeventhandler(webbrowser_loadcompleted);
//使webbrowser寄宿于label上,实现webborwser内部跳转,不用ie打开
label lb = new label { content = webbrowser };
webbrowserhelper webbrowserhelper = new webbrowserhelper(webbrowser);
helperregistery.sethelperinstance(lb, webbrowserhelper);
webbrowserhelper.newwindow += webbrowseronnewwindow;
this.lbbrowserhost.content = lb;
// this.webbrowser.navigate(new uri("http://www.baidu.com", urikind.relativeorabsolute));
}
private void webbrowseronnewwindow(object sender, canceleventargs e)
{
dynamic browser = sender;
dynamic activeelement = browser.document.activeelement;
var link = activeelement.tostring();
this.webbrowser.navigate(new uri(link, urikind.relativeorabsolute));
e.cancel = true;
}
}
{
private webbrowser webbrowser = new webbrowser();
public mainwindow()
{
initializecomponent();
this.webbrowser.loadcompleted += new loadcompletedeventhandler(webbrowser_loadcompleted);
//使webbrowser寄宿于label上,实现webborwser内部跳转,不用ie打开
label lb = new label { content = webbrowser };
webbrowserhelper webbrowserhelper = new webbrowserhelper(webbrowser);
helperregistery.sethelperinstance(lb, webbrowserhelper);
webbrowserhelper.newwindow += webbrowseronnewwindow;
this.lbbrowserhost.content = lb;
// this.webbrowser.navigate(new uri("http://www.baidu.com", urikind.relativeorabsolute));
}
private void webbrowseronnewwindow(object sender, canceleventargs e)
{
dynamic browser = sender;
dynamic activeelement = browser.document.activeelement;
var link = activeelement.tostring();
this.webbrowser.navigate(new uri(link, urikind.relativeorabsolute));
e.cancel = true;
}
}
2、引用com:microsoft internet controls实现(参考msdn:http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.aspx public partial class mainwindow : window
复制代码 代码如下:
<em id="__mcedel"> {
public mainwindow()
{
initializecomponent();
this.webbrowser1.navigate(new uri("http://www.baidu.com", urikind.relativeorabsolute));
this.webbrowser1.loadcompleted += new loadcompletedeventhandler(webbrowser1_loadcompleted);
}
private iserviceprovider serviceprovider;
void webbrowser1_loadcompleted(object sender, navigationeventargs e)
{
if (this.serviceprovider == null)
{
serviceprovider = (iserviceprovider)webbrowser1.document;
if (serviceprovider != null)
{
guid serviceguid = new guid("0002df05-0000-0000-c000-000000000046");
guid iid = typeof(shdocvw.webbrowser).guid;
var webbrowserptr = (shdocvw.webbrowser)serviceprovider
.queryservice(ref serviceguid, ref iid);
if (webbrowserptr != null)
{
webbrowserptr.newwindow2 += webbrowser1_newwindow2;
}
}
}
}
private void webbrowser1_newwindow2(ref object ppdisp, ref bool cancel)
{
dynamic browser = this.webbrowser1;
dynamic activeelement = browser.document.activeelement;
var link = activeelement.tostring();
this.webbrowser1.navigate(new uri(link, urikind.relativeorabsolute));
cancel = true;
}
[comimport, interfacetype(cominterfacetype.interfaceisiunknown)]
[guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
internal interface iserviceprovider
{
[return: marshalas(unmanagedtype.iunknown)]
object queryservice(ref guid guidservice, ref guid riid);
}
}
</em>
public mainwindow()
{
initializecomponent();
this.webbrowser1.navigate(new uri("http://www.baidu.com", urikind.relativeorabsolute));
this.webbrowser1.loadcompleted += new loadcompletedeventhandler(webbrowser1_loadcompleted);
}
private iserviceprovider serviceprovider;
void webbrowser1_loadcompleted(object sender, navigationeventargs e)
{
if (this.serviceprovider == null)
{
serviceprovider = (iserviceprovider)webbrowser1.document;
if (serviceprovider != null)
{
guid serviceguid = new guid("0002df05-0000-0000-c000-000000000046");
guid iid = typeof(shdocvw.webbrowser).guid;
var webbrowserptr = (shdocvw.webbrowser)serviceprovider
.queryservice(ref serviceguid, ref iid);
if (webbrowserptr != null)
{
webbrowserptr.newwindow2 += webbrowser1_newwindow2;
}
}
}
}
private void webbrowser1_newwindow2(ref object ppdisp, ref bool cancel)
{
dynamic browser = this.webbrowser1;
dynamic activeelement = browser.document.activeelement;
var link = activeelement.tostring();
this.webbrowser1.navigate(new uri(link, urikind.relativeorabsolute));
cancel = true;
}
[comimport, interfacetype(cominterfacetype.interfaceisiunknown)]
[guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
internal interface iserviceprovider
{
[return: marshalas(unmanagedtype.iunknown)]
object queryservice(ref guid guidservice, ref guid riid);
}
}
</em>
二、webbrowser与js的交互
1、与页面标签的交互
复制代码 代码如下:
//引用microsoft.mshtml
//1、添加一个html标签到id为lg的div中
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelement lbelem = doc.createelement("button");
lbelem.innertext = "test";
lbelem.style.background = "red";
ihtmldomnode node = doc.getelementbyid("lg") as ihtmldomnode;
node.appendchild(lbelem as ihtmldomnode);
//2、设置id为su的标签value值和style
//2.1 使用setattribute
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelement search = doc.getelementbyid("su");
ihtmldomattribute att = search.getattribute("value") as ihtmldomattribute;
search.setattribute("value", "百度一下");
//search.click();
search.style.display = "none";
//2.2 使用outerhtml
search.outerhtml = "<input id=\"su\" value=\"百度一下\" class=\"bg s_btn\" type=\"submit\" onclick=\"alert('百度一下');\" />";
//2.3 使用ihtmldomattribute
ihtmlattributecollection attributes = (search as ihtmldomnode).attributes as ihtmlattributecollection;
foreach (ihtmldomattribute attr in attributes)
{
if (attr.nodename == "value")
{
attr.nodevalue = "百度一下";
}
}
//3、替换应用了类样式mnav的a标签
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelementcollection collect = doc.getelementsbytagname("a");
foreach (ihtmlelement elem in collect)
{
if (!(elem is ihtmlunknownelement) && elem.classname != null)
{
if (elem.classname.equals("mnav", stringcomparison.ordinalignorecase))
{
elem.outerhtml = "<a href='#' title='替换标签' >替换</a>";
}
}
}
//4、删除节点
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelement search = doc.getelementbyid("su");
ihtmldomnode node = search as ihtmldomnode;
node.parentnode.removechild(node);
//5、js事件
//5.1 添加js
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelement search = doc.getelementbyid("su");
search.outerhtml = "<input id=\"su\" value=\"百度一下\" class=\"bg s_btn\" type=\"submit\" onclick=\"onclick();\" />";
ihtmlscriptelement scripterrorsuppressed = (ihtmlscriptelement)doc.createelement("script");
scripterrorsuppressed.type = "text/javascript";
scripterrorsuppressed.text = "function onclick(){ alert('添加js'); }";
ihtmlelementcollection nodes = doc.getelementsbytagname("head");
foreach (ihtmlelement elem in nodes)
{
var head = (htmlheadelement)elem;
head.appendchild((ihtmldomnode)scripterrorsuppressed);
}
//5.2 删除js
ihtmlelementcollection scripts = (ihtmlelementcollection)doc.getelementsbyname("script");
foreach (ihtmlelement node in scripts)
{
if (!(node is ihtmlunknownelement))
{
ihtmlscriptelement script = node as ihtmlscriptelement;
//删除所有js文件引用
if (string.isnullorempty(script.text))
{
ihtmldomnode remove = script as ihtmldomnode;
remove.parentnode.removechild(remove);
}
}
}
//6、write new html
mshtml.ihtmldocument2 doc2 = this.webbrowser.document as mshtml.ihtmldocument2;
doc2.clear();
doc2.writeln("<html><body>write new html</body></html>");
//1、添加一个html标签到id为lg的div中
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelement lbelem = doc.createelement("button");
lbelem.innertext = "test";
lbelem.style.background = "red";
ihtmldomnode node = doc.getelementbyid("lg") as ihtmldomnode;
node.appendchild(lbelem as ihtmldomnode);
//2、设置id为su的标签value值和style
//2.1 使用setattribute
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelement search = doc.getelementbyid("su");
ihtmldomattribute att = search.getattribute("value") as ihtmldomattribute;
search.setattribute("value", "百度一下");
//search.click();
search.style.display = "none";
//2.2 使用outerhtml
search.outerhtml = "<input id=\"su\" value=\"百度一下\" class=\"bg s_btn\" type=\"submit\" onclick=\"alert('百度一下');\" />";
//2.3 使用ihtmldomattribute
ihtmlattributecollection attributes = (search as ihtmldomnode).attributes as ihtmlattributecollection;
foreach (ihtmldomattribute attr in attributes)
{
if (attr.nodename == "value")
{
attr.nodevalue = "百度一下";
}
}
//3、替换应用了类样式mnav的a标签
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelementcollection collect = doc.getelementsbytagname("a");
foreach (ihtmlelement elem in collect)
{
if (!(elem is ihtmlunknownelement) && elem.classname != null)
{
if (elem.classname.equals("mnav", stringcomparison.ordinalignorecase))
{
elem.outerhtml = "<a href='#' title='替换标签' >替换</a>";
}
}
}
//4、删除节点
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelement search = doc.getelementbyid("su");
ihtmldomnode node = search as ihtmldomnode;
node.parentnode.removechild(node);
//5、js事件
//5.1 添加js
htmldocument doc = (htmldocument)this.webbrowser.document;
ihtmlelement search = doc.getelementbyid("su");
search.outerhtml = "<input id=\"su\" value=\"百度一下\" class=\"bg s_btn\" type=\"submit\" onclick=\"onclick();\" />";
ihtmlscriptelement scripterrorsuppressed = (ihtmlscriptelement)doc.createelement("script");
scripterrorsuppressed.type = "text/javascript";
scripterrorsuppressed.text = "function onclick(){ alert('添加js'); }";
ihtmlelementcollection nodes = doc.getelementsbytagname("head");
foreach (ihtmlelement elem in nodes)
{
var head = (htmlheadelement)elem;
head.appendchild((ihtmldomnode)scripterrorsuppressed);
}
//5.2 删除js
ihtmlelementcollection scripts = (ihtmlelementcollection)doc.getelementsbyname("script");
foreach (ihtmlelement node in scripts)
{
if (!(node is ihtmlunknownelement))
{
ihtmlscriptelement script = node as ihtmlscriptelement;
//删除所有js文件引用
if (string.isnullorempty(script.text))
{
ihtmldomnode remove = script as ihtmldomnode;
remove.parentnode.removechild(remove);
}
}
}
//6、write new html
mshtml.ihtmldocument2 doc2 = this.webbrowser.document as mshtml.ihtmldocument2;
doc2.clear();
doc2.writeln("<html><body>write new html</body></html>");
2、数据交互
复制代码 代码如下:
public mainwindow()
{
initializecomponent();
this.webbrowser.objectforscripting = new scriptevent();
this.webbrowser.navigatetostring(@"<html><head><title>test</title></head><body><input type=""button"" value=""点击"" onclick=""window.external.showmessage('百度一下');"" /></body></html>");
}
[system.runtime.interopservices.comvisible(true)]
public class scriptevent
{
//供js调用
public void showmessage(string message)
{
messagebox.show(message);
}
}
{
initializecomponent();
this.webbrowser.objectforscripting = new scriptevent();
this.webbrowser.navigatetostring(@"<html><head><title>test</title></head><body><input type=""button"" value=""点击"" onclick=""window.external.showmessage('百度一下');"" /></body></html>");
}
[system.runtime.interopservices.comvisible(true)]
public class scriptevent
{
//供js调用
public void showmessage(string message)
{
messagebox.show(message);
}
}
希望本文所述对大家的c#程序设计有所帮助。
推荐阅读
-
C#的WEBBROWSER与JS交互小结
-
利用thrift实现js与C#通讯的实例代码
-
C#的WebBrowser的操作与注意事项介绍
-
Android的WebView与H5前端JS代码交互的实例代码
-
c#两种方式调用google地球,调用COM API以及调用GEPLUGIN 与js交互,加载kml文件,dae文件。将二维高德地图覆盖到到三维谷歌地球表面。
-
JS与OC交互,JS中调用OC方法(获取JSContext的方式)
-
vue.js与后台数据交互的实例讲解
-
TP5(thinkPHP5)框架使用ajax实现与后台数据交互的方法小结
-
c#两种方式调用google地球,调用COM API以及调用GEPLUGIN 与js交互,加载kml文件,dae文件。将二维高德地图覆盖到到三维谷歌地球表面。
-
Winform 通过 WebBrowser 与 JS 交互