473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I tweak this javascript expression?

I have got a bookmarklet for use with Opera which highlights all
occurrences of some text in the displayed page. I find it very
useful.

Sometimes I need to use two or three different colours for different
keywords I want highlighting on the same page. I have done this by
changing the original word 'yellow' for other javascript colour
words.

I know next to nothig about javascript. So I would ask if anyone
knows how I can tweak this javascript bookmarklet code (see below) in
two sepearte ways:

(1) So that I can select a color from a list. This may be as simple
as changing the caption to say "enter 1 for lime, 2 for yellow," etc.
or it might be cleverer.

(2) This is mutually exclusive with (1). In this case the
javascript would prompt me to enter a javascript colour keyword (like
"yellow" or colour code like "FF00FF").

Of course, in each case the javascript needs to ask me what i am
searching for!

The javascript is below. Is anyone able to advise on this?

TIA.

Jane
--------------
javascript:(function(){var count=0, text, dv;text=prompt("Search
phrase (yellow):", "");if(text==null ||
text.length==0)return;dv=document.defaultView;func tion
searchWithinNode(node, te, len){var pos, skip, spannode, middlebit,
endbit, middleclone;skip=0;if( node.nodeType==3
){pos=node.data.toUpperCase().indexOf(te);if(pos>= 0){spannode=documen
t.createElement("SPAN");spannode.style.backgroundC olor="yellow";middl
ebit=node.splitText(pos);endbit=middlebit.splitTex t(len);middleclone=
middlebit.cloneNode(true);spannode.appendChild(mid dleclone);middlebit
..parentNode.replaceChild(spannode,middlebit);++co unt;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.childNo des[child], te,
len);}}return skip;}window.status="Searching for
'"+text+"'...";searchWithinNode(document.body, text.toUpperCase(),
text.length);window.status="Found "+count+"
occurrence"+(count==1?"":"s")+" of '"+text+"'.";})();

Jul 23 '05 #1
3 1733
Jane D wrote:
I have got a bookmarklet for use with Opera which highlights all
occurrences of some text in the displayed page. I find it very
useful.

Sometimes I need to use two or three different colours for different
keywords I want highlighting on the same page. I have done this by
changing the original word 'yellow' for other javascript colour
words.

I know next to nothig about javascript. So I would ask if anyone
knows how I can tweak this javascript bookmarklet code (see below) in two sepearte ways:

(1) So that I can select a color from a list. This may be as simple as changing the caption to say "enter 1 for lime, 2 for yellow," etc. or it might be cleverer.

(2) This is mutually exclusive with (1). In this case the
javascript would prompt me to enter a javascript colour keyword (like "yellow" or colour code like "FF00FF").

Of course, in each case the javascript needs to ask me what i am
searching for!

The javascript is below. Is anyone able to advise on this?

TIA.

Jane
--------------
javascript:(function(){var count=0, text, dv;text=prompt("Search
phrase (yellow):", "");if(text==null ||
text.length==0)return;dv=document.defaultView;func tion
searchWithinNode(node, te, len){var pos, skip, spannode, middlebit,
endbit, middleclone;skip=0;if( node.nodeType==3
){pos=node.data.toUpperCase().indexOf(te);if(pos>= 0){spannode=documen
t.createElement("SPAN");spannode.style.backgroundC olor="yellow";middl
ebit=node.splitText(pos);endbit=middlebit.splitTex t(len);middleclone=
middlebit.cloneNode(true);spannode.appendChild(mid dleclone);middlebit
.parentNode.replaceChild(spannode,middlebit);++cou nt;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.childNo des[child], te,
len);}}return skip;}window.status="Searching for
'"+text+"'...";searchWithinNode(document.body, text.toUpperCase(),
text.length);window.status="Found "+count+"
occurrence"+(count==1?"":"s")+" of '"+text+"'.";})();


Hi Jane.

The second option is a lot easier, I'd say...

javascript:(function(){var count=0, text, dv;text=prompt("Search
phrase:", "");hilite=prompt("Hilite
color:", "");if(!hilite)hilite="yellow";if(text==null.. .

....and:

t.createElement("SPAN");spannode.style.backgroundC olor=hilite;middl...

No testing for invalid input, of course. Option (1) might involve a
pop-up window with a listbox...but I'm eating lunch right now.

Jul 23 '05 #2
On 24 Feb 2005, RobB wrote:
Hi Jane.

The second option is a lot easier, I'd say...

javascript:(function(){var count=0, text, dv;text=prompt("Search
phrase:", "");hilite=prompt("Hilite
color:", "");if(!hilite)hilite="yellow";if(text==null.. .

...and:

t.createElement("SPAN");spannode.style.backgroundC olor=hilite;mid
dl...

No testing for invalid input, of course. Option (1) might
involve a pop-up window with a listbox...but I'm eating lunch
right now.

That works well! Thank you.

J
Jul 23 '05 #3
OK guys, I am sorry to be a pain but after I was kindly helped to
tweak some javascript in my Opera bookmarklet, I have found a similar
but much better bookmarklet.

Yes, you guessed it ... I want to get thi snew one tweaked
too! Unfortunately the layout of this new one is completely
different to the first one so I can't use the info people kindly gave
me.

Like the first, this bookmarklet highlights chosen text but with an
important difference. This one tells you how many instances it has
found. This count can be very useful!

Sometimes I need to use two or three
different colours for different keywords which I want highlighting on
the same page. I have done this in my new bookmarklet by changing
the original word 'yellow' for other javascript colour words.

How I can tweak this new javascript bookmarklet code (listed below)
to allow me to select the highlighting color?

As before there are two dialogs with the user:

(1) I can select a color from a list. This may be as simple
as changing the caption to say "enter 1 for lime, 2 for yellow," etc.
or it might be cleverer.

(2) This is mutually exclusive with (1). In this case the
javascript would prompt me to enter a javascript colour keyword (like
"yellow" or colour code like "FF00FF").

The new javascript is below. Is anyone able to advise on this?

Thank you.
Jane

================== JAVASCRIPT ========================

javascript:void(s=prompt('Query:',''));s='('+s+')' ;x=new
RegExp(s,'gi');rn=Math.floor(Math.random()*100);ri d='z' + rn;b =
document.body.innerHTML;b=b.replace(x,'<span name=' + rid + ' id=' +
rid + ' style=\'color:#000;background-
color:lime;\'>$1</span>');void(document.body.innerHTML=b);alert('Fou n
d ' + document.getElementsByName(rid).length + '
matches.');window.scrollTo(0,document.getElementsB yName(rid)[0].offse
tTop);
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
27
by: C Gillespie | last post by:
Dear All, Hopefully I have a simple problem. Basically, I just want to alter some text with JS. Here is some of my test code: <snip> <script type="text/javascript"> var tmp='a';
2
by: 'perch | last post by:
I'm trying to tweak the Google toolbar search button to do two things; to include selected text as a string search (i.e. as though I'd entered the text in ""), and to open a new page for the search...
3
by: Csaba Gabor | last post by:
How, using javascript, can you set or change a CSS value from something to something else which has the IE proprietary format: expression(javasriptCode)? I'd like to know the answer in...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
4
by: DGS | last post by:
Hi all... Yesterday I asked about setting a cookie with an intially requested URL that could be used later. I found the code to do it. For the code on the login.html page I would like to tweak...
7
by: PhilTheGap | last post by:
Hi, I've tried this: <asp:Button ID="Save" runat="server" Text="OK" OnClick="ServerSave" OnClientClick="SaveParam (<% Util.MaxTags %>)" /> but if fails... Util is a C# class, MaxTags a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.