触发 webViewDidFinishLoad for google 搜索结果
Trigger webViewDidFinishLoad for google search results
我正在尝试在页面加载完成后运行代码 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;
}
相关文章:
- AngularJS/HTML/Bootstrap元素用于动态搜索结果
- 在web应用程序中的新搜索中重新加载搜索结果(不带jQuery)
- 检查搜索结果是否存在多次如果是,则在Javascript中只显示一个结果
- 显示可链接的搜索结果+对齐方式
- 谷歌位置服务附近搜索结果基于正确的地图中心的位置
- 为自定义网站创建JavaScript搜索框,创建显示搜索结果的弹出窗口
- 主干,如何记住搜索结果
- 为什么不'我的扩展程序不会出现在Chrome网上商店的搜索结果中
- 如何从谷歌自定义搜索下载搜索结果
- (临时)在Web应用程序中存储JSON搜索结果
- Angular JS根据搜索结果和点击事件更新DOM元素
- 当至少有20个搜索结果时,只呈现10个搜索结果?使用Google Places API
- (html) Google 地图搜索结果与 Maps/地点 API 调用之间的差异(结果较少)
- 如何在javascript中获取Google搜索结果的“真实URL”
- 谷歌地图Api附近搜索-为什么Google.Maps.places.RankBy.DISTANCE返回的结果更少
- Google电子表格——在使用ImportXML()将Google搜索结果导入单元格后,转换单元格值,Google将ur
- 如何收集10个Google搜索结果链接(其中似乎删除了class属性)
- 将Google Maps搜索结果(商店定位器)显示为无序列表,
- 从Google搜索结果URL获取搜索查询
- 触发 webViewDidFinishLoad for google 搜索结果