WKUserScript не работает

Я хочу внедрить скрипт, используя WKWebview API. Почему-то не работает, не могу понять. Я пытался выполнить отладку в консоли разработчика Safari и не могу найти там код JavaScript.

Код реализации следующим образом:

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];
    });

person Avner Barr    schedule 05.10.2014    source источник
comment
Итак, вы нашли ответ на свой вопрос?   -  person Bastian    schedule 21.01.2016
comment
@ Бастиан, можешь попробовать мое решение, если это тебе поможет   -  person Anurag Soni    schedule 17.05.2017


Ответы (2)


alert не реализован в WKWebView по умолчанию, поэтому даже если ваш пользовательский скрипт запустится, он ничего не сделает. Вам нужно реализовать 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)
}

Я не думаю, что внедренный JavaScript действительно где-либо появляется в DOM, это внутреннее свойство WKWebView.

person BonzaiThePenguin    schedule 14.12.2017

Пожалуйста, используйте код, подобный этому, и добавьте обработчик сообщений сценария и установите делегата навигации.

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];
// Add a script message handler for receiving  "buttonClicked" event notifications posted from the JS document using window.webkit.messageHandlers.buttonClicked.postMessage script message
    [c addScriptMessageHandler:self name:@"buttonClicked"];

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];
});

реализовать обработчик сообщений сценария "WKScriptMessageHandler" с именем метода

#pragma mark -WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"buttonClicked"]) {
    self.buttonClicked ++;
}

// JS objects are automatically mapped to ObjC objects
id messageBody = message.body;
if ([messageBody isKindOfClass:[NSDictionary class]]) {
    NSString* idOfTappedButton = messageBody[@"ButtonId"];
    [self updateColorOfButtonWithId:idOfTappedButton];
}

}

и опубликуйте форму сообщения js, как это

var button = document.getElementById("clickMeButton");
button.addEventListener("click", function() {
        var messgeToPost = {'ButtonId':'clickMeButton'};
        window.webkit.messageHandlers.buttonClicked.postMessage(messgeToPost);
    },false);

вам перезвонят

person Anurag Soni    schedule 28.02.2017
comment
@mrjimoy_05 mrjimoy_05, что вы пробовали, это реализовано и протестировано, и этот ответ касается обработчика сообщений скрипта, вы выполняете js, я проверил ваш код по вашему вопросу - person Anurag Soni; 24.05.2017