美文网首页
WKWebView -- WKUserScript JS注入

WKWebView -- WKUserScript JS注入

作者: 夕儿77 | 来源:发表于2019-02-22 15:36 被阅读1次

WebKit 提供 WKUserScript


屏幕快照 2019-02-22 14.39.19.png

WKUserContentController方法

//WKUserContentController
- (void)addUserScript:(WKUserScript *)userScript;
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
- (void)removeScriptMessageHandlerForName:(NSString *)name;
- (void)addContentRuleList:(WKContentRuleList *)contentRuleList;
- (void)removeAllContentRuleLists;

WKUserScript方法

//WKUserScript
- (instancetype)initWithSource:(NSString *)source injectionTime:
(WKUserScriptInjectionTime)injectionTime forMainFrameOnly:
(BOOL)forMainFrameOnly
{
/
* source  脚本的源代码。
* injectionTime  脚本应该注入网页的时间。
* forMainFrameOnly 一个布尔值,指示脚本是仅应注入主框架(YES)还是注入所有框架(NO)。
/
}

应用

    WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
    config.userContentController = [[WKUserContentController alloc] init];
    WKWebView* webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    WKUserScript* userScript = [[WKUserScript alloc] initWithSource:@"脚本" 
                                                 injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                                   forMainFrameOnly:YES];
   [ config.userContentController addUserScript:userScript];

在 WK 中,默认是没有弹出框的,如果你需要设置弹出框 代理方法

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
    UIAlertController* ac = [UIAlertController alertControllerWithTitle:webView.title
                                                                    message:message
                                                             preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action)
    {
        completionHandler();
    }];
    [ac addAction:cancelAction];
    [self presentViewController:ac animated:YES completion:NULL];
}

- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler
{
    UIAlertController* ac = [UIAlertController alertControllerWithTitle:webView.title
                                                                message:message
                                                         preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* confirmAction = [UIAlertAction actionWithTitle:@"确定"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action)
    {
        completionHandler(true);
    }];
    [ac addAction:confirmAction];

    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action)
    {
        completionHandler(false);
    }];
    [ac addAction:cancelAction];
    [self presentViewController:ac animated:YES completion:NULL];
}

- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler
{
    UIAlertController* ac = [UIAlertController alertControllerWithTitle:webView.title
                                                                message:prompt
                                                         preferredStyle:UIAlertControllerStyleAlert];
    
    [ac addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.text = defaultText;
    }];
    
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action)
                                   {
                                       completionHandler(false);
                                   }];
    [ac addAction:cancelAction];

    UIAlertAction* confirmAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * _Nonnull action)
                                    {
                                        completionHandler(ac.textFields.firstObject.text);
                                    }];
    [ac addAction:confirmAction];
    
    [self presentViewController:ac animated:YES completion:NULL];
}

以上看大家博客借鉴总结,欢迎大家批评指正补充 ,我是一个小码农

相关文章

网友评论

      本文标题:WKWebView -- WKUserScript JS注入

      本文链接:https://www.haomeiwen.com/subject/heleyqtx.html