gpt4 book ai didi

javascript - Objective C 检查网页 View 中是否存在元素

转载 作者:行者123 更新时间:2023-11-29 13:17:08 25 4
gpt4 key购买 nike

回答:这是您的操作方法,因为我无法使用以下任何解决方案。基本上,您必须使用 javascript,检查文档是否准备就绪,然后向 objective-c 返回一个值。简洁大方。在 objective-c 中,webviewdidfinishload:

        NSLog(@"Didload:Check if webview %d loading or loaded", X);
// The JS File
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[webView stringByEvaluatingJavaScriptFromString:jsString];
NSString *result1 = [webView stringByEvaluatingJavaScriptFromString:@"uiWebview_ReturnDocStatus()"];
NSLog(@"docread= %@", result1);
int RREsult = [result1 intValue];
if (RREsult == 0) {
NSLog(@"Didload: webview %d not yet loaded", X);
}
else if (RREsult == 1){
NSLog(@"Didload: webView %d loaded", X);
}

然后,在 javascript 中,在我的 UIWebViewSearch.js 文件中:

function uiWebview_ReturnDocStatus() {
alert ("check");
if(document.readyState == 'complete'){
return 1;
}
else {
return 0;
}
}

下面是我的代码。我试图确定某个元素是否存在于我的 WebView 中。我不想使用 document.body 等将整个 html 文本提取到 objective-c ,因为这会占用太多内存。我想要一些东西快点。但是下面的选项都不起作用。请帮忙。

NSString *oneString = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('moodlez_documentnotyetloaded').value"];
BOOL test = [webView stringByEvaluatingJavaScriptFromString:@"return !!document.getElementById('moodlez_documentnotyetloaded');"];

编辑1:

这是我的 objective-c 代码,后面是 javascript。这段代码在 didfinishload 中。

  if (1 ==1 ) {
//doesn't work
NSString *exists = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('moodlez_documentnotyetloaded');"];
NSLog(@"exists value=%d", [exists intValue]);

}
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[webView stringByEvaluatingJavaScriptFromString:jsString];
[webView stringByEvaluatingJavaScriptFromString:@"uiWebview_InjectJSToTellIfLoading()"];

.JS代码:

 function uiWebview_InjectJSToTellIfLoading() {
if(document.readyState == 'complete'){
var element = document.createElement('input');
element.setAttribute('type','hidden');
element.setAttribute('id','moodlez_documentloaded');
document.body.appendChild(element);

}
else {
if (1 == 1) {
var element = document.createElement('input');
element.setAttribute('type','hidden');
element.setAttribute('id','moodlez_documentnotyetloaded');
document.body.appendChild(element);
}

}

编辑 2:好的,有两种替代方法可以解决此问题。第一种是使用 javascript 从 webview 获取 html 代码并手动搜索 moodlez 标签。这种方法是不可取的,因为对于大型网页,它可能需要长达 1-2 秒的时间,但它确实有效。第二个是制作一个 javascript 函数来检查文档是否准备就绪,如果是,则修改一个 javascript 变量,然后可以从 objective-c 检查该变量。

最佳答案

这是我的 objective-c 代码,后面是 javascript。这段代码在 didfinishload 中。

  if (1 ==1 ) {
//doesn't work
NSString *exists = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('moodlez_documentnotyetloaded');"];
NSLog(@"exists value=%d", [exists intValue]);

}
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[webView stringByEvaluatingJavaScriptFromString:jsString];
[webView stringByEvaluatingJavaScriptFromString:@"uiWebview_InjectJSToTellIfLoading()"];

.JS代码:

function uiWebview_InjectJSToTellIfLoading() {
if(document.readyState == 'complete'){
var element = document.createElement('input');
element.setAttribute('type','hidden');
element.setAttribute('id','moodlez_documentloaded');
document.body.appendChild(element);

}
else {
if (1 == 1) {
var element = document.createElement('input');
element.setAttribute('type','hidden');
element.setAttribute('id','moodlez_documentnotyetloaded');
document.body.appendChild(element);
}
}

关于javascript - Objective C 检查网页 View 中是否存在元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15451674/

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