使用 Javascript 将 CSS 样式表作为字符串注入 [英] Inject CSS stylesheet as string using Javascript

查看:20
本文介绍了使用 Javascript 将 CSS 样式表作为字符串注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Chrome 扩展程序,我希望用户能够添加自己的 CSS 样式来更改扩展程序页面(而非网页)的外观.我已经研究过使用 document.stylesheets,但它似乎想要拆分规则,并且不会让您注入完整的样式表.有没有一种解决方案可以让我使用字符串在页面上创建新的样式表?

I'm developing a Chrome extension, and I'd like users to be able to add their own CSS styles to change the appearance of the extension's pages (not web pages). I've looked into using document.stylesheets, but it seems like it wants the rules to be split up, and won't let you inject a complete stylesheet. Is there a solution that would let me use a string to create a new stylesheet on a page?

我目前没有使用 jQuery 或类似工具,因此最好使用纯 Javascript 解决方案.

I'm currently not using jQuery or similar, so pure Javascript solutions would be preferable.

推荐答案

有几种方法可以做到这一点,但最简单的方法是创建一个