// UIWebViewで検索を実現する
// http://www.icab.de/blog/2010/01/12/search-and-highlight-text-in-uiwebview/

#import "SearchWebView.h"

@implementation UIWebView (SearchWebView)

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"SearchWebView" ofType:@"js"];
	nslog(@"path=%@",path);
	
	// 検索処理の登録
    NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
	nslog(@"jsCode=%@",jsCode);
	//jsCode =
	[self stringByEvaluatingJavaScriptFromString:jsCode];
	nslog(@"ret=%@",jsCode);

	// 検索文字列の登録
    NSString *startSearch = [NSString stringWithFormat:@"MyApp_HighlightAllOccurencesOfString('%@')",str];
	nslog(@"startSearch=%@",startSearch);
	//jsCode =
	[self stringByEvaluatingJavaScriptFromString:startSearch];
	nslog(@"ret=%@",jsCode);
	
	// 結果の呼び出し
    NSString *result =	[self stringByEvaluatingJavaScriptFromString:@"MyApp_SearchResultCount"];
	
	nslog(@"result=%@",result);
    return [result integerValue];
}

- (void)removeAllHighlights
{
    [self stringByEvaluatingJavaScriptFromString:@"MyApp_RemoveAllHighlights()"];
}

@end
