I have written the below code to search for a string in a web page. This is working fine but I need suggestions on improving this code to start using this. This is written to work under IE.
$(document).ready(function()
{
var NotFounds = [];
function countString(s) {
var re = new RegExp(s, 'gi');
var text = document.body.createTextRange().text;
var matches = text.replace(/\s+/g, ' ').match(re);
return matches? matches.length : 0;
}
// This function is the Sentence search function.
function doSearch(tex) {
if (document.body.createTextRange) {
var textRange = document.body.createTextRange();
textRange.collapse(true);
if(textRange.findText(tex)) {
countString(tex) > 1 ? textRange.execCommand("BackColor", false, "#FF6600") : textRange.execCommand("BackColor", false, "yellow");
textRange.collapse(false);
}
else
{
NotFounds.push(tex);
}
}
}
function trim(value) {
var temp = value;
var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
var obj = / +/g;
temp = temp.replace(obj, " ");
if (temp == " ") { temp = ""; }
return temp;
}
function SearchQueue(text)
{
if(text !== null)
{
if($('#NotFounds').size() > 0)
{
$('#NotFounds').detach();
}
text = text.replace(/^\s*$[\n\r]{1,}/gm, '');
text = text.replace(/”/g, "\"");
text = text.replace(/“/g, "\"");
text = text.replace(/”/g, "\"");
text = text.replace(/’/g, "\'");
text = text.replace(/‘/g, "\'");
text = text.replace(/–/g, "\-");
text = text.replace(/—/g, "\-");
text = text.replace(/–/g, "\-");
var txt1=text;
text = trim(txt1);
var SearchItems = text.split(/\r\n|\r|\n/);
document.designMode = "on";
for(var i=0;i<SearchItems.length;i++)
{
var textTrim = $.trim(SearchItems[i]);
doSearch(textTrim);
}
document.designMode = "off";
if(NotFounds.length > 0)
{
var st = "<center><b>Mismatched texts are...</b></center>";
for(var k=0;k<NotFounds.length;k++)
{
st = st + "<br>Mismatch "+ (k+1) + ". <span style='color:blue'>" + NotFounds[k] + "</span>";
}
st = st + "</font>";
$('body').prepend('<div id="NotFounds" style="width:100%;border:1px solid gray"></div>');
$('#NotFounds').html(st);
alert("Some items are mismatched... Please check at the top of the page.");
}
}
}
var body = $('body');
var OrigBody = body.html();
$('body').dblclick(function() {
var txt = window.clipboardData.getData("Text");
var ClipText = txt.replace(/[a-zA-Z0-9\?\&\=\%\#]+s\=(\w+)(\&.*)?/,"$1");
if ($.browser.msie) {
NotFounds = [];
SearchQueue(ClipText.replace(/\%20|\+/g,"\|"));
}
});
shortcut.add("Ctrl+Z",function() {
body.html(OrigBody);
});
shortcut.add("Up",function() {
$('b,i,u,sup,sub,strong,font,a').wrap('<font style="color:blue"></font>');
});
shortcut.add("Right",function() {
$('table').filter(function() { return ($(this).find('input').size() > 0); }).find('tr').each(function()
{
$(this).find('input').filter(function(){
return (Math.round(Math.random()) == 1);
}).each(function()
{
switch(this.type) {
case 'checkbox':
case 'radio':
this.checked = true;
break;
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'textarea':
$(this).val('2');
break;
}
});
});
$('#forwardbutton').trigger('click');
});
});