Startpage - Remove Google Merchant Center Tracking Parameters

Remove Google Merchant Center query parameters from links and their corresponding text on Startpage.com results

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Startpage - Remove Google Merchant Center Tracking Parameters
// @version      1.1.2
// @description  Remove Google Merchant Center query parameters from links and their corresponding text on Startpage.com results
// @namespace    August4067
// @author       August4067
// @match        https://www.startpage.com/*
// @license      MIT
// @grant        none
// @icon         https://www.startpage.com/favicon.ico
// @require      https://update.greatest.deepsurf.us/scripts/502635/1422102/waitForKeyElements-CoeJoder-fork.js
// ==/UserScript==

/**
 * Removes the 'srsltid' parameter from a URL if it exists.
 * @param {string} url - The original URL.
 * @returns {string} - The cleaned URL.
 */
function removeSrsltidParam(url) {
  const urlObj = new URL(url);

  if (urlObj.searchParams.has("srsltid")) {
    urlObj.searchParams.delete("srsltid");
    return urlObj.toString();
  }

  return url;
}

/**
 * Cleans up links on the page by removing 'srsltid' parameters from hrefs and link text.
 */
function cleanUpLinks() {
  console.debug("Cleaning up links");
  const wgl = document.querySelector("div[class~='w-gl']");

  if (wgl) {
    const links = wgl.querySelectorAll("a");

    links.forEach((link) => {
      const originalHref = link.href;
      const newHref = removeSrsltidParam(originalHref);

      if (newHref !== originalHref) {
        console.debug(`Removing srsltid: ${originalHref}`);
        link.href = newHref;
        link.ariaLabel = newHref;
        
        const linkTexts = link.querySelectorAll('span[class~="link-text"]');
        linkTexts.forEach(linkText => {
          if (linkText.innerText === originalHref) {
            linkText.innerText = newHref;
          }
        });
        
        console.debug(`Result: ${newHref}`);
      }
    });
  }
  console.debug("Cleaned up links");
}

(function () {
  "use strict";
  waitForKeyElements("section[id='main']", () => {
    cleanUpLinks();
  }, false);
})();