Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKUserScript not working

I want to inject a script using WKWebview API. For some reason it isn't working and I can't figure it out. I've tried debugging in Safari developer console and I can't find the JavaScript code in there either.

Implementation Code as follows:

NSString *js = @"document.body.style.background = \"#FF0000\";";

    NSString *myScriptSource = @"alert('Hello, World!')";


    WKUserScript *s = [[WKUserScript alloc] initWithSource:myScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
    WKUserContentController *c = [[WKUserContentController alloc] init];
    [c addUserScript:s];

    WKWebViewConfiguration *conf = [[WKWebViewConfiguration alloc] init];
    conf.userContentController = c;

    WKWebView *webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:conf];
    [self.view addSubview:webview];
    webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // Do any additional setup after loading the view, typically from a nib.
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
        [webview loadRequest:request];
    });
like image 632
Avner Barr Avatar asked Oct 05 '14 13:10

Avner Barr


People also ask

What is WKUserScript?

A script that the web view injects into a webpage.

What is WKUserContentController?

Overview. A WKUserContentController object provides a bridge between your app and the JavaScript code running in the web view. Use this object to do the following: Inject JavaScript code into webpages running in your web view. Install custom JavaScript functions that call through to your app's native code.

How do I run JavaScript in WKWebView?

Inject JavaScript to WKWebView with WKUserScript viewDidLoad() let config = WKWebViewConfiguration() let js = getMyJavaScript() let script = WKUserScript(source: js, injectionTime: . atDocumentEnd, forMainFrameOnly: false) config. userContentController. addUserScript(script) config.


Video Answer


1 Answers

alert isn't implemented in WKWebView by default, so even if your user script runs it won't visibly do anything. You need to implement runJavaScriptAlertPanelWithMessage:

func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (() -> Void)) {
  let alert = UIAlertController.create(title: frame.request.url?.host, message: message, preferredStyle: .alert)
  alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
    completionHandler()
  }))
  present(alert, animated: true, completion: nil)
}

I don't think the injected JavaScript actually appears in the DOM anywhere, it's a property internal to the WKWebView.

like image 83
BonzaiThePenguin Avatar answered Oct 13 '22 20:10

BonzaiThePenguin