3

我是Mac世界的新手。

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

我的代码如下:

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

任何人都可以在这件事上提供帮助?

4

1 回答 1

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);
}
于 2009-06-09T14:56:33.207 回答