如何控制浏览器加载页面?

发布于 2024-09-24 06:29:27 字数 38 浏览 3 评论 0 原文

我想控制网络浏览器中加载的网页。 XUL 可用于满足此要求吗?

I want to control a webpage loaded in web browser. Can XUL be useful for fulfilling this requirement?

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

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

发布评论

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

评论(1

你是暖光i 2024-10-01 06:29:27

任何允许注入 DOM 事件或脚本标签的东西都可以用来控制网页。 XUL 是一种用于描述 GUI 的语言,但对此没有什么用处。

以下是一些如何控制网页的示例。您没有提供有关您打算如何处理所述控件(浏览器扩展、后台脚本等)的任何详细信息,因此我将给出我能想到的所有情况的示例:

如果您想远程控制网络作为脚本的一部分独立于普通浏览器,您可以选择以下选项:

  • 如果您不需要运行网站的 Javascript,请使用像 Mechanize 这样的伪浏览器对于 PerlPython,或 Ruby 或可能 Twill 如果您使用 shell 脚本。它们比实际执行 Javascript 的东西要轻,但它们仍然会处理 cookie,并让您以编程方式填写表格和单击链接。
  • 如果您确实需要运行网站的 javascript,最简单的解决方案是编写一个 Python 脚本,该脚本将 PyQt 与 QWebView 结合使用,但实际上不必显示任何窗口。 QtWebKit 提供了一个受 jQuery 启发的界面,用于在站点的 DOM 中进行操作,并且能够注入 Javascript,这就是远程控制网页所需的一切。 (QtWebKit 确实要求 GUI 在 Linux 上运行,但您不需要实际显示任何窗口来使用它。)必要的 QtWebKit API 也应该存在于任何其他 Qt 绑定中您可能更喜欢的语言。
  • 如果您无法使用 QtWebKit 但需要 Javascript(也许您正在编写 GTK+ 应用程序),您的选择会受到更多限制,但任何浏览器小部件都应该至少支持注入新的

如果您想在现有浏览器中编写网页脚本(也许是为了隐藏广告或跳过您经常访问的页面上的导航屏幕),您可以选择以下选项:

  • 如果您想修改浏览器本身,编写一个浏览器扩展。这可能就是您询问 XUL 的原因。你真正应该问的是 Javascript(Firefox 和 Chrome 扩展中实际执行操作的部分是用 Javascript 编写的。XUL 只是用于在 Firefox 中布局窗口。)...具体来说,Chrome 称之为 内容脚本。 Firefox 的内部结构太复杂,我根本懒得去学习等效的术语。)这是您最强大的选项,但它也是最复杂的选项,并且您的扩展程序只能在一种浏览器上运行。
  • 如果您只是想修改或控制网站,请编写用户脚本。用户脚本是浏览器注入到您选择的页面中的 Javascript 代码。 Firefox 通过 Greasemonkey 以某种形式支持它们、ChromeOpera、各种鲜为人知的浏览器,以及 IE7Pro 扩展,由 Internet Explorer 7 和 8 提供。它们无法修改浏览器的界面,但除此之外,它们非常强大。 (示例

Anything which allows you to inject DOM events or script tags can be used to control a web page. XUL is a language for describing GUIs and isn't useful for that.

Here are some examples of how you can control a webpage. You didn't provide any details as to what you intend to do with said control (Browser extension, background scripting, etc.), so I'll give examples for all cases I can think of:

If you want to remote-control web pages as part of a script independent from your normal browser, here are your options:

  • If you don't need to run the site's Javascript, use a pseudo-browser like Mechanize for Perl, Python, or Ruby or possibly Twill if you're shell scripting. They're lighter than something that actually executes Javascript, but they'll still handle cookies and let you fill out forms and click links programatically.
  • If you do need to run the site's javascript, the simplest solution is to write a Python script which uses PyQt with QWebView but doesn't actually have to show any windows. QtWebKit provides an interface inspired by jQuery for twiddling around in the site's DOM and the ability to inject Javascript and that's all you need to remote-control a web page. (QtWebKit does require that a GUI be running on Linux, but you don't need to actually show any windows to use it.) The necessary QtWebKit APIs should also be present in the Qt bindings for any other language you may prefer.
  • If you can't use QtWebKit but need Javascript (perhaps you're writing a GTK+ application), your options are much more limited, but any browser widget out there should at least support injecting new <script> tags to control the page. This answer (also mine) has a list of web widgets for various Python toolkits and most (probably all) of them are available for other languages as well. You'll also want to read the instructions for writing userscripts.

If you want to script a webpage inside your existing browser (perhaps to hide ads or skip nag screens on pages you visit frequently), here are your options:

  • If you want to modify the browser itself, write a browser extension. This is probably why you were asking about XUL. What you really should be asking about is Javascript (The parts of Firefox and Chrome extensions that actually do things are written in Javascript. XUL is just for laying out windows in Firefox.)... specifically, what Chrome calls Content Scripts. Firefox is too complex internally for me to have bothered to learn the equivalent terminology.) This is your most powerful option, but it's also the most involved and your extension will only work with one browser.
  • If you just want to modify or control the website, write a userscript. Userscripts are bits of Javascript which the browser injects into pages of your choice. They're supported in some form or other by Firefox with Greasemonkey, Chrome, Opera, various lesser-known browsers, and, with the IE7Pro extension, by Internet Explorer 7 and 8. They can't modify the browser's interface, but aside from that, they're pretty powerful. (example)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文