触发 webViewDidFinishLoad for google 搜索结果

Trigger webViewDidFinishLoad for google search results

本文关键字:搜索结果 google for webViewDidFinishLoad 触发      更新时间:2023-09-26

我正在尝试在页面加载完成后运行代码 webViewDidFinishLoad.但是,如果我在UIWebView中访问 google.com 然后输入搜索,则第一个查询有效,但所有其他搜索都不会触发webViewDidFinishLoad。根据其他文章,这是因为谷歌使用ajax请求。为了解决这个问题,将javascript注入页面,以便在加载完成后触发iOS方法。但是,它似乎不适用于谷歌,但适用于雅虎等其他ajax网站。

我是否正确地执行了JavaScript?有没有人对如何在 ajax 加载完成后让 javascript 调用 Obj-C 方法有建议?

ajax_handler.js(与应用程序捆绑在一起)

var s_ajaxListener = new Object();
var originalReadyStateChange;
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
    console.log(this);
    if (this.readyState == 4) {
        console.log("TRIGGERING HANDLER");
        window.location='x3AjaxHandler://' + this.url;
        console.log(originalReadyStateChange);
    } else {
        console.log("READY STATE NOT FINISHED");
    }
    originalReadyStateChange();
};
XMLHttpRequest.prototype.open = function(a,b) {
    if (!a) var a='';
    if (!b) var b='';
    s_ajaxListener.tempOpen.apply(this, arguments);
    s_ajaxListener.method = a;
    s_ajaxListener.url = b;
    if (a.toLowerCase() == 'get') {
        s_ajaxListener.data = b.split('?');
        s_ajaxListener.data = s_ajaxListener.data[1];
    }
}
XMLHttpRequest.prototype.send = function(a,b) {
    if (!a) var a='';
    if (!b) var b='';
    s_ajaxListener.tempSend.apply(this, arguments);
    if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
    console.log(this);
    originalReadyStateChange = this.onreadystatechange;
    this.onreadystatechange = s_ajaxListener.callback;
}

网页视图

#define CocoaJSHandler          @"x3AjaxHandler"
static NSString *ajaxInjectionHandler;
+(void)initialize
{
    ajaxInjectionHandler = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"ajax_handler" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"js: %@", ajaxInjectionHandler);
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"SHOULD LOAD");
    if ([[[request URL] scheme] caseInsensitiveCompare:CocoaJSHandler] == NSOrderedSame) {
        NSString *requestedURLString = [[[request URL] absoluteString] substringFromIndex:[CocoaJSHandler length] + 3];
        NSLog(@"ajax request: %@", requestedURLString);
        [self webViewDidFinishLoad:webView];
        return NO;
    }
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"INJECTING");
    [webView stringByEvaluatingJavaScriptFromString:ajaxInjectionHandler];
}
您可以

尝试创建并删除 iFrame,并将 url 架构和路径作为src属性。设置window.location对我不起作用。到目前为止,这对我有用:

function sendRequestToiOS(url) {
    // Create and append an iFrame to the document and set it with the url schema
    var iframe = document.createElement("IFRAME");
    iframe.setAttribute("src", "x3AjaxHandler://" + url);
    // For some reason we need to set a non-empty size for the iOS6 simulator...
    iframe.setAttribute("height", "1px");
    iframe.setAttribute("width", "1px");
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;
}