Safari CSS file

Discussion in 'Mac Programming' started by BollywooD, Mar 25, 2010.

  1. BollywooD macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #1
    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:
     
  2. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #2
    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];
     
  3. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #3
    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:
     
  4. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #4
    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...
     

Share This Page