Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to programmatically set the value of a WebKit password text field?
    text
    copied!<p><strong>Update: this question is bunk. Move along, nothing to see here. You <em>can</em> set the value of the password text field from either JS or ObjC. I was wrong.</strong></p> <p>I have a WebKit-based Cocoa app which loads an HTML document containing an HTML form in a <code>WebView</code>. The HTML form contains a password text field like:</p> <pre><code>&lt;form name="foo"&gt; &lt;input type="password" name="bar"&gt; &lt;/form&gt; </code></pre> <p>I'd like to set the value of this text field programmatically (from Objective-C if possible, but I'll do whatever works).</p> <p>I believe WebKit (and every other modern browser) implements a JavaScript security feature which prevents JS scripts from setting this value programmatically.</p> <p>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.</p> <p>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.</p> <p>Here's what I've tried in ObjC:</p> <pre><code>DOMHTMLDocument *doc = (DOMHTMLDocument *)[webView mainFrameDocument]; DOMHTMLFormElement *formEl = (DOMHTMLFormElement *)[[doc forms] namedItem:@"foo"]; DOMHTMLInputElement *inputEl = (DOMHTMLInputElement *)[[formEl elements] namedItem:@"bar"]; [inputEl setValue:@"baz"]; </code></pre> <p>this has no effect.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload