2011-08-28 7 views
9

나는 내 자신의 CSS를 사용하는 작은 브라우저를 갖고 싶습니다. 문제는 CSS가로드되지 않았거나로드되지만 효과가없는 것입니다.PyObjC를 사용하여 WebKit WebView에서 사용자 CSS를로드하는 방법은 무엇입니까?

import Foundation 
import WebKit 
import AppKit 
import objc 

def main(): 
    app = AppKit.NSApplication.sharedApplication() 
    rect = Foundation.NSMakeRect(100,350,600,800) 
    win = AppKit.NSWindow.alloc() 
    win.initWithContentRect_styleMask_backing_defer_(rect, AppKit.NSTitledWindowMask | AppKit.NSClosableWindowMask | AppKit.NSResizableWindowMask | AppKit.NSMiniaturizableWindowMask, AppKit.NSBackingStoreBuffered, False) 
    win.display() 
    win.orderFrontRegardless() 

    webview = WebKit.WebView.alloc() 
    webview.initWithFrame_(rect) 

    webview.preferences().setUserStyleSheetEnabled_(objc.YES) 
    print webview.preferences().userStyleSheetEnabled() 
    cssurl = Foundation.NSURL.URLWithString_("http://dev.stanpol.ru/user.css") 
    webview.preferences().setUserStyleSheetLocation_(cssurl) 
    print webview.preferences().userStyleSheetLocation() 

    pageurl = Foundation.NSURL.URLWithString_("http://dev.stanpol.ru/index.html") 
    req = Foundation.NSURLRequest.requestWithURL_(pageurl) 
    webview.mainFrame().loadRequest_(req) 

    win.setContentView_(webview) 
    app.run() 

if __name__ == '__main__': 
    main() 

코드는 오류없이 실행 :

여기에 전체 코드 (I는 인터페이스 빌더를 사용하지 않는)입니다. 인쇄물

True 
http://dev.stanpol.ru/user.css 

하지만 내 CSS가 WebView에 영향을주지 않습니다.

pageurl = Foundation.NSURL.URLWithString_("http://dev.stanpol.ru/index.html") 
req = Foundation.NSURLRequest.requestWithURL_(pageurl) 
webview.mainFrame().loadRequest_(req) 

dom = webview.mainFrame().DOMDocument() 
link = dom.createElement_("link") 
link.setAttribute_value_("rel", "StyleSheet") 
link.setAttribute_value_("type", "text/css") 
link.setAttribute_value_("href", "http://dev.stanpol.ru/user.css") 

head = dom.getElementsByTagName_(u"head") 
hf = head.item_(0) 
hf.appendChild_(link) 

을하지만 어느 경우에 작동하지 않습니다 :

은 내가 DOM에 대한 링크를 추가하는 등 다양한 솔루션을 시도했다.

CSS를로드하는 데 javascript를 사용하고 싶지 않습니다.

아무도 내 코드에서 사용자 CSS를 설정하는 것이 잘못되었다고 말할 수 있습니까?

답변

3

Apple은 setUserStyleSheetLocation이 로컬 경로 또는 data : URL 일 수 있음을 올바르게 문서화하지 않습니다. Qt는 QtWebKit에서 해당 설정을 자신의 문서에이 설명 않습니다

http://doc.qt.nokia.com/latest/qwebsettings.html#setUserStyleSheetUrl

은 모든 웹 페이지를로드하는 사용자 스타일 시트의 위치를 ​​지정합니다.

로컬 파일 시스템에 대한 경로 또는 같은 UTF-8, Base64로 인코딩 된 데이터를 가진 데이터 URL이어야 위치 :

"데이터 : 텍스트/CSS; 문자셋 = UTF-8; base64, cCB7IGJhY2tncm91bmQtY29sb3I6IHJlZCB9Ow == "

참고 : base64 데이터가 유효하지 않은 경우 스타일이 적용되지 않습니다.