webview js 注入

来源:互联网 发布:淘宝怎么看行情数据 编辑:程序博客网 时间:2024/05/16 09:22
- (void)addSetPayPwdListener:(UIWebView *)webView{
    NSArray *valueArray = [valueDictionary objectForKey:@"setPayPwd"];
    
    NSString *pwdId1 = valueArray[0];
    NSString *pwdId2 = valueArray[1];
    NSString *submitButtonId = valueArray[2];
    
    NSMutableString *setPayPwdJsHtml = [NSMutableString new];
    [setPayPwdJsHtml appendString:@"var btn = document.getElementById('"];
    [setPayPwdJsHtml appendString:submitButtonId];
    [setPayPwdJsHtml appendString:@"');"];
    [setPayPwdJsHtml appendString:@" btn.onmousemove = function showMessage() {"];
    [setPayPwdJsHtml appendString:@" transformParam("];
    [setPayPwdJsHtml appendFormat:@"document.getElementById('%@').value,",pwdId1];
    [setPayPwdJsHtml appendFormat:@"document.getElementById('%@').value)",pwdId2];
    [setPayPwdJsHtml appendString:@" };"];
    
    DLog(@"%@",setPayPwdJsHtml);
    JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //js调用OC
    context[@"transformParam"] = ^(){
        NSArray *args = [JSContext currentArguments];
        if (args.count == 2) {
            JSValue *firstValue = [args objectAtIndex:0];
            JSValue *secondValue = [args objectAtIndex:1];
            if ([firstValue.toString isEqualToString:secondValue.toString]) {
                [self sendPayPassword:[firstValue toString]];
            }
        }
    };
    [context evaluateScript:setPayPwdJsHtml];
}
原创粉丝点击