1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
This script highlights User-defined case-insensitive Text on a page.
It can run on any page but that adds unnecessary load to your computer. I have set it not to run on any page for now
Go to Add-ons - User Scripts ('Ctrl+ Shift + a' on Firefox) Click on the Script's Option Under User Settings Tab, Add Included/Excluded Pages that you want the script to run on Click OK
Setting Keywords & Highlight Style: Click on GreaseMonkey Icon User Scripts Commands...
Set Keywords Input keywords separated by comma Example: word 1,word 2,word 3
Set Highlight Style Input the Highlight Style (use proper CSS) Example: color: #f00; font-weight: bold; background-color: #ffe4b5;
Note: If you find that another script clashes with this script, set Text Highlighter to Execute first. Go to Add-ons - User Scripts ('Ctrl+ Shift + a' on Firefox) Right Click on the Script On the context menu click: Execute first
On Add-ons - User Scripts, you can also Click on the Execution Order (top Right) and change the execution order so that Text Highlighter runs before those scripts that clashes with it.
--------- History ---------
1.7 Changed script from matching whole words to do partial word match similar to browser's FIND + escaped RegEx Quantifiers in keywords 1.6 Code Improvement, using test() 1.5 Code Improvement 1.4 Code Improvement + Added support for non-English Words 1.3 Code Improvement, 10x speed increase 1.2 Added User Script Commands, script can now be auto-updated without losing User Data 1.1 Total Code rewrite, Xpath pattern 1.0 Initial release
*/
(function() { 'use strict';
if (window.self !== window.top) { return; }
function setUserPref(varName, defaultVal, menuText, promtText, sep) {
GM_registerMenuCommand(menuText, function() {
var val = prompt(promtText, GM_getValue(varName, defaultVal)); if (val === null) { return; }
if (sep && val) { var pat1 = new RegExp('\\s*' + sep + '+\\s*', 'g'); var pat2 = new RegExp('(?:^' + sep + '+|' + sep + '+$)', 'g');
val = val.replace(pat1, sep).replace(pat2, ''); }
val = val.replace(/\s{2,}/g, ' ').trim(); GM_setValue(varName, val); location.reload(); }); }
setUserPref( 'keywords', 'word 1,word 2,word 3', 'Set Keywords', 'Set keywords separated by comma\t\t\t\t\t\t\t\r\n\r\nExample:\r\nword 1,word 2,word 3', ',' );
setUserPref( 'highlightStyle', 'color: #f00; background-color: #ffebcd;', 'Set Highlight Style', 'Set the Highlight Style (use proper CSS)\r\n\r\nExample:\r\ncolor: #f00; font-weight: bold; background-color: #ffe4b5;' );
var highlightStyle = GM_getValue('highlightStyle'); var keywords = GM_getValue('keywords'); if (!keywords || !highlightStyle) { return; }
var rQuantifiers = /[-\/\\^$*+?.()|[\]{}]/g; var keywords = keywords.replace(rQuantifiers, '\\$&').split(',').join('|'); var pat = new RegExp('(' + keywords + ')', 'gi'); var span = document.createElement('span');
var snapElements = document.evaluate( './/text()[normalize-space() != "" ' + 'and not(ancestor::style) ' + 'and not(ancestor::script) ' + 'and not(ancestor::textarea) ' + 'and not(ancestor::code) ' + 'and not(ancestor::pre)]', document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if (!snapElements.snapshotItem(0)) { return; }
for (var i = 0, len = snapElements.snapshotLength; i < len; i++) {
var node = snapElements.snapshotItem(i);
if (pat.test(node.nodeValue)) {
var sp = span.cloneNode(true); sp.innerHTML = node.nodeValue.replace(pat, '<span style="' + highlightStyle + '">$1</span>'); node.parentNode.replaceChild(sp, node); } } })
|