Skip to content

Instantly share code, notes, and snippets.

@Aikufurr
Created September 29, 2019 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aikufurr/14f6f0734a3f686c89b030ac56de5ae9 to your computer and use it in GitHub Desktop.
Save Aikufurr/14f6f0734a3f686c89b030ac56de5ae9 to your computer and use it in GitHub Desktop.
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