在后台执行js更改CSS - Chrome扩展
我正在设计一个 Chrome 插件。我有一个将 CSS 注入网页的脚本。现在它链接到 popup.html,因为它只能这样工作。
问题是,我必须在注入 css 之前单击图标才能打开弹出窗口。因为这会加载 javascript 文件来注入 CSS。我不想这样做。
哦,内容脚本不是一个选项,因为它们不支持 chrome.tabs API。
我想要做什么:无需任何用户交互即可注入 CSS。因为要注入 CSS,我必须单击图标打开弹出窗口以加载 js 来加载 css。
如何在后台加载该 javascript 文件以注入 css,而无需任何用户交互?
I am designing a Chrome plugin. I have a script that injects CSS into a web page. It is linked to the popup.html for now, because it only works like that.
The problem is, I have to click on the icon to open the popup before the css is injected. Because that loads the javascript file to inject the CSS. I don't want to have to do that.
Oh, content scripts aren't an option, because they don't support the chrome.tabs API.
What I want to do: inject the CSS without ANY user interaction. Because to inject the CSS I have to click on the icon to open the popup to load the js to load the css.
How can I load that javascript file in the background to inject the css without ANY user interaction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我之前回答的,您可以通过
manifest.json
中的内容脚本定义注入 CSS。您的内容脚本可以使用 与后台页面进行通信 href="http://code.google.com/chrome/extensions/extension.html#event-sendRequest" rel="nofollow noreferrer">chrome.extension.sendRequest
(在内容脚本)和chrome.extension.onRequest
< /a>(在后台页面)。因此,内容脚本是解决您的问题的方法。
示例:
As I have answered earlier, you can inject CSS via a Content script definition in
manifest.json
. Your content script can communicate with the background page usingchrome.extension.sendRequest
(in the content script) andchrome.extension.onRequest
(at the background page).So, content scripts are the way to solve your problem.
Examples: