Skip to content

Instantly share code, notes, and snippets.

@radiantly
Last active November 18, 2024 06:39
Show Gist options
  • Save radiantly/e1c7319214c77fa007f323fc56cd0239 to your computer and use it in GitHub Desktop.
Save radiantly/e1c7319214c77fa007f323fc56cd0239 to your computer and use it in GitHub Desktop.
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
Copy link

Well so much for "Don't Be Evil"

@aisajib
Copy link

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
Copy link

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
Copy link

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
Copy link

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

@varenc
Copy link

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
Copy link

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

@abctaylor
Copy link

abctaylor commented Sep 27, 2021 via email

@TrashAccount3424
Copy link

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
Copy link

raszpl commented Sep 27, 2021

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

@bradydowling
Copy link

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

@ziotom78
Copy link

Thanks, this is so useful!

@cryptrz
Copy link

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
Copy link

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
Copy link

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.

@NightMachinery
Copy link

NightMachinery 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
Copy link

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)

@n-buna404
Copy link

n-buna404 commented Aug 29, 2022

Thanks for sharing the script, I had found some vanilla methods that also work on firefox 104 which I thought to share here.

i) Right faraway method posted by benczur

"Right-click and hold" on some empty space away from the link. Move your mouse pointer (with right button still depressed) over the link. Release right-click and select 'Copy link location/shortcut'.(Credit to stormenet.)

ii) Inspector method posted by falconepl

Just activate the inspector tool (Shift+Ctrl+C shortcut), then right-click on a given link within Google Search results and select Copy Link Location. Because you've got the inspector tool activated, it would prevent Google's script from replacing original link's location with their own.

Source:https://webapps.stackexchange.com/questions/14120/copy-raw-link-found-by-google-search-not-the-one-with-extra-redirect-added

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