Reputation: 47
I have my app registering a highlighted word when I search in a html file through a UIWebview. The issue I have is the program is just highlighting the selected words. Since I have several pages, how do I make it so the program will automatically go to where the words are highlighted instead of me scrolling around, searching for the highlighted word. Here is my code so far. Thank you
function uiWebview_HighlightAllOccurencesOfStringForElement(element,keyword) {
if (element) {
if (element.nodeType == 3) { // Text node
while (true) {
//if (counter < 1) {
var value = element.nodeValue; // Search for keyword in text node
var idx = value.toLowerCase().indexOf(keyword);
if (idx < 0) break; // not found, abort
//(value.split);
//we create a SPAN element for every parts of matched keywords
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(text);
span.setAttribute("class","uiWebviewHighlight");
span.style.backgroundColor="yellow";
span.style.color="black";
uiWebview_SearchResultCount++; // update the counter
text = document.createTextNode(value.substr(idx+keyword.length));
element.deleteData(idx, value.length - idx);
var next = element.nextSibling;
element.parentNode.insertBefore(span, next);
element.parentNode.insertBefore(text, next);
element = text;
}
} else if (element.nodeType == 1) { // Element node
if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
for (var i=element.childNodes.length-1; i>=0; i--) {
uiWebview_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
}
}
}
}
}
Upvotes: 0
Views: 66
Reputation: 46
// i used that code For Next and Previous Functionality And it works fine // function
uiWebview_HighlightAllOccurencesOfNextStringForElement(element,keyword) {
if (element) {
if (element.nodeType == 3) { // Text node
while (true) {
//if (counter < 1) {
var value = element.nodeValue; // Search for keyword in text node
var idx = value.toLowerCase().indexOf(keyword);
if (idx < 0) break; // not found, abort
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(text);
span.setAttribute("class","MyAppHighlight");
text = document.createTextNode(value.substr(idx+keyword.length));
element.deleteData(idx,value.length-idx);
var next = element.nextSibling;
element.parentNode.insertBefore(span,next);
element.parentNode.insertBefore(text,next);
element = text;
span.scrollIntoView();
span.style.backgroundColor = "yellow";
span.style.color = "black";
a.push(span);
uiWebview_SearchResultCount++;
}
} else if (element.nodeType == 1) { // Element node
if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
for (var i=element.childNodes.length-1; i>=0; i--) {
uiWebview_HighlightAllOccurencesOfNextStringForElement(element.childNodes[i],keyword);
}
}
}
}
}
// the main entry point to start the search
function uiWebview_HighlightAllOccurencesOfNextString(keyword)
`enter code here`{
uiWebview_RemoveAllHighlights();
uiWebview_HighlightAllOccurencesOfNextStringForElement(document.body, keyword.toLowerCase());
}
//And than In Your ViewContrlller Called this function on Next And Previous button method
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self highlightAllOccurencesOfNextString:searchbar.text];
}
- (NSInteger)highlightAllOccurencesOfNextString:(NSString*)str
{
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"UIWebViewSearch" ofType:@"js"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[htmlWebView stringByEvaluatingJavaScriptFromString:jsString];
NSString *startSearch = [NSString stringWithFormat:@"uiWebview_HighlightAllOccurencesOfNextString('%@')",str];
[htmlWebView stringByEvaluatingJavaScriptFromString:startSearch];
NSString *result = [htmlWebView stringByEvaluatingJavaScriptFromString:@"a.length"];
currentPosition = [result intValue] - 1;
return [result integerValue];
}
-(void)nextMethod
{
currentPosition -= 1;
NSString *nextScrollPosition = [NSString stringWithFormat:@"a[%d].scrollIntoView()", currentPosition];
[htmlWebView stringByEvaluatingJavaScriptFromString:nextScrollPosition];
}
-(void)previousMethod
{
currentPosition += 1;
NSString *previousScrollPosition = [NSString stringWithFormat:@"a[%d].scrollIntoView()", currentPosition];
[htmlWebView stringByEvaluatingJavaScriptFromString:previousScrollPosition];
}
Upvotes: 1