如何WebBrowser控件执行自定义的JavaScript? [英] How to execute custom JavaScript in WebBrowser control?

查看:230
本文介绍了如何WebBrowser控件执行自定义的JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些造型命令适用于WebBrowser控件内特定的网站。要做到这一点,最好的方法是调用JavaScript的(我想这种风格可编辑,与JavaScript很容易)。我知道我可以用 WebBrowser1.Navigate时做到这一点(JavaScript的:警报('喜');无效(0);); 但最大URL长度web浏览器接受是502.如何执行较长的脚本?或者,也许有一种方法来添加我的CSS Web文档?

I want to apply some styling commands to specific website inside WebBrowser control. The best way to do it is to invoke javascript (I want that style to be editable, with javascript it's easy). I know I can do it with webBrowser1.Navigate("javascript: alert('hi'); void(0);"); but maximum url length that webBrowser accepts is 502. How to execute longer scripts? Or maybe there is a way to append my CSS to web document?

P.S。我不能编辑文档的文本属性,因为它会在这个网站破坏脚本,我需要一份工作,但只是slyled了一下。

P.S. I can't edit document's text property since it'll break scripts in this website and I need working copy but just slyled a bit.

推荐答案

下面的文章描述了如何追加了WebBrowser控件的脚本元素添加到文档HEAD元素:

The following post describes how to append a script element to the document HEAD element in a WebBrowser control:

<一个href="http://stackoverflow.com/questions/153748/webbrowser-control-from-net-how-to-inject-javascript/154496#154496">WebBrowser从.NET控件 - ?如何注入Javascript的

code:

    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
    HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
    element.text = "function sayHello() { alert('hello') }";
    head.AppendChild(scriptEl);
    webBrowser1.Document.InvokeScript("sayHello");

这篇关于如何WebBrowser控件执行自定义的JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆