在 Chrome 扩展中读取文档 CSS
我正在尝试使用 chrome 扩展来读取页面 CSS。这就是我的内容脚本中的内容:
var allSheets = document.styleSheets;
for (var i = 0; i < allSheets.length; ++i) {
var sheet = allSheets[i];
var src = sheet.href;
var rules = sheet.cssRules || sheet.rules;
}
由于某种原因,规则始终为空。我确实获得了“src”变量中使用的所有 CSS 文件。但规则总是为空。当我在 HTML 页面上将其作为单独的 javascript 尝试时,它就起作用了。但是当我将其放入 chrome 扩展的内容脚本中时失败。有人可以告诉我为什么吗?
I am trying to read the pages CSS using a chrome extension. This is what i have in my content script :
var allSheets = document.styleSheets;
for (var i = 0; i < allSheets.length; ++i) {
var sheet = allSheets[i];
var src = sheet.href;
var rules = sheet.cssRules || sheet.rules;
}
For some reason the rules are always empty. I do get all the CSS files used in the 'src' variable. But the rules always come as null.. Its working when I try it as a separate javascript on a HTML page. But fails when I put it up in the content script of my chrome extension. Can somebody lemme know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,这就是原因,但是为了乐趣和兴趣(以前从未使用样式表做过任何事情)我想我应该做一个如何......
manifest.json
myscript.js
对于这个问题,我可能会在注入的脚本中进行大部分检查,然后通过 div 及其事件将结果传回。但我想看看是否可以使用内容脚本中的 dom 方法来浏览 css,这是我能想到的唯一方法。我不喜欢将规则插入回页面的想法,但无法找到任何其他方法。
Well thats the Why, but for fun and interest (never done anything with style sheets before) I thought Id do a How....
manifest.json
myscript.js
In the case of this question Id prolly do most of the checks in the injected script and then pass the results back through the div and its event. But I wanted to see if I could use the dom methods in the content script to go through the css and this was the only way I could figure to do it. I dont like the idea of inserting the rules back into the page, but couldnt figure any other way of doing it.
要获取所有外部 css 和所有内部 css 文件,您可以使用 devtools API。如果你想在 chrome 扩展中使用它,你需要将 devtool 挂接到你的 chrome 扩展中。这段代码将起作用
For getting all external css and all internal css file, you can use devtools API. If you want to use it in chrome extension you need to hook devtool into you chrome extension. This code will work
只是猜测,但由于 Chrome 扩展是基于 Javascript 的,因此它们可能存在跨域问题。当以编程方式尝试从另一个域获取样式表时,Chrome 将规则和 cssRules 设置为 null。
Just a guess, but since chrome extensions are Javascript based, they may have cross domain issues. Chrome sets the rules and cssRules to null when programmatically trying to get a stylesheet from another domain.
回答迟了,但我想我可以帮忙。访问受 COR 保护的外部表的 cssRules 的一种方法是使用 Yahoo 的 YQL 服务。我已将其合并到 Chrome 的开发人员工具扩展中,用于捕获页面片段的样式和标记。该扩展程序位于 Chrome 网页版存储并位于Github。
从 Github 获取源代码并查看 content.js 脚本以了解如何使用 YQL。基本上,您将对 YQL 进行 AJAX 调用,它会为您获取 CSS。您需要获取 CSS 内容并将其作为嵌入式样式标记注入到页面中,或者使用 JavaScript 解析 CSS(有一些用于此目的的库)。如果您选择将它们注入回文档中,请确保将新样式块设置为禁用,以免破坏页面的呈现。
扩展本身可能对您有用:
Answer is late, but I think I can help. One method of accessing the cssRules of external sheets protected by CORs is to use Yahoo's YQL service. I've incorporated it into a developer tools extension for Chrome for capturing styles and markup for a page fragment. The extension is in the Chrome Web Store and is on Github.
Grab the source from Github and look at the content.js script to see how YQL is used. Basically, you'll make an AJAX call to YQL and it will fetch the CSS for you. You'll need to take the CSS content and either inject it into the page as an embedded style tag or parse the CSS using JavaScript (there are some libraries for that purpose). If you choose to inject them back into the document, make sure to set the new style blocks to disabled so that you don't screw up the rendering of the page.
The extension itself might be useful to you: