Raw Source
AlD / Amazon Smile Redirect

// ==UserScript==
// @name         Amazon Smile Redirect
// @license      BSD-3-Clause
// @namespace    https://gist.github.com/AlD/e288d567bc581c97f4a3a4bc1bf5d447
// @updateURL    https://gist.github.com/AlD/e288d567bc581c97f4a3a4bc1bf5d447/raw/amazon-smile-redirect.user.js
// @downloadURL  https://gist.github.com/AlD/e288d567bc581c97f4a3a4bc1bf5d447/raw/amazon-smile-redirect.user.js
// @version      0.3.0
// @description  Redirect from regular Amazon to Amazon Smile
// @author       Daniel Albers <daniel@lbe.rs>
// @match        https://www.amazon.de/*
// @match        https://www.amazon.com/*
// @match        https://www.amazon.co.uk/*
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    const lp = '[Smile Redirect]';
    const lrt = 'lastRedirectTarget';

    let u = window.location;
    let h = u.hostname.split('.');
    console.assert(h[0] == 'www', "URL not matching 'www.*'");
    h[0] = 'smile';
    let r = new URL(u);
    r.hostname = h.join('.');

    if (GM_getValue(lrt) != r.toString()) {
        console.log([lp, 'Redirecting from', u, 'to', r + '.'].join(' '));
        GM_setValue(lrt, r.toString());
        window.location = r;
    } else {
        console.log([
            lp, 'Previous redirect also originated from', u + '.',
            'Not redirecting again to prevent loops.',
        ].join(' '));
    }
})();