click on highlighted text

I know I am going to get a “no” for this one. But it is so important that I would like to get it rather than avoid asking.

Cmd+F searches for text in web page in Safari.

Is it possible to open the underlying link beneath the highlighted text?
(if not applescript, then javascript)

(I know what to do in Firefox, Camino etc. I want to know about Safari only)

How you would do this in these browsers?

they have " / " and "’ ’ " keys to select text and links and then you press return or command return

Well, via AppleScript → I don’t think so :confused:

via JavaScript through AppleScript → only the selected Text, but not the link behind it

only returns a string… I mean the javascript function getSelection()

So I ended up with a search_appleScript… perhaps not exactly what you were looking for, but perhaps some kind of helpfull (the javascript part could be optimized for sure :lol: )

-- 28.09.2009 hubionmac.com
-- prompts in Safari for a Search-String (applescript)
-- searches current site for links containing search-string (javascript)
-- highlights found links (yellow) and temp. adds their id-num (javascript)
-- shows a list of all found links containing search-string (applescript)
-- opens choosen link in same window //option to open in new window.... (javascript)

property open_link_in_new_window : true
property case_sensitiv_search : false
property highlight_found_links : true
my clean_prev_found()
my do_search()

on do_search()
	tell application "Safari"
		activate
		tell document 1
			set searchstring to text returned of (display dialog "Seach Links containing:" default answer "")
			set found_links to do JavaScript "var k = new Array();
		for(var i=0;i<document.links.length;i++) {
			if (" & case_sensitiv_search & "==true)
				var foundit = document.links[i].text.indexOf('" & searchstring & "')
			else
				var foundit = document.links[i].text.toLowerCase().indexOf('" & searchstring & "'.toLowerCase())
		
			if (foundit>-1)
			{
				k.push(document.links[i].text + ' ('+i+')')
					if (" & highlight_found_links & "==true)
					{
						document.links[i].style.background = 'yellow'
						for (var s=0;s<document.links[i].getElementsByTagName('span').length;s++)
						{
							if (document.links[i].getElementsByTagName('span')[s].id == 'foundthishereid')
							{
								document.links[i].getElementsByTagName('span')[s].innerHTML = '('+ i + ')'
								break;
							}
						}
					}
				}
			}
		k;"
			if (count of found_links) > 0 then
				set choosen_link to (choose from list found_links default items (item 1 of found_links) with prompt "Found Links containing:" & return & "\"" & searchstring & "\"")
				if choosen_link ≠ false then
					my clean_prev_found()
					if open_link_in_new_window = false then
						do JavaScript "window.location = document.links[" & last word of (choosen_link as text) & "]"
					else
						do JavaScript "window.open(document.links[" & last word of (choosen_link as text) & "])"
					end if
				end if
			else
				display dialog "Nothing found" giving up after 1
			end if
		end tell
	end tell
end do_search
on clean_prev_found()
	tell application "Safari"
		tell document 1
			activate
			do JavaScript "
			for(var i=0;i<document.links.length;i++)
			{	
				if (document.links[i].style.background == 'yellow')
					document.links[i].style.background = ''
				var there_is_alread_one = false
				if (document.links[i].getElementsByTagName('span').length>0)
				{
					for (var k=0;k<document.links[i].getElementsByTagName('span').length;k++)
					{
						if (document.links[i].getElementsByTagName('span')[k].id == 'foundthishereid')
						{
							document.links[i].getElementsByTagName('span')[k].innerHTML = '';
							there_is_alread_one = true;	
						}
					}
					if (there_is_alread_one==false)
						document.links[i].innerHTML = document.links[i].innerHTML + '<span id=\\'foundthishereid\\' style=\\'color:white;background:red\\'></span>'
				}else
				{
					document.links[i].innerHTML = document.links[i].innerHTML + '<span id=\\'foundthishereid\\' style=\\'color:white;background:red\\'></span>'

				}
			}
"
		end tell
	end tell
end clean_prev_found

I never bothered to ask for this before because I was comfortable using Camino. However, when I read Safari is now 64 bit in Snow Leopard and javascript executes faster, I became tempted to start using Safari again.
I really never expected a solution and you have really surprised me, hubionmac. I cannot thank you enough.

I am afraid that i don’t know anything about javascript. Also, I don’t know of any javascript forum which is kind to beginners like this forum is for applescript newbies. So I am going to have to beg for help from others when I need some changes in the script.:slight_smile:
In its current state, the script you posted works fine on the web pages I tested.

However, please make one amendment in the script:

Open “http://www.macscripter.net/”. Run your script the search for “Scripting”. You will see that the first result is “Scripting Forums, Tutorials, Articles…(13)”. Open it and a new blank tab will open. This is because: It is not actually a link to a web page but to run a script. So it has to open in the same tab. Can you please add some code to determine which links should open in the same tab and which should open in a new tab?

(I think, instead of a display dialog to take the search query, it would be better to use UI scripting and get the text from “Find” bar in Safari which springs down when we press “Cmd+F” (Because, Cmd+F can be changed to something simpler like " / " or " ’ ". See: http://www.macosxhints.com/article.php?story=20071212073849257).)

Thanks again.

sorry, since your script contained a lot of javascript, i did not even bother to read it. You have clear instructions in the comments. There is no need for any change in the script. I will make two different scripts and setup two different keyboard shortcuts to open the url in the same tab or new tab.