views:

347

answers:

1

Hi,

I am a newbie in the Mac world.

I need to create an app that is able to extract information entered on a web page, from text fields. My app will load a webpage hosted somewhere, and within the webpage there will be a a series of text fields and a submit button. Once the button is clicked, I must be able to read the information entered into the text fields of this webpage.

I have code as follows:

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
{
// For security, you must explicitly allow a selector to be called from JavaScript.

if (aSelector == @selector(showMessage:)) {
    return NO; // i.e. showMessage: is NOT _excluded_ from scripting, so it can be called.
}

return YES; // disallow everything else
}

- (void)showMessage:(NSString *)message
{
    // This method is called from the JavaScript "onClick" handler of the INPUT element 
    // in the HTML. This shows you how to have HTML form elements call Cocoa methods.

  DOMDocument *myDOMDocument = [[webView mainFrame] DOMDocument];     // 3
  DOMElement *contentTitle = [myDOMDocument getElementById:@"TexTest"];   // DOM
  message = [[contentTitle firstChild] nodeValue];                                            // lines

  NSRunAlertPanel(@"Message from JavaScript", message, nil, nil, nil);
}

When I run the app, and it gets to NSRunAlertPanel, it does not want to execute further.

When I comment the 3 DOM lines out, the NSRunAlertPanel shows its message and I can continue.

The HTML looks like this:

<body> 
  <h1 id="contentTitle">Some kind of title</h1>
  <div id="main_content"> 
    <p>Some content</p> 
    <p>Some more content</p> 
  </div> 
  <div>
    <input id="TexTest" value=" " type="text">
  </div> 
  <div>
    <input id="message_button" value="Show Message" onclick="window.AppController.showMessage_('Hello there...');" type="button">
  </div>
</body>

Anybody able to assist in this matter?

+1  A: 

I have found a solution for my problem. The answer is the following code change:

- (void)showMessage:(NSString *)message
{
    // This method is called from the JavaScript "onClick" handler of the INPUT element 
    // in the HTML. This shows you how to have HTML form elements call Cocoa methods.

  DOMHTMLDocument *myDOMDocument = [[webView mainFrame] DOMDocument];
  DOMHTMLElement *contentTitle = [myDOMDocument getElementById:@"TexTest"];
  message = [contentTitle value];

  NSRunAlertPanel(@"Message from JavaScript", message, nil, nil, nil);
}
wdicks