Gelbooru Saved Search Respect Tag Blacklist

Properly hides image thumbnails that have tags that have user-blacklisted tags on Saves Searches page.

As of 2020-06-12. See the latest version.

// ==UserScript==
// @name         Gelbooru Saved Search Respect Tag Blacklist
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  Properly hides image thumbnails that have tags that have user-blacklisted tags on Saves Searches page.
// @author       Xerodusk
// @homepage     https://greasyfork.org/en/users/460331-xerodusk
// @include      https://gelbooru.com/index.php*s=saved_search*
// @grant        none
// @icon         https://gelbooru.com/favicon.png
// ==/UserScript==
/* jshint esversion: 6 */

// Get cookie by name
// From https://www.w3schools.com/js/js_cookies.asp
function getCookie(cname) {
    'use strict';

    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

// Get tag blacklist as list of strings
function GetBlockedTags() {
    'use strict';

    // Get blocked tags string from cookie
    let blockedTags = getCookie('tag_blacklist');

    // Split tags into list
    blockedTags = blockedTags.split('%20');

    return blockedTags;
}

// Get tags list for image thumbnail as list of strings
function GetImageTags(imageThumb) {
    'use strict';

    let tagsString = imageThumb.getElementsByClassName('thumbnail-preview')[0].getAttribute('alt');
    let tagsList = tagsString.split(' ');

    return tagsList;
}

// Mark images as blacklisted
function MarkBlacklistedImages(blockedTags) {
    'use strict';

    // Get all image thumbnails on page
    let imageThumbs = document.querySelectorAll('.container-fluid > span.thumb');

    // Apply blacklist to image thumbnails
    imageThumbs.forEach(imageThumb => {
        let tags = GetImageTags(imageThumb);
        if (tags.some(tag => blockedTags.indexOf(tag) >= 0)) {
            imageThumb.classList.add('blacklisted');
        }
    });
}

(function() {
    'use strict';

    let blockedTags = GetBlockedTags();
    if (blockedTags) {
        MarkBlacklistedImages(blockedTags);
    }
})();