How to stop firefox from downloading and applying CSS via a firefox extension?

巧了我就是萌 提交于 2019-12-07 13:16:25

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.

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.

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 :)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!