插入带有 firefox 扩展名的本地 css 文件
我正在构建一个 Firefox 扩展,需要在文档中插入一些元素和 css。
我尝试按照 如何Firefox 扩展将本地 css 文件注入网页? 和 使用 Firefox 扩展插入 CSS,但没有运气。
我知道我错过了一些愚蠢的观点,但我无法真正弄清楚它是什么,如果有人能向我指出它,我将非常感激。
这是我的 chrome.manifest:
content helloworld content/
overlay chrome://browser/content/browser.xul chrome://helloworld/content/overlay.xul
locale helloworld en-US locale/en-US/
skin helloworld classic/1.0 skin/
和我的overlay.js:
var fileref = gBrowser.contentDocument.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", "resource://helloworld/skin/global.css");
gBrowser.contentDocument.getElementsByTagName("head")[0].appendChild(fileref);
我什至在我的overlay.js 中尝试过这个,
var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
.getService(Components.interfaces.nsIStyleSheetService);
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var uri = ios.newURI(url, null, null);
sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
但又没有运气。
我缺少什么?我真的想不通。
- 尝试使用控制台,没有显示任何内容
- 当我复制并粘贴我的 href“chrome://helloworld/skin/global.css”时,我可以在浏览器中看到我的 css 文件。
I am building a firefox extension and need to insert some elements and css into the doc.
I tried following How can a Firefox extension inject a local css file into a webpage? and Inserting CSS with a Firefox Extension, but had no luck.
I know am missing some silly point but I cant really make out what it is,and would really appreciate if some one can point it out to me.
Heres my chrome.manifest:
content helloworld content/
overlay chrome://browser/content/browser.xul chrome://helloworld/content/overlay.xul
locale helloworld en-US locale/en-US/
skin helloworld classic/1.0 skin/
And my overlay.js:
var fileref = gBrowser.contentDocument.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", "resource://helloworld/skin/global.css");
gBrowser.contentDocument.getElementsByTagName("head")[0].appendChild(fileref);
I even tried this inside my overlay.js
var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
.getService(Components.interfaces.nsIStyleSheetService);
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var uri = ios.newURI(url, null, null);
sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
No luck again.
What am I missing? I seriously can't figure out.
- Tried using the console,shows nothing
- When I copy and paste my href "chrome://helloworld/skin/global.css", I can see my css file in the browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该设置 javascript.options.showInConsole,重新启动,然后检查错误控制台。
nsIStyleSheetService 片段应该是最简单的工作方式。
其中的
url
是什么?您发布的其他片段/评论相互矛盾 - chrome.manifest 和您的评论“当我复制并粘贴我的 href ...,我可以在浏览器中看到我的 css 文件”暗示您正在使用 chrome:// helloworld/skin/global.css,但您的其他代码片段显示您使用了resource:// URL,这是一个不同的野兽。您如何确定该代码片段不起作用?你的样式表可能有错吗?你是否尝试过像
* {color:red !important;}
这样简单的东西作为你的CSS?PS 如果您使用 nsIStyleSheetService,请注意 MDC 上有关注意不要多次注册同一张表的注释。
另请注意,在使用 nsIStyleSheetService 时,您应该小心,不要将样式应用到您不打算修改的页面。 @-moz-document 可以非常对此有用。
You should set the javascript.options.showInConsole, restart, then check the Error Console.
The nsIStyleSheetService snippet should be the simplest to get working.
What's the
url
in it? The other snippets/comments you posted contradict each other -- the chrome.manifest and your comment "When I copy and paste my href ..., I can see my css file in the browser" imply you're using chrome://helloworld/skin/global.css, but your other snippet shows you use a resource:// URL, which is a different beast.How do you determine the snippet is not working? Could you have your stylesheet wrong? Did you try something simple like
* {color:red !important;}
as your CSS?P.S. If you use nsIStyleSheetService, please note the comment on MDC about taking care not to register the same sheet multiple times.
Also note that when using nsIStyleSheetService you should be careful not to make your styles apply to the pages you didn't intend to modify. @-moz-document can be very useful for that.
我不确定这是否能解决您的问题,但您应该侦听所有选项卡中的加载事件,将您的overlay.js更改为如下所示:
I'm not sure if this will solve your problem, but you should listen for load events in all tabs changing your overlay.js to something like:
忘记overlay.js 文件和overlay.xul 文件,您不需要它。使用 chrome.manifest 的样式指令,如下所示:
No js req'd
Forget the overlay.js file and the overlay.xul file, you don't need it. Use the style instruction for chrome.manifest instead, like so:
No js req'd