Advertisement
Sebi

Search Regex in Chrome/Firefox - Bookmark URL

Oct 8th, 2019
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Add the following to bookmarks to get regex search with highlighting.
  2. // You can also see the matches in console, with the ability to scroll into viewport on right click in some browsers.
  3.  
  4. 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);}}}})})()
  5.  
  6. // Formatted
  7. javascript:(function(){
  8.     Array.from(document.getElementsByClassName('ext-search-match')).forEach(x => {
  9.         x.insertAdjacentText('afterend', x.textContent);
  10.         x.remove()
  11.     });
  12.  
  13.     const
  14.         r=prompt('Regex search: (preceed with $ for case insensitive)'),
  15.        
  16.         i=r.length&&r[0]==='$',
  17.        
  18.         p=new RegExp(i?r.substring(1):r, i?'gi':'g'),
  19.        
  20.         all=document.querySelectorAll('*');
  21.    
  22.     all.forEach(el => {
  23.         if(el.tagName === 'SCRIPT' || el.tagName === 'STYLE')
  24.             return;
  25.        
  26.         for(let n of el.childNodes) {
  27.             if(n.nodeType === 3) {
  28.                 const v = n.nodeValue;
  29.                 if(p.test(v)) {
  30.                     console.log("Found a match:", el, "Content: ", el.textContent);
  31.                     c = el.childNodes.length > 1 ? document.createElement('span') : el;
  32.                     c.innerHTML = v.replace(p,'<span style="background-color:red;" class="ext-search-match">$&</span>');
  33.                     c !== el && el.replaceChild(c, n);
  34.                 }
  35.             }
  36.         }
  37.     })
  38. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement