Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Finding and auto-filling HTML login forms in a UIWebView using JavaScript
- document.getElementsByTagName('INPUT')[0].value
- document.getElementsByTagName('INPUT')[1].value
- document.getElementsById('id_name').value
- <input type="text" class="inputtext" name="email" id="email" value="" tabindex="1" />
- <input type="password" class="inputtext" name="pass" id="pass" tabindex="2" />
- <input id="ap_email" name="email" value="" type="email" size="30" maxlength="128" tabindex="1" autocorrect="off" autocapitalize="off" />
- <input id="ap_password" name="password" type="password" maxlength="1024" size="20" tabindex="2" onkeypress="displayCapsWarning(event,'ap_caps_warning', this);" class="password"/>
- <input type="text" tabindex="1" maxlength="20" size="32" name="UID" id="UID">
- <input type="password" onkeyup="keyUp(event)" onkeydown="return onlyNumerics(event)" tabindex="2" maxlength="9" size="32" name="PIN" id="PIN" autocomplete="off">
- <input type="text" spellcheck="false" name="Email" id="Email" value="">
- <input type="password" name="Passwd" id="Passwd">
- document.querySelectorAll("input[type='password']")
- document.querySelectorAll("input[type='text']")
- - (void)webViewDidFinishLoad:(UIWebView *)webView {
- NSString *savedUsername = @"peter";
- NSString *savedPassword = @"Pan123";
- 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:@"document.querySelectorAll("input[type='password']").value ='%@'", savedPassword];
- //autofill the form
- [self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
- [self.webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
- }
- }
- // Get the HTML from the UIWebView
- NSMutableString *html = [[[self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML;"] mutableCopy] autorelease];
- // Find the range of the first input element.
- NSRange firstInputRange = [html rangeOfString:@"<input" options:NSCaseInsensitiveSearch];
- // Check if it was found...
- if (firstInputRange.location != NSNotFound) {
- // Add replace with the populated value attribute appended.
- [html replaceCharactersInRange:firstInputRange withString:@"<input value='username'"];
- // Now do the same for the second input range, checking the html after the first input.
- NSRange secondInputRange = [html rangeOfString:@"<input" options:NSCaseInsensitiveSearch range:NSMakeRange(firstInputRange.location+firstInputRange.length, html.length - firstInputRange.location - firstInputRange.length)];
- // And if found, append the populated value attribute.
- if (secondInputRange.location != NSNotFound) {
- [html replaceCharactersInRange:secondInputRange withString:@"<input value='password'"];
- }
- }
- // Finally, load the amended HTML back into the UIWebView/
- [self.webView loadHTMLString:html baseURL:nil];
Add Comment
Please, Sign In to add comment