通过CSS文件将CSS添加到页面

通过CSS文件将CSS添加到页面

本文介绍了通过CSS文件将CSS添加到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以通过CSS文件向页面添加CSS,类似于通过JS文件(contentscript)向页面添加JS。所以,我可以定义CSS文件可以工作的页面,然后就像contentcript一样生效。我知道可以通过JavaScript文件添加CSS,但如果可能的话,这样做会更方便。如果这是不可能的,那么当然我必须使用contentscript,但在排除任何可能性之前,我首先想知道。我搜索并没有收到任何答案 - 要么我搜索错误,要么根本不可能。尽管Chrome扩展已经走过了很长的一段路,所以我仍然不排除,直到有人知道可以告诉我是和是的。如何或没有。感谢您的回应。

您的清单文件必须包含内容脚本指向 CSS文件,当url 匹配所选的url字段时将会加载该文件...

 content_scripts:[{
css:[scrollbars.css],
:[http://www.google.com/*]
}],

这与注入 javascript代码的方法是相同的,除非你指向一个 js文件,而不是...




I was wondering if it was possible to add CSS to pages through a CSS file, similarly to adding JS to pages through a JS file (contentscript). So, I could just define what pages said CSS file would work on and then it would take effect just like a contentscript. I understand that it is possible to add CSS through a JavaScript file, but it would be more convenient to do it this way instead if possible. If it's not possible, then of course I must use contentscript, but I would first like to know before I rule out any possibilities. I've searched and received no answers - either I'm searching wrong or it really isn't possible. Chrome extensions have come a long way, though, so I'm still not ruling it out until someone who knows can tell me yes & how or no. Thanks for any responses.

解决方案

Your manifest file must contain a content script pointing to a CSS file, that will load when a url matches your selected url fields...

"content_scripts": [ {
   "css": [ "scrollbars.css" ],
   "matches": [ "http://www.google.com/*" ]
} ],

This is the same method you would use to inject javascript code, except you would point to a js file instead...

More information here...

这篇关于通过CSS文件将CSS添加到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:26