通过 firefox 扩展将 CSS 文件注入网页
我正在编写一个 Firefox 扩展,并且正在使用他们的附加 SDK;但我不知道如何将数据文件夹中的本地 CSS 文件注入到网页中。如果有一种方法可以通过 page_mod
包来做到这一点,那就太好了。
I am writing a Firefox extension and I am using their Add-on SDK; but I can't figure out how to inject a local CSS file from the data folder into the webpage. It would be great if there were a way to do it via page_mod
package.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从附加 SDK 1.14 开始,page-mod 模块:
参见 修改网页基于 URL,获取使用 page-mod 的详细指南。
Addon SDK 的 wiki 上有一个页面讨论当前实现的问题,虽然看起来有点过时了。
它在底层使用 nsIDOMWindowUtils.loadSheet( )来添加样式表而不触及页面的 DOM。 (此 API 已在 Firefox 18 中添加,请参阅 bug 737003。在此之前,您有使用 nsIStyleSheetService 其中类似,但不是特定于选项卡的。)
在此之前,您可以使用 page-mod 的内容脚本插入链接或样式元素(示例)。 [编辑] 感谢 lwburk 的评论,这里有更详细的描述 Greasemonkey 黑客:提示和信息用 Firefox 重新混合 Web 的工具 作者:Mark Pilgrim:“更改页面样式”部分。
As of Add-on SDK 1.14 there's experimental (API may change) support for this in the page-mod module:
See Modifying Web Pages Based on URL for an elaborate guide to using page-mod.
There's a page on the Addon SDK's wiki discussing issues with the current implementation, although it seems a bit outdated.
Under the hood it uses nsIDOMWindowUtils.loadSheet() to add the stylesheet without touching the page's DOM. (This API was added in Firefox 18, see bug 737003. Before that you had to use nsIStyleSheetService which was similar, but not tab-specific.)
Before that you could use the page-mod's content script to insert the link or style element (example). [edit] thanks to lwburk's comment, here's a more elaborate elaborate description in Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox By Mark Pilgrim: "Alter a Page's Style" section.
要从 main.js 插入 CSS,现在可以使用“page-mod”:
To insert CSS from main.js one can now use "page-mod":