Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replaces the long URL with a simpler shorter one
// ==UserScript==
// @name Amazon URL Shortener
// @namespace https://gist.github.com/Aikufurr
// @version 0.1
// @description Replaces the long URL with a simpler shorter one
// @author Aikufurr
// @match https://www.amazon.co.uk/*
// @match https://www.amazon.com/*
// @match https://www.amazon.de/*
// @match https://www.amazon.co.jp/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var shortURL = /(dp\/.*?-?[^a-zA-Z0-9])/g;
var currentURL = window.location.href;
var website = /(amazon.*?\/)/g;
var domain = "https://www.";
if (!currentURL.match(website)[0].endsWith("/")){
domain += currentURL.match(website)[0] + "/"
} else {
domain += currentURL.match(website)[0]
}
var longURL = /(amazon.*\/[a-zA-Z0-9].*\/dp)/g;
if (longURL.test(currentURL)){
if (shortURL.test(currentURL)){
var newURL = domain + currentURL.match(shortURL)[0].substring(0,currentURL.match(shortURL)[0].length-1);
} else {
var newURL = domain + currentURL.match(/(dp.*)/g)[0].substring(0,currentURL.match(/(dp.*)/g)[0].length-1);
}
window.history.pushState(newURL, "Title", newURL);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.