E-Hentai & ExHentai Fade or hide viewed galleries and tag hider

Fade or hide viewed galleries and hide flagged tags on e-hentai and exhentai. Now working with GM4

As of 2017-12-29. See the latest version.

// ==UserScript==
// @name        E-Hentai & ExHentai Fade or hide viewed galleries and tag hider
// @namespace   https://greasyfork.org/users/25356
// @description Fade or hide viewed galleries and hide flagged tags on e-hentai and exhentai. Now working with GM4
// @version     4.8

// @include     https://exhentai.org/*
// @include     https://e-hentai.org/*

// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @require	https://code.jquery.com/jquery-git.min.js

// @grant		GM.getValue
// @grant		GM.setValue
// @grant		GM_getValue
// @grant		GM_setValue

// @author      CoLdAsIcE
// ==/UserScript==

(async () => {
    'use strict';

    let $hideGal = await GM.getValue("EHXFade_hidevisited", false);

    async function isGalVisited(gid) {

        let $md = await GM.getValue("g_" + gid, 0);
        if ($md == 1) {
            return true;
        }
        return false;
    }

    async function visitGal(gid) {
        if (!await isGalVisited(gid)) {
            GM.setValue("g_" + gid, 1);
        }
    }

    function saveViewedGal() {

        if (document.URL.match(/^https?:\/\/(e-|e[^-])hentai\.org\/g\/.*$/)) { //gallery case
            //record visited galleries to local DB
            let $gal_idx = document.URL.match(/^https?:\/\/(?:e-|e[^-])hentai\.org\/g\/(\d*)\/.*$/);

            if (!isNaN($gal_idx[1])) {

                let gal_id = parseInt($gal_idx[1], 10);
                visitGal(gal_id);
            }
        }
    }

    function fadeThumb($tag) {

        let $p = $tag.closest('.id1'); //also fade title in gallery

        let $alpha = parseFloat(0.2);
        if (isNaN($alpha))
            $alpha = '0.2';

        $p.css('opacity', $alpha);

        $p.hover(
            function () {
                $(this).stop().fadeTo('fast', 1);
            },
            function () {
                $(this).stop().fadeTo('fast', $alpha);
            }
        );
    }

    function fadeViewedGal() {
        $('.itg > div.id1').each(function () {
            $(this).find(".id44").each(async () => {
                if (document.URL.match(/^https?:\/\/(e-|e[^-])hentai\.org\/(?!s).*$/) && self == top) { //search / index case, exclude iframe
                    let $tagContainer = $(this);
                    let $gallery = $(this).closest('.id1');

                    let $gal_href = $tagContainer.closest('.id1').find('div.id2 a').first().attr('href');

                    let $gal_idx = $gal_href.match(/^https?:\/\/(?:e-|e[^-])hentai\.org\/g\/(\d*)\/.*$/);

                    if (!isNaN($gal_idx[1])) {
                        let gal_id = parseInt($gal_idx[1], 10);

                        if (await isGalVisited(gal_id)) {
                            if (!$hideGal) {
                                fadeThumb($tagContainer);
                                $gallery.show();
                            } else {
                                $gallery.hide();
                            }
                        }
                    }
                }
            });
        });
    }

    function removeBadTags() {
        $('.itg > div.id1').each(function (ix) {

            let $blankGif = $(this).find(".id3 img").attr('src');
            let $image = $(this);
            if ($blankGif == "https://exhentai.org/img/blank.gif") {
                $image.hide();
            }
        });
    }

    function css() {           
				if(document.URL.match(/^https?:\/\/(e-)hentai\.org/)){
           $('<style type="text/css">' +
            'div.hideGal {width:65px; height:auto; position:fixed; margin-left:-76px; background-color:#EDEBDF; color:#5C0D11; border: 1px solid #5C0D11; z-index: 10; cursor:pointer; }' +
            'div.hideGal div.tab { margin:6px; text-align: center;}' +
            '</style>').appendTo("head");
					}else{
            $('<style type="text/css">' +
            'div.hideGal {width:65px; height:auto; position:fixed; margin-left:-76px; background-color:#4F535B; color:skyblue; border: 1px solid #000000; z-index: 10; cursor:pointer; }' +
            'div.hideGal div.tab { margin:6px; text-align: center;}' +
            '</style>').appendTo("head");
          }
      
         $('<style type="text/css">' +
            'div.id2 a:visited, div.id2 a:visited span {font-weight:normal; color:#4b90eb !important; }' +
            '</style>').appendTo("head");
    }

    function hideVisitedGal() {

        //add a hide gallery tab
        if (!$hideGal) {
            $('div.ido').prepend('<div class="hideGal"><div class="tab" id="toggleseen">Hide</div></div>');
        } else {
            $('div.ido').prepend('<div class="hideGal"><div class="tab" id="toggleseen">Show</div></div>');
        }

        //hide or show gallery
        $('#toggleseen').parent().click(function (e) {

            $hideGal = !$hideGal;

            GM.setValue("EHXFade_hidevisited", $hideGal);

            if ($hideGal) {
                $("#toggleseen").text("Show");
            } else {
                $("#toggleseen").text("Hide");
            }

            applyChanges();

        });
    }

    function applyChanges() {
        fadeViewedGal();
    }

    css();
    hideVisitedGal();

    saveViewedGal();
    fadeViewedGal();
    removeBadTags();

})();