ClearURLs / Addon

ClearURLs is an add-on based on the new WebExtensions technology and will automatically remove tracking elements from URLs to help protect your privacy.
http://docs.clearurls.xyz
GNU Lesser General Public License v3.0
4.06k stars 111 forks source link

Suggestion to shorten Amazon links further #333

Open Johnnycyan opened 11 months ago

Johnnycyan commented 11 months ago

When going to a product page with ClearURLs I get the link https://www.amazon.com/Sabrent-Right-Angle-Locking-Drives/dp/B08FTL9XPF

But this could be shortened even further to https://www.amazon.com/dp/B08FTL9XPF

So would it be possible to remove the information before the /dp/ part?

I'm using the extension on Firefox if that matters.

Splarkszter commented 11 months ago

This is really good!

nascentt commented 8 months ago

Whilst I like minimal urls, that seems to go beyond this addon's purpose (https://github.com/ClearURLs/Addon#Features) and might break things for some situations.

It's easy to do yourself with Greasemonkey though.

// ==UserScript==
// @name         Amazon URL Cleaner
// @namespace    http://amazon.com/
// @version      1.0
// @description  Remove section between domain extension and /dp/ in Amazon URLs
// @match        https://www.amazon.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Get the current URL
    var url = window.location.href;

    // Check if the URL is an Amazon product page
    if (url.includes("/dp/")) {
        // Find the positions of the domain extension and "/dp/"
        var domainExtPos = url.indexOf(".com/") + 4;
        var dpPos = url.indexOf("/dp/");

        // Check if there is a section between the domain extension and "/dp/"
        if (dpPos > domainExtPos) {
            // Extract the desired parts of the URL
            var newURL = url.slice(0, domainExtPos) + url.slice(dpPos);

            // Replace the current URL with the cleaned version
            window.history.replaceState({}, document.title, newURL);
        }
    }
})();
Johnnycyan commented 8 months ago

Whilst I like minimal urls, that seems to go beyond this addon's purpose (https://github.com/ClearURLs/Addon#Features) and might break things for some situations.

It's easy to do yourself with Greasemonkey though.

// ==UserScript==
// @name         Amazon URL Cleaner
// @namespace    http://amazon.com/
// @version      1.0
// @description  Remove section between domain extension and /dp/ in Amazon URLs
// @match        https://www.amazon.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Get the current URL
    var url = window.location.href;

    // Check if the URL is an Amazon product page
    if (url.includes("/dp/")) {
        // Find the positions of the domain extension and "/dp/"
        var domainExtPos = url.indexOf(".com/") + 4;
        var dpPos = url.indexOf("/dp/");

        // Check if there is a section between the domain extension and "/dp/"
        if (dpPos > domainExtPos) {
            // Extract the desired parts of the URL
            var newURL = url.slice(0, domainExtPos) + url.slice(dpPos);

            // Replace the current URL with the cleaned version
            window.history.replaceState({}, document.title, newURL);
        }
    }
})();

Thank you that works great!

kaskavalci commented 7 months ago

Thanks @nascentt for the idea!

I found this to be a better script, that also removes URL parameters. Copying source code below for amazon.de

    // ==UserScript==
    // @name        Amazon URL Cleaner DE
    // @description Show the shortest possible URL for Amazon items.
    // @namespace   https://arantius.com/misc/greasemonkey/
    // @match       https://www.amazon.de/dp/*
    // @match       https://www.amazon.de/*/dp/*
    // @match       https://www.amazon.de/gp/product/*
    // @match       https://www.amazon.de/*/ASIN/*
    // @run-at      document-start
    // @version     9
    // @grant       none
    // @icon        https://www.amazon.com/favicon.ico
    // ==/UserScript==

    function getProductId() {
      var m;
      m = document.location.href.match(/(?:.+\/)?dp\/([^/?]+)/);
      if (m) return m[1];
      m = document.location.href.match(/gp\/product\/([^/?]+)/);
      if (m) return m[1];
      m = document.location.href.match(/ASIN\/([^/?]+)/);
      if (m) return m[1];
    }

    var productId = getProductId();
    if (productId) {
      history.replaceState(
          {}, document.title, 'https://www.amazon.de/dp/' + productId);
    }