LdizL / Reddit Hide All

// ==UserScript==
// @name          Reddit Hide All
// @namespace     http://userscripts.org/users/dbeck
// @updateURL https://openuserjs.org/meta/LdizL/Reddit_Hide_All.meta.js
// @description   Adds a button next to the logo to Hide All. Idea based off of: http://userscripts.org/users/dbeck http://douglasbeck.com/scripts/reddit/
// @include       http://reddit.com/*
// @include       http://*.reddit.com/*
// @include       https://www.reddit.com/
// @include       https://www.reddit.com/*
// @include       https://*.reddit.com/*
// @exclude       http://www.reddit.com/comscore-iframe/*
// @exclude       http://static.reddit.com/ads/*
// @version       1
// @date          2018-02-09 20:40 Eastern
// @creator       alpha1125
// @grant GM_addStyle
// @license MIT
// ==/UserScript==


var uh;

(function() {
    'use strict';


    function getModhash(){

        $.ajax({
            url:'/api/me.json',
            type: 'get',
            dataType: 'json',
            success: function(data){
                uh = data.data.modhash;
            }
        });
    }

    getModhash();


    function hideAllAction(){
        var $stories = $('div[data-fullname]');
        var $hideTheseIDs = '';
        // could filter it, so only beginning with t3_.
        var first=true;
        for(var i=0; i < $stories.length; i++){

            var fullname = $($stories[i]).data('fullname');
            if(first) {
                $hideTheseIDs = 'id='+fullname;
                first=false;
            } else {
                $hideTheseIDs += ','+fullname;
            }


        }
        var $payload = $hideTheseIDs + '&executed=hidden&uh=' + uh;

        $.ajax({
            url: '/api/hide',
            type: "POST",
            data: $payload,
            success: function(data){
                location.reload();
            }
        });

    }

    function addMenuItem(id, label){
        var menuItem=`<li><a href="#" id="${id}">${label}</a></li>`;
        $('.tabmenu').prepend(menuItem);
        attachClickEvent('#'+id);
    }

    function attachClickEvent(selector){
        $(selector).click(function(){
            hideAllAction();
        });
    }

    addMenuItem('reddit-hide-all', 'hide all');
    $("div.nav-buttons").append('<br/><span class="nextprev"><a href="#" id="reddit-hide-all3" rel="nofollow next">hide all</a></span>');
    attachClickEvent('#reddit-hide-all3');



})();