How Can I Get Link URL from ‘Click Google Chrome Link’?

I would like to get URL from hyperlink on webpage without using cursor. As Click Google Chrome Link seems to get link texts and URLs, can I possibly get a certain URL from those? If no, how can I do it on KM?

Thank you very much for any help.

Hey There,

That’s not nearly enough information for us to help you.

What specific URL?

How can we possibly discriminate amongst all the links on a page?

Without more detail this is impossible.

It would be best if you could provide an example URL, and show us specifically what you want.

-Chris

Hey Chris,

Always thank you for your help. Your help has been a great deal in my daily work for a couple years. And I would like to apologize for not giving you enough information for this issue. Not understanding script languages and KM enough in general, I didn’t know how much info I need to provide. Privacy concerns as well.

This time, I made some page examples:

EXAMPLE 1

Text A

Same Text“Keyboard Maestro Forum”

Text B

Same TextSailors fighting in the dance hall
Same Text“Oh man!”
Same TextLook at those cavemen go

(would yield: https://forum.keyboardmaestro.com/)

For now, I use two following actions consecutively to get the URL from the hyperlinks under the Text A. The two hyperlinks, "Same Text" and the link text next to it are linked to the same URL.


Although Click Google Chrome Link only shows one "Same Text" in its dropdown menu, even though there are 3 more on the same page, it works for me as it clicks the first one which is the target in this example. But it doesn't work when the page is like this:

EXAMPLE 2

Text C

Same TextIt’s the freakiest show

Text A

Same TextDownload KM

Text B

Same TextTake a look at the Lawman
Same TextBeating up the wrong guy

(would yield: https://www.keyboardmaestro.com/main/)

As Click Google Chrome Link only clicks the first one, it clicks "Same Text" under the Text C, not the one under the Text A.

I would like to get URL from the hyperlinks under Text A. From which to get it, "Same Text" or the text next to it, doesn't matter.

I hope I provided enough information for this issue this time. If it is not enough and if you need saved html files or whatever of the actual webpage, I would give you via message. Please just tell me. I need your help.

Thank you so much for any help.

K

I still don’t know if I understand the question, but would it help if I say that

  • the ‘Tab’ key will advance through each of the links on a page; and
  • the ‘Return’ key will open the highlighted link?

For anyone wondering why their Tab key doesn't advance through links, buttons, etc., you have to enable that behavior in System Preferences > Keyboard > Shortcuts.

Hey @kyulee,

Hmm… What about this?

This will scrape all the links in the page, and you can use a literal string or a regular expression in the regexStr variable to narrow down the results.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/20 23:10
# dMod: 2017/03/20 23:18
# Appl: Google Chrome
# Task: Extract HREF links from the front page with an optional regular expression.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @Extract, @HREF, @Links, @Front, @Page, @Regular, @Expression, @RegEx, @ccstone
------------------------------------------------------------------------------

set regexStr to "youtube"
set linkList to chromeLinks(regexStr, "*", "href")
set linkList to text of linkList

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
--» chromeLinks()
------------------------------------------------------------------------------
--  dCre: 2016/12/12 15:00
--  dMod: 2016/12/15 08:06
------------------------------------------------------------------------------
on chromeLinks(regexStr, tagName, tagType)
   set javascriptCMD to "function in_array (array, item) {
   for (var i=0; i < array.length; i++) {
      if ( array[i] == item ) {
         return true;}}
   return false;}
   var a_tags = document.getElementsByTagName('" & tagName & "');
   var href_array = new Array();
   var reg = new RegExp(/" & regexStr & "/i);
   for (var i=0; i < a_tags.length; i++) {
      var href = a_tags[i]." & tagType & ";
      if ( reg.test(href)) {
         if ( !in_array(href_array, href)) {
            href_array.push(href);}}}

   href_array;
"
   try
      
      tell application "Google Chrome"
         if front window exists then
            tell active tab of front window
               set linkList to execute javascript javascriptCMD
               if linkList = missing value then set linkList to {}
            end tell
         end if
      end tell
      
   on error
      set linkList to {}
   end try
   
   return linkList
   
end chromeLinks

------------------------------------------------------------------------------

Run this from the Script Editor, and you’ll see an AppleScript list returned.

You can return plain text instead of an AppleScript list object by changing the lines above the handlers line to:

set regexStr to "youtube"
set linkList to chromeLinks(regexStr, "*", "href")
set linkList to text of linkList
set AppleScript's text item delimiters to linefeed
set linkList to linkList as text

HTH

-Chris

1 Like

This is great. Thanks again, Chris!

One extra question,

http://abc.def/ghi/jk/1234
http://gdf.fgag/gawe/dk/5678
http://axdfb.ht/gfh/gk/9012
http://hthsc.d/gshi/jn/3456

From the example result, how can I get only the second URL?

Hey @kyulee,

If you return that as an AppleScript list object then it’s easy:

set myVar to item 2 of myListVar

It’s still easy if you return it as a text-list.

set myVar to paragraph 2 of myTextVar

-Chris

I had made KM save the result(text) as variable to get the second URL from it using Search Variable or whatever.

I'm sorry, I don't think I understand how I use this, can you please tell me where I should put this string?

Hey @kyulee,

Look at this script and find “-- NEW”.

There’s only one little change at that point.

Try running the script from the Script Editor.app, before you run it from Keyboard Maestro.

-Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/20 23:10
# dMod: 2017/03/30 06:06
# Appl: Google Chrome
# Task: Extract HREF links from the front page with an optional regular expression.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @Extract, @HREF, @Links, @Front, @Page, @Regular, @Expression, @RegEx, @ccstone
------------------------------------------------------------------------------

set regexStr to "youtube"
set linkList to chromeLinks(regexStr, "*", "href")
set linkList to text of linkList -- filter out any missing values

set myURL to item 2 of linkList -- NEW

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
--» chromeLinks()
------------------------------------------------------------------------------
--  dCre: 2016/12/12 15:00
--  dMod: 2016/12/15 08:06
------------------------------------------------------------------------------
on chromeLinks(regexStr, tagName, tagType)
   set javascriptCMD to "function in_array (array, item) {
   for (var i=0; i < array.length; i++) {
      if ( array[i] == item ) {
         return true;}}
   return false;}
   var a_tags = document.getElementsByTagName('" & tagName & "');
   var href_array = new Array();
   var reg = new RegExp(/" & regexStr & "/i);
   for (var i=0; i < a_tags.length; i++) {
      var href = a_tags[i]." & tagType & ";
      if ( reg.test(href)) {
         if ( !in_array(href_array, href)) {
            href_array.push(href);}}}

   href_array;
"
   try
      
      tell application "Google Chrome"
         if front window exists then
            tell active tab of front window
               set linkList to execute javascript javascriptCMD
               if linkList = missing value then set linkList to {}
            end tell
         end if
      end tell
      
   on error
      set linkList to {}
   end try
   
   return linkList
   
end chromeLinks

------------------------------------------------------------------------------
1 Like

Thanks a lot, Chris.