나는 내 자신의 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를 설정하는 것이 잘못되었다고 말할 수 있습니까?