Skip to content

Instantly share code, notes, and snippets.

@wayne5540
Created January 20, 2017 02:14
Show Gist options
  • Save wayne5540/171874d820262fb2db3465cb538a6a16 to your computer and use it in GitHub Desktop.
Save wayne5540/171874d820262fb2db3465cb538a6a16 to your computer and use it in GitHub Desktop.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
let contentController = WKUserContentController()
let js: String = "var h1s = document.querySelectorAll('h1'); for (var i = 0; i < h1s.length; i++) { h1s[i].style.color = 'red' };"
let userScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: false)
contentController.addUserScript(userScript)
webConfiguration.userContentController = contentController
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "http://localhost:3000")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment