如何通过 Firefox 扩展阻止 Firefox 下载和应用 CSS?

发布于 2024-08-25 05:42:10 字数 991 浏览 8 评论 0原文

提前感谢大家——

所以我已经研究这个问题很长一段时间了,并且已经考虑了我所有的选择。我当前取消CSS请求的方法是在nsIWebProgressListener.onStateChange内部使用nsIRequest.cancel。这在大多数情况下都有效,除非事情有点滞后,在我到达它们之前,有一些会滑过并跳出负载组。这显然是一个肮脏的解决方案。

我已阅读以下链接,尝试更好地了解如何在创建 nsIRequest 之前禁用 css...没有骰子。

https://developer.mozilla.org/en/Document_Loading_-_From_Load_Start_to_Finding_a_Handler https://developer.mozilla.org/en/The_life_of_an_HTML_HTTP_request https://developer.mozilla.org/en/Bird's_Eye_View_of_the_Mozilla_Framework

如何禁用 CSS通过演示对象/接口?这可能吗? nsIDocShell 内部有一些属性,暗示您可以通过浏览器 docshell 禁用 css -allowPlugins、allowJavascript、allowMetaRedirects、allowSubframes、allowImages。

有什么建议吗?

谢谢,

萨姆

Thanks to everyone in advance -

So I have been banging on this issue for quite a while now and have burned through all my options. My current approach to canceling css requests is with nsIRequest.cancel inside of nsIWebProgressListener.onStateChange. This works most of the time, except when things are a little laggy a few will slip through and jump out of the loadgroup before I can get to them. This is obviously a dirty solution.

I have read through the following links to try and get a better idea of how to disable css before a nsIRequest is created...no dice.

https://developer.mozilla.org/en/Document_Loading_-_From_Load_Start_to_Finding_a_Handler
https://developer.mozilla.org/en/The_life_of_an_HTML_HTTP_request
https://developer.mozilla.org/en/Bird's_Eye_View_of_the_Mozilla_Framework

How do I disable css via presentation objects/interfaces? Is this possible? Inside of nsIDocShell there are a few attributes that kind of imply you can disable css via the browsers docshell - allowPlugins, allowJavascript, allowMetaRedirects, allowSubframes, allowImages.

Any suggestions?

Thanks,

Sam

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

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

发布评论

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

评论(3

北方。的韩爷 2024-09-01 05:42:10

禁用样式表的菜单选项使用一个函数

setStyleDisabled(true)

,因此您可以在创建新的浏览器选项卡时调用此函数。样式表仍然从服务器请求,但不应用。这个函数不是很复杂,不会与 nsIRequest 混淆,来源:

function setStyleDisabled(disabled) {
  getMarkupDocumentViewer().authorStyleDisabled = disabled;
}

挖掘 Web 开发工具栏源代码我注意到他们的“禁用样式表”函数循环遍历所有 document.styleSheets 并设置 < code>disabled 属性设置为 true,例如:

/* if DOM content is loaded */
var sheets = document.styleSheets;
for(var i in sheets){ sheets[i].disabled = true; }

因此,如果关键是不将 CSS 应用到页面,则上述解决方案之一应该有效。但如果您确实需要阻止从服务器下载样式表,我担心 nsIRequest 拦截是您唯一的选择。

The menu option that disables style sheets uses a function

setStyleDisabled(true)

so you probably can just call this function whenever new browser tab is created. Style sheets are still requested from server, but not applied. This function is not very sophisticated and doesn't mess with nsIRequest, source:

function setStyleDisabled(disabled) {
  getMarkupDocumentViewer().authorStyleDisabled = disabled;
}

Digging in Web Developer Toolbar source code I have noticed that their "disable stylesheets" function loops trough all document.styleSheets and sets the disabled property to true, like:

/* if DOM content is loaded */
var sheets = document.styleSheets;
for(var i in sheets){ sheets[i].disabled = true; }

So if the key is to not apply CSS to pages, one of the above solutions should work. But if you really need to stop style sheets from being downloaded from servers, I'm affraid nsIRequest interception is your only option.

淡看悲欢离合 2024-09-01 05:42:10

将 requests.default.stylesheet 设置为 2,瞧!

实际上,您可以使用权限管理器在逐个主机的基础上阻止或允许样式表。

Set permissions.default.stylesheet to 2 and voilà!

You can actually use the permissions manager to block or allow stylesheets on a host-by-host basis.

一紙繁鸢 2024-09-01 05:42:10

不幸的是,似乎没有像allowImages 这样的简单标志。为此添加的 bugzilla 是 https://bugzilla.mozilla.org/show_bug.cgi?id =340746。您现在可以使用新的 bugzilla 投票功能为其投票。您还可以将自己添加到 CC 列表中,以便在有人使用该列表时收到通知。

一个相关的请求是为我们提供基本的 HTML 解析支持,这可能就是您想要做的。不幸的是,这还不支持,但您可以在 https 上投票/跟踪 bugzilla ://bugzilla.mozilla.org/show_bug.cgi?id=102699

因此,唯一可行的解​​决方案似乎是@pawal 建议的某种拦截。这是一个讨论拦截基础知识的链接,至少可以让您/我们开始https:// developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads。它列出了我在下面列出的几个选项。

前几个似乎只是在页面/文档级别,所以我认为它们没有帮助:

  • Load Events (addEventListener load)
  • Web Progress Listeners (nsIWebProgressListener) - 我尝试了这种方法,它似乎只为页面本身调用,不适用于页面内的内容。
  • 文档加载器服务 - nsIWebProgressListener 的全局版本,因此我认为它具有相同的问题(仅页面级别),

这使得另外两个我还没有尝试过。它们在全球范围内工作,因此您需要将它们过滤到您关心的浏览器/页面。

  • HTTP Observers - 看起来它可能有效,需要验证它回调 CSS
  • 内容策略 - 对我来说似乎是最好的选择,因为它明确被 CSS 调用,有一天我希望尝试一下:)

Unfortunately there doesn't seem to be a simple flag like allowImages. The bugzilla adding for that is https://bugzilla.mozilla.org/show_bug.cgi?id=340746. You can now vote for it using the new bugzilla voting functionality. You can also add yourself to the CC list to be notified if anyone ever works on it.

A related request is to just give us basic HTML parsing support, which may be what you are trying to do. Unfortunately that isn't supported yet either, but you can vote/track the bugzilla for that at https://bugzilla.mozilla.org/show_bug.cgi?id=102699.

So the only workable solution seems to be some sort of interception as @pawal suggests. Here is a link that talks about the basics of interception to at least get you/us started https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads. It lists several options that I list below.

These first few seem to just be at the page/document level so I don't think they help:

  • Load Events (addEventListener load)
  • Web Progress Listeners (nsIWebProgressListener) - I tried this approach, it only seems to be called for the page itself, not for content within the page.
  • Document Loader Service - A global version of nsIWebProgressListener so I think it has the same problem (page level only)

That leaves two others I have not tried yet. They work globally so you would need to filter them to just the browser/pages you care about.

  • HTTP Observers - Seems like it might work, need to verify it calls back for CSS
  • Content Policy - Seems like the best option to me since it explicitly is called for CSS, someday I hope to try it :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文