Chrome 中的 cssRules/rules 为 null
我的 chrome 扩展需要修改用户页面上的某些 css 规则。通过 document.styleSheets
访问样式只能访问从同一域内链接的样式。 document.styleSheets
数组的其他元素将 cssRules/rules
设置为 null。
为什么这里应用跨域策略?无论其起源如何,样式都会被应用,那么有什么意义呢?就我而言,如何解决这个问题?
编辑:
我需要修改用户CSS规则(而不是简单地添加我自己的规则)的原因是我需要保护扩展注入的自定义元素免受*
的影响规则。 查看此问题中的详细信息
My chrome extension needs to modify certain css rules on user's page. Accessing styles via document.styleSheets
only gives access to styles linked from within the same domain. Other elements of document.styleSheets
array have cssRules/rules
set to null.
Why is it cross domain policy applies here? Styles are being applied anyway regardless of their origin, so what is the point? And how to get around it in my case?
EDIT:
The reason I need to MODIFY user css rules (as opposed to simply adding my own) is that I need to protect custom element injected by extension from being affected by *
rules. see details in this question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与常规 JavaScript 相比,内容脚本不具有任何跨域权限,因此任何限制都会被保留。请参阅相关的问题#1、问题#2。
您可以在清单中注入自己的CSS样式:
您可以尝试通过定义更高的规则来覆盖原始样式 特异性。
您还可以通过 JavaScript 调整具体元素样式:
Content scripts don't have any cross-domain privileges comparing to a regular javascript, so any limitations are carried over. See related question #1, question #2.
You can inject your own css style in the manifest:
where you can try to overwrite original styles by defining rules with higher specificity.
You can also just tweak concrete element styles through javascript:
我通过将 url 从 http:// 更改为 https:// 解决了我的问题版本。哎哟!
I fixed my version of the issue by changing the url from http:// to https://. Doh!