我是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>
有人能够协助解决这个问题吗?
答案 0 :(得分: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);
}