热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从浏览器中获取原始CSS文件内容-FetchingrawCSSfilecontentsfromabrowser

IsthereanywaytofetchtherawcontentsofaCSSfile?有没有办法获取CSS文件的原始内容?LetsimaginethatIwa

Is there any way to fetch the raw contents of a CSS file?

有没有办法获取CSS文件的原始内容?

Lets imagine that I wanted to fetch any vendor-specific css properties from a CSS file. I would need to somehow grab the CSS contents and parse them accordingly. Or I could just use the DOM to access the rules of a CSS file.

让我们想象一下,我想从CSS文件中获取任何特定于供应商的css属性。我需要以某种方式获取CSS内容并相应地解析它们。或者我可以使用DOM来访问CSS文件的规则。

The problem is that in while using the DOM, mostly all browsers (except for <= IE8) tend to strip out all of the custom properties that do not relate to their browser engine (webkit strips out -moz and -o and -ms). Therefore it wouldn't be possible to fetch the CSS contents.

问题在于,在使用DOM时,大多数浏览器(除了<= IE8)都倾向于删除与浏览器引擎无关的所有自定义属性(webkit剥离-moz和-o和-ms) 。因此,无法获取CSS内容。

If I were to use AJAX to fetch the contents of the CSS file, then if that CSS file hosted on another domain, then the same origin policy would break and the CSS contents could not be fetched.

如果我使用AJAX来获取CSS文件的内容,那么如果该CSS文件托管在另一个域上,那么相同的源策略将会中断并且无法获取CSS内容。

If one were to use a cross-domain AJAX approach then there would only be a JSONP solution which wouldn't work since we're not parsing any Javascript code (therefore there is no callback).

如果一个人使用跨域AJAX方法,那么只有JSONP解决方案不起作用,因为我们没有解析任何Javascript代码(因此没有回调)。

Is there any other way to fetch the contents?

有没有其他方法来获取内容?

3 个解决方案

#1


1  

If a CSS file is on the same domain as the page you're running the script on, you can just use AJAX to pull in the CSS file:

如果CSS文件与您运行脚本的页面位于同一个域中,则可以使用AJAX来引入CSS文件:

$.get("/path/to/the.css", function(data) {/* ... */});

If not, you could try using Yahoo! Pipes as a proxy and get the CSS with JSONp.

如果没有,你可以尝试使用Yahoo!管道作为代理并使用JSONp获取CSS。

As for parsing, you can check out Sizzle to parse the selectors. You could also use the CSS grammar (posted in the CSS standards) to use a JS lex/yacc parser to parse out the document. I'll leave you to get creative with that.

至于解析,您可以查看Sizzle来解析选择器。您还可以使用CSS语法(在CSS标准中发布)来使用JS lex / yacc解析器来解析文档。我会让你发挥创造力。

Good luck!

#2


0  

No, you've pretty much covered it. Browsers other than IE strip out unknown rules from their object models both in the style/currentStyle objects and in the document.styleSheets interface. (It's usually IE6-7 whose CSS you want to patch up, of course.)

不,你已经覆盖了它。 IE以外的浏览器在style / currentStyle对象和document.styleSheets界面中从其对象模型中删除了未知规则。 (当然,它通常是IE6-7,你要修补它的CSS。)

If you wanted to suck a stylesheet from an external domain you would need proxy-assisted-AJAX. And parsing CSS from would be a big nasty job, especially if you needed to replicate browser quirks. I would strenuously avoid any such thing!

如果你想从外部域中吮吸样式表,你需要代理辅助的AJAX。从中解析CSS将是一项非常糟糕的工作,特别是如果您需要复制浏览器怪癖。我会竭力避免任何这样的事情!

#3


0  

JSONP is still a valid solution, though it would hurt the eyes somewhat. Basically, in addition to the callback padding, you would have to add one JSON property "padding" and pass the CSS as a value. For example, a call to a script, http://myserver.com/file2jsonp/?jsOnp=myCallback&textwrapper=cssContents could return this:

JSONP仍然是一个有效的解决方案,虽然它会有点伤害眼睛。基本上,除了回调填充之外,您还必须添加一个JSON属性“padding”并将CSS作为值传递。例如,调用脚本http://myserver.com/file2jsonp/?jsOnp=myCallback&textwrapper=cssContents可以返回:

myCallback("cssContents":"body{text-decoration:blink;}\nb{text-size:10em;}");

You'd have to text-encode all line breaks and wrap the contents of the CSS file in quotes (after encoding any existing quotes). I had to resort to doing this with a Twitter XML feed. It felt like such a horrible idea when I built it, but it did its job.

您必须对所有换行符进行文本编码,并将CSS文件的内容用引号括起来(在编码任何现有引号之后)。我不得不求助于使用Twitter XML feed。当我建造它时感觉这是一个可怕的想法,但它确实起了作用。


推荐阅读
author-avatar
手机用户2602938525
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有