如何注入
进入 WPF WebBrowser 控件以重叠其中的所有网页内容?

发布于 2025-01-04 01:07:27 字数 176 浏览 0 评论 0原文

我想要做的是,当 WPF WebBrowser 加载页面时,我只想在页面内容上放置一些 以防止用户在下载的页面中发生任何点击事件

所以这个想法是以某种方式将包含它的样式插入到下载的页面中......

可以这样做吗?

如果可以的话,那么如何用C#实现呢?

What I want to do is when WPF WebBrowser loads page I just want to put over page content some to prevent any click events from user within downloaded page.

So the idea is somehow insert that including it style into downloaded page...

Is it possible to do?

If yes, then how it could be implemented in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2025-01-11 01:07:27

您可以导航到 javascript: URL 吗?这可能是最便宜的方法,我相信这就是我过去最终所做的。

还有另一种方法涉及直接访问 DOM。它涉及访问 WebBrowser.Document,您可以将其转换为 COM 类型。我需要向您回复确切的技术 - 我已将其保存在某处 - 但在 MSDN

编辑:

测试 JavaScript 的最简单方法是执行以下操作:

webBrowser.Navigate("javascript:alert('Test');")

如果出现一个对话框,显示 Test 且网页没有消失,则说明它有效。如果测试有效,您可以在那里插入任何您想要的 JavaScript。例如:

webBrowser.Navigate("javascript:void(document.body.innerHTML = '<div>Test</div>' + document.body.innerHTML);");

正确的方法

我将尝试一下,但它尚未经过测试,因此请记住,您可能需要稍微调整此过程。

  1. 参考ShDocVw.dll
  2. mshtml 命名空间添加 using 语句。
  3. WebBrowser.Document 对象转换为 mshtml.IHTMLDocument2 接口。
  4. 这应该使您能够访问文档的 DOM,因此您应该能够使用 IntelliSense 来弄清楚从这里要做什么。不过,您将使用 COM,因此这会有点麻烦,并且需要在接口方面进行大量工作。

编辑:这个问题 有一些很好的例子。不过,我会使用普通转换而不是 as 关键字。 obj as Somethingobj is Something 的语法糖? (某事)obj:null。额外的测试在这里确实是不必要的。

Can you navigate to a javascript: URL? That's probably the cheap way to do it, and I believe that's what I ended up doing in the past.

There's another way involving directly accessing the DOM. It involves accessing WebBrowser.Document, which you can cast to a COM type. I'll need to get back to you on the exact technique--I have it saved somewhere--but there's a tiny bit of a hint on MSDN.

Edit:

The easiest way to test the JavaScript would be to do something like:

webBrowser.Navigate("javascript:alert('Test');")

If you get a dialog saying Test without your web page disappearing, it works. You can insert any JavaScript you want there, provided the test worked. For example:

webBrowser.Navigate("javascript:void(document.body.innerHTML = '<div>Test</div>' + document.body.innerHTML);");

The Proper Way

I'm going to give this a shot, but it's untested, so keep in mind that you might need to tweak this process a bit.

  1. Reference to ShDocVw.dll.
  2. Add a using statement for the mshtml namespace.
  3. Cast the WebBrowser.Document object to an mshtml.IHTMLDocument2 interface.
  4. This should give you access to the document's DOM, so you should be able to use IntelliSense to figure out what to do from here. You'll be using COM, though, so it will be a bit of a pain, and will require a lot of work with interfaces.

Edit: This question has some good examples. I would use plain casting instead of the as keyword, though. obj as Something is syntax sugar for obj is Something ? (Something)obj : null. That extra test really is unnecessary here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文