我正在使用 WebBrowser c# 抓取网站并在向用户显示之前对其进行操作。
例如,我想隐藏当前网站的所有Button。
我通过注入(inject) Javascript 来做到这一点,但我认为为时已晚,因为页面已经呈现出来,因此,在隐藏所有按钮之前会出现轻微的闪烁(闪烁网站显示了所有按钮)。
(显示所有按钮 -> JavaScript .ready(function(){})
注入(inject)执行 -> 所有按钮隐藏)。
private void browser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string js = "$(document).ready(function() { hideButtons(); })";
script = browser1.Document.createElement("script");
script.SetAttribute("text", js);
head.AppendChild(script);
}
上面的代码:JavaScript 注入(inject)。
如何解决闪烁问题(当所有按钮显示一小会儿然后在 jQuery 执行后将它们全部隐藏起来)?
我尝试通过将 OuterHtml
设置为 nothing ""或通过将 css 属性设置为 display:none;
方法在 DocumentCompleted
中操作 DOM 对象。 code> 但还是有那么轻微的闪烁。
private void browser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var b = browser1.Document.getElementById("btn1");
b.OuterHtml = "";
}
有没有办法在呈现给用户之前操作 DOM 对象?
我调用什么事件?
最佳答案
我能想到的唯一可行的方法是拦截导航功能,手动拉取页面,然后在浏览器窗口中呈现 HTML。
private void browser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//Cancel the request
e.Cancel = true;
using (WebClient client = new WebClient())
{
string html = client.DownloadString(e.Url.ToString());
//Find the text that you want to replace here and replace it
browser1.DocumentText = html;
}
}
关于javascript - 在 WebBrowser 的 DocumentCompleted 事件中操作 DOM 时,由于 jQuery 的 $(document).ready 导致轻微闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52278569/