Update: this question is bunk. Move along, nothing to see here. You can set the value of the password text field from either JS or ObjC. I was wrong.
I have a WebKit-based Cocoa app which loads an HTML document containing an HTML form in a WebView. The HTML form contains a password text field like:
<form name="foo">
<input type="password" name="bar">
</form>
I’d like to set the value of this text field programmatically (from Objective-C if possible, but I’ll do whatever works).
I believe WebKit (and every other modern browser) implements a JavaScript security feature which prevents JS scripts from setting this value programmatically.
Unfortunately, it seems that the same security restriction applies to Objective-C, as I can’t seem to set the value using ObjC either. While the JS restriction is reasonable, the ObjC seems a bit unreasonable.
Is there any way to programmatically set the value of this field (short of bundling a custom WebKit in my app that has been altered to allow this)? I’m open to any suggestion.
Here’s what I’ve tried in ObjC:
DOMHTMLDocument *doc = (DOMHTMLDocument *)[webView mainFrameDocument];
DOMHTMLFormElement *formEl = (DOMHTMLFormElement *)[[doc forms] namedItem:@"foo"];
DOMHTMLInputElement *inputEl = (DOMHTMLInputElement *)[[formEl elements] namedItem:@"bar"];
[inputEl setValue:@"baz"];
this has no effect.
This should definitely work without having to jump through any hoops to do so. I just wrote a quick test app that loaded http://mail.google.com/ and signed into it fine with the following code on SnowLeopard and Leopard:
I also have very similar code at work whose only job is to sign into website and its been working fine on Tiger for a year. In that case I do everything, including creating the WebView via code. I would also be very surprised if Safari’s autofill didn’t use very similar code to this.
That said, I would make sure your app isn’t doing anything crazy to the WebView. Do you do any other modification the DOM anywhere else? Try doing this in a sample app like I did and see if it works there.
Is the HTML you’re loading your own or on a remote server? Some websites include Javascript that clears out form fields on load or a second or two after to prevent autofill. Perhaps you’re running into that?
If all else fails, make sure nothing is being logged to the WebView’s console. You can see that by opening up the web inspector or temporarily implementing the private WebUIDelegate method: