K-Meleon

KMeleonWiki > Resources > MacroLibrary > MacroLibrary2 > Highlight

Submitted by: Rayven



Notes:

Created by hugo_heden. Works on html files, but doesn't work on txt files.


In Macros:

#_______________________
# A global variable for storing the words
# searched for, as a string with
# non-alnum separated words:
$_highlight_strings = "";



#_______________________
highlight{
menu = "Highlight.."
$old_clip = getclipboard();
# get current selection:
setclipboard("");
id(ID_EDIT_COPY);
$q = getclipboard();
$q = gsub( "\n", " ", $q );
$q = gsub( " ", " ", $q );
# Prompt for searched words (with
# current selection as default):
$_highlight_strings = prompt("Highlight words", "Highlight words:", $q);
# Call the highlighter macro (which will
# use the strings stored in $_highlight_strings):
macros(_highlight__);
setclipboard($old_clip);
$_highlight_strings = "";
}
#_______________________



#________________________________________
_highlight__ {
$javascript_enabled = getpref(BOOL, "javascript.enabled") ;
setpref(BOOL, "javascript.enabled", true) ;
#_______ script starts here, no line breaks in it!_________
   open("javascript: var styles = new Array( 'background-color:#ffff66;font-weight:bolder;color:black' , 'background-color:#66ffff;font-weight:bolder;color:black' , 'background-color:#ff66ff;font-weight:bolder;color:black' , 'background-color:#9999ff;font-weight:bolder;color:black' , 'background-color:#ff9999;font-weight:bolder;color:black' , 'background-color:#ff99ff;font-weight:bolder;color:black' ); var endTag = '</span>'; var cssStartTags = new Array(); for(var j=0; j<styles.length; j++){ spanTag = '<span style='+styles[j]+' class=Highlighted>'; cssStartTags.push(spanTag); } function removeEmpties(stringarr){ var ret = new Array(); var j; for (j=0; j<stringarr.length; j++) if (stringarr[j].length != 0) ret.push(stringarr[j]); return ret; } function insertBetweenAll(arr, element) { var ret = new Array(); var j; for (j=0; j< arr.length; j++) { ret.push(arr[j]); ret.push(element); } ret.pop(); return ret; } function indexOfByValue(arr, str){ var check = /[^A-ZÅÄÖa-zåäö0-9_-]/i; if (check.test(str)) return -1; var j; var regexp = new RegExp('^'+str+'$', 'i'); for(j=0; j<arr.length; j++){ if (regexp.test(arr[j].valueOf())) return j; } return -1; } function createRE(arrWordsToHighlight){ var tmp = insertBetweenAll(arrWordsToHighlight, '|'); var wordToHighlightRegExpStr = tmp.join(''); var regstr = '(?:<!--(?:.*?)-->)|(?:<(script|textarea|style|head|option|xmp)(?:\\s*)(?:>|[^>]*>)(?:[^<])*<\\/\\1([\\s])*>)|(?:<(?:(?:\\%22(?:[^\\%22]*?)\\%22)|[^>])*>)|' + wordToHighlightRegExpStr ; return new RegExp(regstr, 'ig'); } function getSelectedInFrame(aFrame){ var str = new String(aFrame.getSelection()); for (var j=0; j<aFrame.frames.length; j++) str += ' ' + aFrame.frames[j].getSelection(); return str; } var __newHtml = new Array(100); var __lastCut = 0; var __re, __html, __cssStartTags, __endTag, __wordsArr; var _highlight_count = 0; function userStopIt(event){ if (event.which != 19 && event.which != 27) return; stopIt(); } function stopIt(){ window.status = 'Finishing...'; if (interval != null) { clearInterval(interval); interval = null } __newHtml.push(__html.substring(__lastCut)); finishMarkUp(__newHtml.join('')); } function loopChunk(){ var result; for (var j =0; j<2000; j++){ if (!(result=__re.exec(__html))){ stopIt(); return; } var foundOn = result.index; var wordNum = indexOfByValue(__wordsArr, result[0]); __newHtml.push(__html.substring(__lastCut, foundOn)); if ( wordNum != -1 ) { __newHtml.push(__cssStartTags[wordNum % __cssStartTags.length]); _highlight_count++; } __newHtml.push(result[0]); if ( wordNum != -1 ) __newHtml.push(__endTag); __lastCut = __re.lastIndex; } window.status = Math.round(foundOn*100/__html.length)+'% ... Press Escape to interrupt.'; } function insertMarkups(re, html, cssStartTags, endTag, wordsArr){ __re = re; __html=html; __cssStartTags = cssStartTags; __endTag = endTag; __wordsArr = wordsArr; window.status = 'Scanning ...'; interval = setInterval('loopChunk()', 1); } var _element = null, _wordArr = null, _wordsRE = null; var _currentSubFrame = -1 ; function getProperDocElement(aFrame){ if (aFrame.document.body) return aFrame.document.body; return aFrame.document.documentElement; } function finishMarkUp(newhtml){ _element.innerHTML = newhtml; _element = null; if (window.frames){ _currentSubFrame++; if (_currentSubFrame < window.frames.length){ __newHtml = new Array(100); __lastCut = 0; _element = getProperDocElement(window.frames[_currentSubFrame]); markUpInElement(); return; } } window.status = 'Done. Highlighted words: '+_wordArr.join(',') + '. Found total: '+ _highlight_count; _highlight_count = 0; document.onkeyup = null; } function markUpInElement(){ var html = _element.innerHTML; insertMarkups(_wordsRE, html, cssStartTags, endTag, _wordArr); } function highlight(){ var wordArr = '".$_highlight_strings."'; wordArr = removeEmpties(wordArr.split(/[^A-ZÅÄÖa-zåäö0-9_-]/gi)) ; if (wordArr.length == 0) return; var element; document.onkeyup = userStopIt; var wordsRE = createRE(wordArr); if (document.body) element = document.body; else element = document.documentElement; _element = element; _wordArr = wordArr; _wordsRE = wordsRE; markUpInElement(); }  highlight();");
#_______ script ends here _________
setpref(BOOL, "javascript.enabled", $javascript_enabled);
}
#________________________________________


In Menus:

macros(highlight)


User Comments:

K-Meleon

(c) 2000-2010 kmeleonbrowser.org. All rights reserved.
design by splif.