-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aefe5ff
commit ea07907
Showing
7 changed files
with
184 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Alfred | ||
import WebKit | ||
|
||
/// Once the CSS in injected, the JS that did the injection will send | ||
/// a message with this name to the webview. See `injecterJS`. | ||
private let cssInjectedMessageName = "cssInjected" | ||
|
||
/// There's no way to directly inject CSS into a WKWebView. | ||
/// The only way is to inject a <style> tag into the document by executing | ||
/// JS in the webview. | ||
private func injecterJS(_ cssString: String) -> String { | ||
""" | ||
var style = document.createElement('style'); | ||
style.innerHTML = `\(cssString)`; | ||
document.head.appendChild(style); | ||
window.webkit.messageHandlers.\(cssInjectedMessageName).postMessage('done'); | ||
""" | ||
} | ||
|
||
class InjectedCSSWKWebView: WKWebView, WKScriptMessageHandler { | ||
// This is required because we're subclassing WKWebView, | ||
// and has nothing to do with the CSS injection. | ||
required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
} | ||
|
||
init( | ||
frame: CGRect, | ||
configuration: WKWebViewConfiguration, | ||
cssString: String | ||
) { | ||
log("will inject css: \(cssString)") | ||
|
||
let userScript = WKUserScript( | ||
source: injecterJS(cssString), | ||
injectionTime: .atDocumentEnd, | ||
forMainFrameOnly: true | ||
) | ||
|
||
let contentController = WKUserContentController() | ||
contentController.addUserScript(userScript) | ||
|
||
configuration.userContentController = contentController | ||
super.init(frame: frame, configuration: configuration) | ||
|
||
contentController.add(self, name: cssInjectedMessageName) | ||
} | ||
|
||
override func load(_ request: URLRequest) -> WKNavigation? { | ||
// we don't want the webview to be visible till the css is injected, and | ||
// has taken effect. | ||
self.isHidden = true | ||
return super.load(request) | ||
} | ||
|
||
override func loadFileURL( | ||
_ URL: URL, | ||
allowingReadAccessTo readAccessURL: URL | ||
) -> WKNavigation? { | ||
// we don't want the webview to be visible till the css is injected, and | ||
// has taken effect. | ||
self.isHidden = true | ||
return super.loadFileURL(URL, allowingReadAccessTo: readAccessURL) | ||
} | ||
|
||
func userContentController( | ||
_ userContentController: WKUserContentController, | ||
didReceive message: WKScriptMessage | ||
) { | ||
// Once the CSS is injected, make the webview visible. | ||
if message.name == cssInjectedMessageName { | ||
self.isHidden = false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.