2009-06-08 53 views
3

我是Mac世界的新手。从加载的网页获取文本字段信息 - Mac OS X Development

我需要创建一个应用程序,能够从文本字段中提取在网页上输入的信息。我的应用程序会加载一个托管在某个地方的网页,并且在网页中会有一系列文本字段和一个提交按钮。点击按钮后,我必须能够读取输入到此网页文本字段的信息。

我的代码如下:

+ (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); 
} 

当我运行应用程序,并且它得到NSRunAlertPanel,它不希望进一步执行。

当我注释3个DOM行时,NSRunAlertPanel显示它的消息,我可以继续。

的HTML看起来像这样:

<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> 

有人能够帮助这件事情?

回答

1

我找到了解决我的问题的方法。答案是下面的代码更改:

- (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); 
}