Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

at0m87

macrumors newbie
Original poster
Mar 2, 2012
17
0
Hi guys, i am trying to sieve out the username and text fields of a HTML Form loading in a web view. Previously i tried this method to narrow down the corresponding text fields that i need but it was tedious and there is no end to the computation of variables. even though i thought i listed out a lot of variables, it was still a hit and miss solution, not all website works.

Code:
NSString *loadUsrText = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                                     for (var i = inputFields.length >>> 0; i--;) { \
                                 if ( (inputFields[i].getAttribute('name') == 'email') || (inputFields[i].getAttribute('name') == 'Email') || (inputFields[i].getAttribute('name') == 'e-mail') || (inputFields[i].getAttribute('name') == 'E-mail') || (inputFields[i].getAttribute('name') == 'emailerr') || (inputFields[i].getAttribute('name') == 'UID') || (inputFields[i].getAttribute('name') == 'uid') || (inputFields[i].getAttribute('name') == 'username') || (inputFields[i].getAttribute('name') == 'Username') || (inputFields[i].getAttribute('name') == 'userName') || (inputFields[i].getAttribute('name') == 'user_name') || (inputFields[i].getAttribute('name') == 'User_Name') || (inputFields[i].getAttribute('name') == 'User_name') || (inputFields[i].getAttribute('name') == 'userid') || (inputFields[i].getAttribute('name') == 'userID') || (inputFields[i].getAttribute('name') == 'UserID') || (inputFields[i].getAttribute('name') == 'Userid') || (inputFields[i].getAttribute('name') == 'ID') || (inputFields[i].getAttribute('name') == 'id') || (inputFields[i].getAttribute('name') == 'j_username') || (inputFields[i].getAttribute('name') == 'session_key') || (inputFields[i].getAttribute('name') == 'login_password') || (inputFields[i].getAttribute('name') == 'liveid') || (inputFields[i].getAttribute('name') == 'Liveid') || (inputFields[i].getAttribute('name') == 'liveID') || (inputFields[i].getAttribute('name') == 'LiveID') || (inputFields[i].getAttribute('name') == 'liveID') || (inputFields[i].getAttribute('name') == 'LiveID') || (inputFields[i].getAttribute('name') == 'live_id') || (inputFields[i].getAttribute('name') == 'Live_id') || (inputFields[i].getAttribute('name') == 'live_ID') || (inputFields[i].getAttribute('name') == 'Live_ID') || (inputFields[i].getAttribute('name') == 'Live') || (inputFields[i].getAttribute('name') == 'live') || (inputFields[i].getAttribute('name') == 'live_email') || (inputFields[i].getAttribute('name') == 'Live_email') || (inputFields[i].getAttribute('name') == 'Live_Email')){ \
                                 inputFields[i].value = '%@';}}", myUsername];
[self.myWebView stringByEvaluatingJavaScriptFromString: loadUsrText];

Then i was told about RegularExpression but i wasn't very familiar with it and i read some tutorials about it but not specific to my project to try and get what it means and i piece out this code which is also a hit and miss solution. I am not even sure if i am doing it right but the code is definitely short this time for sure.

Code:
NSString *loadUsrText = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                                 for (var i = inputFields.length >>> 0; i--;) { \
                                    regStr = /(mail|user|iden|name|id|key|login|username|email)/i; \
                                    searchName = inputFields[i].getAttribute('name').search(regStr);\
                                    searchId = inputFields[i].getAttribute('id').search(regStr);\
                                    if (!(searchName == -1) || !(searchId == -1)){ \
                                        inputFields[i].value = '%@';}}", myUsername];
[self.myWebView stringByEvaluatingJavaScriptFromString: loadUsrText];

Can someone tell me if i have done this RegularExpression correctly? i really have no idea. Some sites it works, some it doesn't.
 
Some sites it works, some it doesn't.

For the ones it doesn't work, what are the input fields' name attributes? If they're not "mail" or "user" or "iden" or "name" or "id" or "key" or "login" or "username" or "email", which is what you appear to be searching for, they won't be matched.

I don't think you're going to be able to find a way to detect what you want across all sites, since there is no guaranteed consistency in how the HTML for these is constructed.
 
For the ones it doesn't work, what are the input fields' name attributes? If they're not "mail" or "user" or "iden" or "name" or "id" or "key" or "login" or "username" or "email", which is what you appear to be searching for, they won't be matched.

I don't think you're going to be able to find a way to detect what you want across all sites, since there is no guaranteed consistency in how the HTML for these is constructed.

Thank You dejo!

It happened that one of the site that didn't work, the name attribute was set as "name='email'" which was one of my search variables. That is what is puzzling me and made me feel that i may have some mistake in the code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.