Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BollywooD

macrumors 6502
Original poster
Apr 27, 2005
372
46
Hamburg
I would like to use a CSS style sheet, in Safari. How can I set the path for this in Safari programatically.

ie not through, Safari Preferences.


I am currently applying a CSS file via javascript, but applying the CSS directly would remove the javascript overhead.

I ran a class-dump of Safari, and found the following methods:
Code:
- (void)setStyleSheetPathPreference:(id)arg1;
- (id)styleSheetPathPreference;
- (void)updateStyleSheetMenu;
- (void)takeStyleSheetFromMenuItem:(id)arg1;
- (void)disableUserStyleSheet:(id)arg1;
- (void)choseStyleSheetFromPanel:(id)arg1 returnCode:(int)arg2 contextInfo:(void *)arg3;
- (void)chooseStyleSheetFromFileBrowser:(id)arg1;

anyone, care to take an educated guess, as to which one I should swizzle...

or better yet, is there an official way to do this?
:confused:
 
Ive come up with the following, but are not quite sure how to apply it to the current webView:

Code:
NSString *applicationSupportFolder = [applicationSupportFolderPath stringByExpandingTildeInPath];
 
	NSString *CSSFilePath = [applicationSupportFolder stringByAppendingPathComponent:CSSFile];
	CSSFilePath = [CSSFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSURL *CSSFileURL = [NSURL URLWithString:CSSFilePath];
 
	WebPreferences*	preferences	= [WebPreferences standardPreferences];
	[preferences setUserStyleSheetEnabled:YES];
	[preferences setUserStyleSheetLocation:CSSFileURL];
 
I am now using the following:

Code:
WebView* webView = [frame webView];
	
    DOMDocument* domDocument=[webView mainFrameDocument];
	DOMElement* styleElement=[domDocument createElement:@"style"];
	
	[styleElement setAttribute:@"type" value:@"text/css"];
	DOMText* cssText=[domDocument createTextNode:myCSSString];
	[styleElement appendChild:cssText];
	DOMElement* headElement=(DOMElement*)[[domDocument getElementsByTagName:@"head"] item:0];
	[headElement appendChild:styleElement];

applying it dynamically, using:
Code:
+ (void) progressStarted: (NSNotification*) aNotification
{	
	WebView* webView = [aNotification object];
}

+ (void) progressFinished:(NSNotification *) aNotification
{	
	WebView* webView = [aNotification object];
}

but its not very user friendly, as its not applied till the page has finished downloading.....
:confused:
 
ok so it was a simple oversight:

I needed to add this line:
CSSFilePath = [NSString stringWithFormat:mad:"file://%@", CSSFilePath];

Code:
 NSString *applicationSupportFolder = [applicationSupportFolderPath stringByExpandingTildeInPath];
 
	NSString *CSSFilePath = [applicationSupportFolder stringByAppendingPathComponent:CSSFile];
	CSSFilePath = [CSSFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
CSSFilePath = [NSString stringWithFormat:@"file://%@", CSSFilePath];
	NSURL *CSSFileURL = [NSURL URLWithString:CSSFilePath];

 
	WebPreferences*	preferences	= [WebPreferences standardPreferences];
	[preferences setUserStyleSheetEnabled:YES];
	[preferences setUserStyleSheetLocation:CSSFileURL];


but how can I force safari to use the updated style sheet, when the file is changed?

I also would like to apply the stylesheet on a page by page basis...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.