gpt4 book ai didi

javascript - 自动填充 UIWebView

转载 作者:行者123 更新时间:2023-11-28 09:08:38 27 4
gpt4 key购买 nike

我环顾四周,发现了很多自动填充的代码,如下所示:

 - (void)webViewDidFinishLoad:(UIWebView *)webView
{
//For Authentication
NSString *savedUsername = @"Bapu";
NSString *savedPassword = @"Bapu123";

if (savedUsername.length != 0 && savedPassword.length != 0)
{
//create js strings
NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='password']\"); \
for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];

//autofill the form
[webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
[webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
}

这就是我的 .m 中的内容,但是,当我转到 webView 时,字段仍为空白。关于为什么它无法填充的任何线索?

如果有帮助的话,我要填写的网站是:sis.ocsarts.net,相关代码是:

input type="text" size="20" name="UserName" value=""  onkeydown="if (event.keyCode==13) { document.frmLogin.submit(); return false; };"

还有

input type="password" size="20" name="Password"  onkeydown="if (event.keyCode==13) { document.frmLogin.submit(); return false; };"

我的 FirstViewController.m 的完整代码是:

//
// FirstViewController.m
// OCSA
//
// Created by Jack Kearl on 4/1/13.
// Copyright (c) 2013 Jack Kearl. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize Aeries;

- (void)viewDidLoad
{
[super viewDidLoad];
[Aeries loadRequest:[NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://sis.ocsarts.net/login.asp"]]];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//For Authentication
NSString *savedUsername = @"Bapu";
NSString *savedPassword = @"Bapu123";

if (savedUsername.length != 0 && savedPassword.length != 0)
{
//create js strings
NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='password']\"); \
for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];

//autofill the form
[webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
[webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

您尝试访问的网站 http://sis.ocsarts.net包含框架 http://sis.ocsarts.net/login.asp

更改 Web View 以从该 login.asp 页面加载。

我通过使用Chrome开发者控制台发现了它,尝试在你提到的原始URL上执行javascript,只是发现获取输入元素的javascript函数总是返回空数组。我什至尝试了 getElementsByName、getElementsByTagName 等。

确保 webview delegate 已链接并尝试将 javascript 字符串合并为一个:

NSString *loadUsernameAndPasswordJS = [NSString stringWithFormat:@"document.getElementsByTagName('input')[1].value = '%@';document.getElementsByTagName('input')[2].value = '%@';", savedUsername, savedPassword];

关于javascript - 自动填充 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16579506/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com