开发者

Manipulating WebKit CSS data before loading?

开发者 https://www.devze.com 2022-12-15 15:29 出处:网络
Is it possible to manipulate CSS data before it is parsed in WebKit? I\'ve tried using the delegate method

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

I've tried using the delegate method

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

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

Can I manipulate the data in memory before it's parsed?


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:

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.

EDIT: 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.


As long as you can keep it under the 10MB limit, there is always stringByEvaluatingJavaScriptFromString and then do some loading of the CSS dynamically.

0

精彩评论

暂无评论...
验证码 换一张
取 消