【问题标题】:Get text field info out of loaded webpage - Mac OS X Development从加载的网页中获取文本字段信息 - Mac OS X 开发
【发布时间】:2009-06-08 14:51:14
【问题描述】:

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

有人可以帮忙解决这个问题吗?

【问题讨论】:

    标签: objective-c cocoa macos webview


    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-07
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多