[NSURLProtocol registerClass:[MP3DetectURLProtocol class]];
#import <Foundation/Foundation.h>
@interface MP3DetectURLProtocol : NSURLProtocol
@property (nonatomic, strong) NSURLConnection *connection;
@end
#import "MP3DetectURLProtocol.h"
@implementation MP3DetectURLProtocol
static NSString *PBProxyURLHeader = @"X-PB";
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
//if([[request.URL absoluteString] rangeOfString:@"mp3"].length !=0)
NSLog(@"request url = %@", [request.URL absoluteString]);
if ([request valueForHTTPHeaderField:PBProxyURLHeader] == nil)
{
return YES;
}
return NO;
}
+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}
- (void) startLoading
{
NSMutableURLRequest *newRequest = [self.request mutableCopy];
[newRequest setValue:@"" forHTTPHeaderField:PBProxyURLHeader];
// Here we set the User Agent
//[newRequest setValue:@"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36 Kifi/1.0f" forHTTPHeaderField:@"User-Agent"];
//[NSURLProtocol setProperty:@YES forKey:@"UserAgentSet" inRequest:newRequest];
//NSLog(@"start loading");
self.connection = [NSURLConnection connectionWithRequest:newRequest delegate:self];
}
- (void)stopLoading
{
//[self.connection cancel];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.client URLProtocol:self didLoadData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self.client URLProtocol:self didFailWithError:error];
self.connection = nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response
{
[self.client URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self.client URLProtocolDidFinishLoading:self];
self.connection = nil;
}
@end
NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"var a = document.getElementsByTagName(\"a\");for(var i=0; i<a.length; i++){a[i].onclick = function(){alert(this.href);}}"];