Greasy Fork is available in English.

SteamGifts: Look for similar

Adds a button to search for a similar giveaways.

Version au 10/03/2016. Voir la dernière version.

// ==UserScript==
// @name         SteamGifts: Look for similar
// @namespace    lainscripts_sg_look_for_similar
// @version      1.5
// @description  Adds a button to search for a similar giveaways.
// @author       lainverse
// @match        http://www.steamgifts.com/
// @match        http://www.steamgifts.com/giveaway/*
// @match        http://www.steamgifts.com/giveaways/*
// @grant        none
// ==/UserScript==
/* jshint esnext: true */
(function() {
    'use strict';

    (function(st) {
        st.id = 'sg_look_for_similar_stylesheet';
        document.head.appendChild(st);
        st.sheet.insertRule('.featured__heading .fa-search { font-size: 2em }', 0);
    })(document.createElement('style'));

    var clHeader = '.giveaway__heading__name,.table__column__heading,.featured__heading__medium',
        clHeaderIns = ['giveaway__heading__name','table__column__heading'],
        caHeaderSib = ['giveaway__heading__thin','featured__heading__small'],
        loc = location.pathname.replace(/\/search$/i,'');
    loc = loc==='/'||/^\/giveaway(\/|s\/new)/i.test(loc)?'/giveaways':loc;

    function ofClass(itm, lst) {
        var i = lst.length;
        while(i--)
            if (itm.classList.contains(lst[i]))
                return true;
        return false;
    }

    function appendLFSLink(he) {
        var heLink = he.pathname ? he : he.querySelector('a'),
            heText = he.textContent;
        
        // Do not parse invite-only giveaways and non-giveaway links
        if (!he || heText == 'Invite Only' || 
            (heLink && !/^\/giveaway\//.test(heLink.pathname)))
            return;
        
        // Do not add LFS button to a node with already existing LFS button
        if (he.parentNode.querySelector('.fa-search'))
            return;
        
        // Create LFS button
        var ea = document.createElement('a'),
            ei = document.createElement('i');
        ei.className = 'giveaway__icon fa fa-search';
        // Remove number of copies joined witht the title in some lists
        // and ellipsis from the long title names.
        heText = heText.replace(/\s\(\d+\sCopies\)$/i,'').replace(/\.\.\.$/,'');
        ea.href = loc + '/search?q=' + encodeURIComponent(heText);
        ea.appendChild(ei);       
        while (he.nextElementSibling && ofClass(he.nextElementSibling, caHeaderSib))
            he = he.nextElementSibling;
        if (ofClass(he, clHeaderIns))
            he.appendChild(ea);
        else
            he.parentNode.insertBefore(ea, he.nextElementSibling);
    }

    (function(he) {
        var i = he.length;
        while (i--)
            appendLFSLink(he[i]);
    })(document.querySelectorAll(clHeader));

    function parseAddedNodes(m, he) {
        var i = m.length;
        while (i--) {
            if (!m[i].querySelector)
                continue;
            var hes = m[i].querySelectorAll(clHeader), j = hes.length;
            while (j--)
                appendLFSLink(hes[j]);
        }
    }

    var obs = new MutationObserver(function(ms){
        var i = ms.length;
        while (i--)
            if (ms[i].addedNodes.length)
                parseAddedNodes(ms[i].addedNodes);
    });

    obs.observe(document.body, {childList: true, subtree: true});
})();