gpt4 book ai didi

javascript - 为谷歌搜索结果触发 webViewDidFinishLoad

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:28 26 4
gpt4 key购买 nike

我正在尝试在我的 webViewDidFinishLoad 中完成加载页面时运行代码。但是,如果我在我的 UIWebView 中访问 google.com,然后输入搜索,第一个查询有效,但所有其他搜索都不会触发 webViewDidFinishLoad。根据其他文章,这是因为谷歌使用 ajax 请求。为了解决这个问题,将 javascript 注入(inject)页面以在加载完成后触发 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;
}

WebView

#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];
}

最佳答案

您可以尝试使用 url 架构和路径作为 src 属性来创建和删除 iFrame。设置 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;
}

关于javascript - 为谷歌搜索结果触发 webViewDidFinishLoad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22948303/

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