forked from pratyushmittal/Regex-Search-Bookmarklet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarklet.js
1 lines (1 loc) · 2.65 KB
/
bookmarklet.js
1
(function(){var v="1.3.2";if(window.jQuery===undefined||window.jQuery.fn.jquery<v){var done=false;var script=document.createElement("script");script.src="http://ajax.googleapis.com/ajax/libs/jquery/"+v+"/jquery.min.js";script.onload=script.onreadystatechange=function(){if(!done&&(!this.readyState||this.readyState=="loaded"||this.readyState=="complete")){done=true;initMyBookmarklet();}};document.getElementsByTagName("head")[0].appendChild(script);}else{initMyBookmarklet();}function initMyBookmarklet(){(window.myBookmarklet=function(){function searchWithinNode(node,re){var pos,skip,acronym,middlebit,endbit,middleclone;skip=0;if(node.nodeType==3){pos=node.data.search(re);if(pos>=0){acronym=document.createElement('ACRONYM');acronym.title='Search '+(searches+1)+': '+re.toString();acronym.style.backgroundColor=backColor;acronym.style.borderTop='1px solid '+borderColor;acronym.style.borderBottom='1px solid '+borderColor;acronym.style.fontWeight='bold';acronym.style.color=borderColor;middlebit=node.splitText(pos);endbit=middlebit.splitText(RegExp.lastMatch.length);middleclone=middlebit.cloneNode(true);acronym.appendChild(middleclone);middlebit.parentNode.replaceChild(acronym,middlebit);count++;skip=1;}}else if(node.nodeType==1&&node.childNodes&&node.tagName.toUpperCase()!='SCRIPT'&&node.tagName.toUpperCase!='STYLE'){for(var child=0;child<node.childNodes.length;++child){child=child+searchWithinNode(node.childNodes[child],re);}}return skip;}function set_message(text){noti=$('#reg-noti');if(noti.length==0){notifications='<div style="position:fixed;bottom:0;display:block;padding:5px;background-color:white;" id="reg-noti">'+text+'</div>';$('body').append(notifications);}else{message='<br />'+text;noti.append(message);}}function post_search(){set_message('Found '+count+' match'+(count==1?'':'es')+' for '+regexp+'.');if(count>0){searches++;}}var count=0,searches=0,text,regexp,borderColor,backColor;for_input='Enter Regex Pattern:<input style="border:1px solid #cdcdcd;padding:3px;margin:3px;" id="regex-query" type="text"/><a style="padding-left:5px;" id="regex-search" href="#">Search</a>';set_message(for_input);$('#regex-search').click(function(){text=$('#regex-query').val();if(text==null||text.length==0){alert('Nothing to find.');return false;}try{regexp=new RegExp(text,'i');}catch(er){set_message('Unable to create regular expression using text \''+text+'\'.\n\n'+er);return false;}borderColor='#'+(searches+8).toString(2).substr(-3).replace(/0/g,'3').replace(/1/g,'6');backColor=borderColor.replace(/3/g,'c').replace(/6/g,'f');if(searches%16/8>=1){var tempColor=borderColor;borderColor=backColor;backColor=tempColor;}searchWithinNode(document.body,regexp);post_search();});})();}})();