gpt4 book ai didi

objective-c - 从加载的网页中获取文本字段信息 - Mac OS X 开发

转载 作者:搜寻专家 更新时间:2023-10-30 20:27:42 25 4
gpt4 key购买 nike

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

有人能协助解决这个问题吗?

最佳答案

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

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

关于objective-c - 从加载的网页中获取文本字段信息 - Mac OS X 开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/965265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com