在加载之前处理WebKit CSS数据? [英] Manipulating WebKit CSS data before loading?

查看:160
本文介绍了在加载之前处理WebKit CSS数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在WebKit中解析之前操作CSS数据?

Is it possible to manipulate CSS data before it is parsed in WebKit?

我尝试使用委托方法

- (NSURLRequest *)webView:(WebView *)sender
                 resource:(id)identifier
          willSendRequest:(NSURLRequest *)request
         redirectResponse:(NSURLResponse *)redirectResponse
           fromDataSource:(WebDataSource *)dataSource

可以通过写下我的操作CSS,然后返回一个替代NSURLRequest指向它的工作。这个工作正常,除了DOM树,然后包含操纵的URLRequest的路径,而不是原始的,这是我的应用程序的坏消息。

which I can make work by writing down my manipulated CSS, then returning an alternative NSURLRequest that points to it. This works fine, apart from the DOM tree then contains the path to the manipulated URLRequest, not the original, which is bad news for my app.

我可以操纵数据

推荐答案

只要加载一个用户样式表来覆盖你想要的CSS(这就是级联的CSS)。这是WebKit的核心功能。

Just load a user style sheet to override whatever CSS you want (that's the "cascading" part of CSS). This is a core feature of WebKit.

WebPreferences setUserStyleSheetEnabled:

例如,在实践中,您可以查看PandoraBoy的 PlayerController 它有钩子(虽然我收拾不需要实际使用它们)。看看 -awakeFromNib

For an example of this in practice, you can look at PandoraBoy's PlayerController which has hooks for it (though I wound up not needing to actually use them). Look in -awakeFromNib.

编辑:如果你真的需要替换你自己的CSS文件,方式是完全透明的WebKit,这是可能的,但它有点更多的工作,更混乱,一般不应该需要。也就是说,PandoraBoy确实遇到了这个问题,并有一个解决方案。查看 ResourceURLProtocol

If you really need to substitute your own CSS file for the original in a way that's totally transparent to WebKit, that's possible but it's a bit more work, more confusing, and generally shouldn't be needed. That said, PandoraBoy did run into this problem, and there's a solution. Look at ResourceURLProtocol.

这篇关于在加载之前处理WebKit CSS数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆