Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Add the following to bookmarks to get regex search with highlighting.
- // You can also see the matches in console, with the ability to scroll into viewport on right click in some browsers.
- javascript:(function(){Array.from(document.getElementsByClassName('ext-search-match')).forEach(x=>{x.insertAdjacentText('afterend',x.textContent);x.remove()});const r=prompt('Regex search: (preceed with $ for case insensitive)'),i=r.length&&r[0]==='$',p=new RegExp(i?r.substring(1):r,i?'gi':'g'),all=document.querySelectorAll('*');all.forEach(el=>{if(el.tagName==='SCRIPT'||el.tagName==='STYLE')return;for(let n of el.childNodes){if(n.nodeType===3){const v=n.nodeValue;if(p.test(v)){console.log("Found a match:",el,"Content:",el.textContent);c=el.childNodes.length>1?document.createElement('span'):el;c.innerHTML=v.replace(p,'<span style="background-color:red;" class="ext-search-match">$&</span>');c!==el&&el.replaceChild(c,n);}}}})})()
- // Formatted
- javascript:(function(){
- Array.from(document.getElementsByClassName('ext-search-match')).forEach(x => {
- x.insertAdjacentText('afterend', x.textContent);
- x.remove()
- });
- const
- r=prompt('Regex search: (preceed with $ for case insensitive)'),
- i=r.length&&r[0]==='$',
- p=new RegExp(i?r.substring(1):r, i?'gi':'g'),
- all=document.querySelectorAll('*');
- all.forEach(el => {
- if(el.tagName === 'SCRIPT' || el.tagName === 'STYLE')
- return;
- for(let n of el.childNodes) {
- if(n.nodeType === 3) {
- const v = n.nodeValue;
- if(p.test(v)) {
- console.log("Found a match:", el, "Content: ", el.textContent);
- c = el.childNodes.length > 1 ? document.createElement('span') : el;
- c.innerHTML = v.replace(p,'<span style="background-color:red;" class="ext-search-match">$&</span>');
- c !== el && el.replaceChild(c, n);
- }
- }
- }
- })
- })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement