Raw Source
AlD / Amazon: Remove Sponsored Items

// ==UserScript==
// @name         Amazon: Remove Sponsored Items
// @license      BSD-3-Clause
// @namespace    https://gist.github.com/AlD/cd505481d2972be02ce246e5368b08e8
// @updateURL    https://gist.github.com/AlD/cd505481d2972be02ce246e5368b08e8/raw/amazon-remove-sponsored-items.user.js
// @downloadURL  https://gist.github.com/AlD/cd505481d2972be02ce246e5368b08e8/raw/amazon-remove-sponsored-items.user.js
// @version      0.9.4
// @description  Amazon: Remove sponsored items in search results
// @author       Daniel Albers <daniel@lbe.rs>
// @match        https://*.amazon.de/*
// @match        https://*.amazon.com/*
// @match        https://*.amazon.co.uk/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const logPrefix = '[Amazon: Remove Sponsored Items]';
    const settingsKey = 'arsi_settings';
    const actionConfigName = 'action';
    const actionSettingsKey = [settingsKey, actionConfigName].join('_');

    const actions = new Map([
        ['remove', e => {e.remove()}],
        ['colorize', e => {e.style.background = 'lightgrey'}],
    ]);

    let configPrompt = (ak) => {
        let f = document.createElement('form');
        f.method = 'dialog';
        f.style.textTransform = 'capitalize';

        for (let k of actions.keys()) {
            let l = document.createElement('label');
            let r = document.createElement('input');
            r.type = 'radio';
            r.name = actionConfigName;
            r.value = k;
            if (k == ak) {
                r.checked = 'checked';
            }
            l.appendChild(r);
            l.appendChild(document.createTextNode(k));
            l.appendChild(document.createElement('br'));
            f.appendChild(l);
        }
        let s = document.createElement('input');
        s.type = 'submit';
        f.appendChild(s);

        let d = document.createElement('dialog');
        d.appendChild(f);
        d.addEventListener('close', (e) => {
            let v = e.currentTarget.querySelector('form')[actionConfigName].value;
            GM_setValue(actionSettingsKey, v);
        });
        window.document.body.appendChild(d);
        d.showModal();
    };

    let actionKey = () => { return GM_getValue(actionSettingsKey) };

    GM_registerMenuCommand("Reconfigure", configPrompt.bind(null, actionKey()), "C");

    if (!actions.has(actionKey())) {
        console.log(logPrefix + ' Config missing');
        configPrompt(null);
        return;
    }
    let action = actions.get(actionKey());

    let i = 0;
    document.querySelectorAll(".AdHolder").forEach(e => {
        action(e);
        i++;
    })
    document.querySelectorAll(".s-sponsored-header").forEach(e => {
        action(e.closest('li'));
        i++;
    })

    actionKey = actionKey();
    console.log([
        logPrefix,
        [ actionKey.charAt(0).toUpperCase(), actionKey.slice(1), 'd' ].join(''),
        i,
        'sponsored items',
    ].join(' '));
})();