Copying a clean link from a Google result (shedding 'referrer' noise)

The usefulness of Google results is reduced by all the referrer clutter which is attached as soon as you engage the link with a mouse, even to do a ctrl-click Copy Link Address

Here is a macro for simply copying a clean plain text version of the link (with the title if you want it).

Copy clean link (with title) from Google result.kmmacros (23.7 KB)

If you select any of the gray text of the Google results, and trigger the macro, it will

  1. use an XPath route from your selection to the html <a> link
  2. bypass all the onmousedown noise, and simply takes the clean data-href or .href contents
  3. also take the .text contents

In other words from a selection like:

the XPath

./ancestor-or-self::div[@class="rc"]/h3/a

takes us to the <a> link in,

and instead of:

https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&uact=8&sqi=2&ved=0CC4QFjACahUKEwj96LPUtZTHAhUjWdsKHeKvAok&url=https%3A%2F%2Fwww.timeshighereducation.co.uk%2Fcontent%2Fbook-review-cakes-custard-and-category-theory-by-eugenia-cheng&ei=103DVb2hPKOy7Qbi34rICA&usg=AFQjCNHlD2o_2Queoep_REAkz_dY621Luw&bvm=bv.99556055,d.ZGU

the macro simply copies:

Cakes, Custard and Category Theory: Easy Recipes - Times ...
https://www.timeshighereducation.co.uk/content/book-review-cakes-custard-and-category-theory-by-eugenia-cheng

It should be straightforward to edit the .js in the macro a little if you prefer either to drop the title string, or combine title with href in the format of a Markdown link. For example:

(function () {
  var oLinkNode = document.evaluate(
      document.kmvar.xPath,
      window.getSelection().anchorNode,
      null, 0, 0
    ).iterateNext();

  return '[' + oLinkNode.text + '](' +
    (oLinkNode.getAttribute('data-href') ||
    oLinkNode.href) + ')';
})();
1 Like

Been waiting for this macro a long time. Thanks.
Can it be modified to work while the cursor hovers over the link and not the gray text?

A variant for directly following links from Google search results, without any referrer data, and bypassing Google's secure referral server.

Note: this may not be what you want to do.

Directly open a cleaned Google results link.kmmacros (24.5 KB)

Concern about Google's data collection and commercial profiling of users has causeed some migration over to non-tracking search engines like https://duckduckgo.com

When you click on a result presented by a Google search page, it is loaded up with tracking data, and directed towards a Google referral server, rather than directly to the target site.

Before mouse-down

After mouse-down ( tracking data now attached , routing will be through Google server )

Advantages: Google's secure search offers you some protection from following hacked or doubtful links
Disadvantages : It often serves pages a little more slowly, and as Google builds and refines its model of your consumer profile, the same tired old ads start trailing around after you : - )

( German furniture in my case ... Not entirely sure why … )

If you want to make some use of Google Search, but occasionally want to take a shorter or less data-laden route to sites you feel very confident of, you could experiment with:

  1. Placing your cursor anywhere in the gray text of a Google search result
  2. triggering this macro.

It will:

  • get a cleaned version of the target link (no referrer data), and
  • open it directly in a new tab (Safari or Chrome, whichever you are using.

i.e. it behaves as if you had opened a new tab and typed a cleaned link in it directly.

You can test it by searching for this page:

Clicking on Google's blue link will result in:

whereas using the macro will lead to:

The Javascript which it evaluates to clean the link is:

(function () {
	var oLinkNode = document.evaluate(
			document.kmvar.xPath,
			window.getSelection().anchorNode,
			null, 0, 0
		).iterateNext();

	return (oLinkNode.getAttribute('data-href') ||
		oLinkNode.href);
})();

(Ethical disclaimer – no regexes were overstretched in the making of this macro : - )

1 Like

There needs to be a selection rather than a hover, I think.

(or just a simple click on the gray text without any blue extended selection)

Haven’t tried but I think it should work if you select any part of the blue text too.

Might be able to use something like: document.querySelectorAll( ":hover" );

(I leave it as an exercise for the reader :wink:)

Rob, the macro/script that you helped me build seems to extract just the target URL from Google results most of the time, and put it in a clean RTF/PlainText hyperlink. As you said above, the user need to select the blue text/link first. Thanks again for all your help.

See:

I’ve updated both of these macros (above) to read the Google result under the mouse (if nothing has yet been directly selected).

(Selection still takes priority over hover)

The pattern of the code in each varies only in respect of the returned string. For a Markdown copy:

(function () {
  // SELECTED RESULT ?
  var nodeAnchor = window.getSelection().anchorNode,
    nl = (nodeAnchor ? null :

      // OR MOUSE HOVERING OVER A RESULT ?
      document.querySelectorAll(":hover")),
    iLast = (nl ? nl.length : null),
    nodeFocus = nodeAnchor ? nodeAnchor :
    (iLast ? nl[iLast - 1] : null),

    // LINK NODE OF ANY SELECTED/HOVERED RESULT ?
    oLinkNode = nodeFocus ? document.evaluate(
      document.kmvar.xPath,
      nodeFocus,
      null, 0, 0
    ).iterateNext() : null;

  // MARKDOWN FOR CLEANED LINK ?
  return oLinkNode ?
    '[' + oLinkNode.text + '](' + (
      (
        oLinkNode.getAttribute('data-href') || oLinkNode.href
      ) + ')'
    ) : '';
})();
1 Like

Yes, yours is much more sophisticated – this is just a light single purpose thing for Markdown and plain text (no RTF).

( Not sure whether the hover-without-click variation looks relevant to your application, but the code is there now, if you need it )

1 Like

Great work. Thanks.

(This begins off-topic, but then veers back again at the end.)

If you use Safari, I would highly recommend the “LinkThing” extension:

http://canisbos.com/linkthing

it gives you a lot of great options for controlling how links are handled, either globally or locally (per site). One of the other features is that it will automatically rewrite Google result links so that you will get the direct URL, not the Google URL.

HOWEVER!

If you would like to use Keyboard Maestro to change a Google Search Result URL into a direct URL, you can, now that version 7.0 has the “Clipboard” monitoring.

Create a macro that is triggered when the system clipboard changes, and then add an “IF” statement looking for an URL containing .google.com/url?

If it is found, run this as a shell script:

#!/bin/zsh -f

# change as appropriate. See `man pbpaste` for details 
LANG=en_US.UTF-8

# change as appropriate, if needed
PATH=/usr/local/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin

URL="`pbpaste`"

if (( $+commands[lynx] ))
then
		# use `lynx` if found
	NEW=`curl -sfL "${URL}" \
	| lynx -stdin -dump -listonly -nonumbers \
	| head -1`
else
		# if lynx is not found, try this
	NEW=`curl -sfL "${URL}" \
	| awk -F"'" '/URL/{print $2}'`
fi

if [ "$NEW" != "" ]
then
	echo -n "$URL" | pbcopy
fi

exit 0

Note that the script should be smart enough that if it does not get anything as a response, it will not change the clipboard.

Also note that you are not getting the <title> of the URL, just the URL.

YMMV. Use at your own risk, etc.

4 Likes

Is it possible for this macro to be adapted to copy the address of a link on any page? I prefer this approach than the GUI approach with right click and %Copy Link%Return%.

A web-only approach, alas …

Looks like I read too fast : - )

( thanks to Chris for pointing that out )

Probably won’t get around to it myself this evening, but you should be able to generalise the XPath to something like:

./ancestor-or-self::a

A quick first draft – does that seem to work yet ?

Copy webpage link under cursor.kmmacros (25.0 KB)

1 Like

Hey Rob,

I've tried it in several places, and it works nicely.   :smile:

It can be easily adapted to do something with the link other than place it on the clipboard.

It will not of course handle JavaScript-generated links, so user's should not be surprised by that.

(I don't think there's any way to make a hover-action manage a click-generated action, but I'm not an expert with html or javascript.)

-Chris

Thanks for testing that – I’ll post a version that copies as a Markdown link (with the text)

The link thing extension does not seem to work with this forum using Safari.

It appears to be working for me here with Safari, if I hover my cursor over the various icons and links.

Any particular links that I should try ?

i got it working. I think it was a local issue with my browser which I sorted out by disabling and re-enabling the extension.

Hey Rob, were you able to post that version? My apologies if I missed it somewhere.

Thanks.

I'll take a closer look at the weekend, but in the meanwhile, as far I as can see, I may not have posted this custom plugin action:

Markdownify Search Results in Safari front tab.zip (241.3 KB)

(Which harvests a Safari Google results page as a list of cleaned Markdown links)