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>
有人能夠幫助這件事情?