Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prevent Google from mangling links on the search results when clicking or copying on Firefox
// ==UserScript==
// @name Prevent link mangling on Google
// @namespace LordBusiness.LMG
// @match https://www.google.com/search
// @grant none
// @version 1.1
// @author radiantly
// @description Prevent google from mangling the link when copying or clicking the link on Firefox
// ==/UserScript==
/*
* If you're on Firefox, you might have noticed that when you try to click (or copy) a link from
* a google search result, it redirects to an intermediate page instead of taking you immediately
* to the search result. This is specifically annoying when trying to copy a google search result
* to send to someone else, because it gives you the mangled google-ified link instead.
*
* The same does not happen on Google Chrome or other chromium browsers. Maybe someone can test
* this on Safari?
*
* To install this script, you'll need a user script manager like ViolentMonkey, after which you
* can click the Raw button (gist.github.com) to install.
*/
(function() {
/*
* The following 3 lines simply prevent the mousedown event from propagating to the respective
* event listeners attached to the various link elements.
*
* On testing, this does not seem to break any actual functionality on the site.
*/
window.addEventListener("mousedown", (event) => {
event.stopImmediatePropagation();
}, true);
})();
@tomlazar

This comment has been minimized.

Copy link

@tomlazar tomlazar commented Sep 27, 2021

Well so much for "Don't Be Evil"

@aisajib

This comment has been minimized.

Copy link

@aisajib aisajib commented Sep 27, 2021

The same does not happen on Google Chrome or other chromium browsers. Maybe someone can test this on Safari?

Same as Firefox on Safari 15.

@varenc

This comment has been minimized.

Copy link

@varenc varenc commented Sep 27, 2021

This doesn't happen in Chrome because it supports the ping attribute on <a> tags. More info on that here and here.

The ping attribute is basically adds link tracking into the browser. When a user clicks on <a> tag in Chrome with a ping attr, the link gets clicked like normal but the browser also sends a non-blocking logging request in the background to the ping url.

@Chaz6

This comment has been minimized.

Copy link

@Chaz6 Chaz6 commented Sep 27, 2021

This doesn't happen in Chrome because it supports the ping attribute on <a> tags. More info on that here and here.

The ping attribute is basically adds link tracking into the browser. When a user clicks on <a> tag in Chrome with a ping attr, the link gets clicked like normal but the browser also sends a non-blocking logging request in the background to the ping url.

How does ping work when copying & pasting a link?

@abctaylor

This comment has been minimized.

Copy link

@abctaylor abctaylor commented Sep 27, 2021

does ping get blocked/ignored when do-not-track is set in the request?

@varenc

This comment has been minimized.

Copy link

@varenc varenc commented Sep 27, 2021

How does ping work when copying & pasting a link?

When copying an <a> tag's link the ping request isn't made, but on Google their JS of course will detect your copying and send a background logging/ping request anyway.

does ping get blocked/ignored when do-not-track is set in the request?

In Chrome there's no way built-in way to turn off ping requests. uBlock Origin can do it though and I highly recommend it: https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm

@dandanua

This comment has been minimized.

Copy link

@dandanua dandanua commented Sep 27, 2021

An alternative solution, implemented as an addon https://github.com/dandanua/copy-true-link/blob/main/content.js

@abctaylor

This comment has been minimized.

Copy link

@abctaylor abctaylor commented Sep 27, 2021

@TrashAccount3424

This comment has been minimized.

Copy link

@TrashAccount3424 TrashAccount3424 commented Sep 27, 2021

An alternative solution, implemented as an addon https://github.com/dandanua/copy-true-link/blob/main/content.js

And here an alternative solution as bookmarklet

javascript:(function(){window.addEventListener("mousedown",(event)=>{event.stopImmediatePropagation();},true);})()

Store this as bookmark (URL) drag in into the bookmark toolbar and click it once before you copy a link
(yes you have to remember click it but nothing to install which I prefer)

@raszpl

This comment has been minimized.

Copy link

@raszpl raszpl commented Sep 27, 2021

You want to add same handler for 'focus' event. Thats what Google uses on Image search.

@bradydowling

This comment has been minimized.

Copy link

@bradydowling bradydowling commented Sep 27, 2021

One reason I would highly recommend DuckDuckGo (and the ddgr CLI). You could also use the googler CLI.

@ziotom78

This comment has been minimized.

Copy link

@ziotom78 ziotom78 commented Sep 28, 2021

Thanks, this is so useful!

@cryptrz

This comment has been minimized.

Copy link

@cryptrz cryptrz commented Sep 28, 2021

It happens on the standard version of Firefox but not on Firefox Nightly, maybe the problem is not Google itself

@qezz

This comment has been minimized.

Copy link

@qezz qezz commented Sep 28, 2021

On TamperMonkey, I managed to make it work with the @include statement instead of @match

// @include     https://www.google.com/search*

UPD: Thanks for the great script!

@cgtyoder

This comment has been minimized.

Copy link

@cgtyoder cgtyoder commented Sep 28, 2021

This is not working for me - I installed with GreaseMonkey on FF 92.0.1 and GM says the script is enabled, but copying links from google search results still results in that intermediate URL. Suggestions?

EDIT: Ok, just saw @qezz's comment - making that change fixed it for me.

@NightMachinary

This comment has been minimized.

Copy link

@NightMachinary NightMachinary commented Oct 3, 2021

Any way to do this on iOS Safari? Perhaps with their new extension API?

Update: The bookmarklet works, but is needs to be triggered each time manually which sucks.

@outloudvi

This comment has been minimized.

Copy link

@outloudvi outloudvi commented Oct 12, 2021

Also, you may also want other regional sites of Google to be included:

(co* instead of com* is a workaround for google.co.jp)

// @match       https://www.google.co*/search

(P.S. Check qezz's comment if this does not work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment