Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion to shorten Amazon links further #333

Open
Johnnycyan opened this issue Nov 23, 2023 · 4 comments
Open

Suggestion to shorten Amazon links further #333

Johnnycyan opened this issue Nov 23, 2023 · 4 comments

Comments

@Johnnycyan
Copy link

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

This is really good!

@nascentt
Copy link

nascentt commented Mar 9, 2024

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

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

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);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants