thomper / uk.match.com Search Page User Hider

// ==UserScript==
// @name            uk.match.com Search Page User Hider
// @namespace       http://ontbee.com/gmscripts
// @description     Adds a button above user nicknames on the search page, clicking it hides that user from search results permanently.  Hidden users are remembered between browsing sessions.  The hidden users will still appear on other pages, e.g. if they wink at you.
// @license         MIT
// @version         1.0
// @include         http://uk.match.com/d/search*
// @include         https://uk.match.com/d/search*
// @grant           GM_getValue
// @grant           GM_setValue
// @noframes
// ==/UserScript==

var USER_ID_REGEX = /profile-display\/(\d+)/;

var blockedUsers = deserialise("blockedUsers");


// ----- functions for blocking users and storing the blocklist -----

function deserialise(name) {
    return eval(GM_getValue(name, ('({})')));
}

function serialise(name, val) {
    GM_setValue(name, uneval(val));
}

function blockUser(userID, nickname) {
    blockedUsers[userID] = nickname;
    serialise("blockedUsers", blockedUsers);
}


// ----- functions for managing user cards -----

function getInfoDiv(cardDiv) {
    return cardDiv.getElementsByClassName("member-card__info")[0];
}

function getNicknameAnchor(cardDiv) {
    return getInfoDiv(cardDiv).getElementsByClassName("member-card__info-nickname")[0];        
}

function buttonAdded(cardDiv) {
    return getInfoDiv(cardDiv).getElementsByClassName("gs-hide-button").length !== 0;
}

function getButtonDiv(cardDiv) {
    return getInfoDiv(cardDiv).getElementsByClassName("gs-hide-button")[0];
}

function getUserID(cardDiv) {
    var href = getNicknameAnchor(cardDiv).getAttribute("href");
    return USER_ID_REGEX.exec(href)[1];
}

function getNickname(cardDiv) {
    return getNicknameAnchor(cardDiv).getElementsByClassName("member-card__info-nickname-string")[0].innerHTML;
}

function removeCard(cardDiv) {
    var gridItemDiv = cardDiv.parentElement;
    gridItemDiv.parentElement.removeChild(gridItemDiv);
}

function addHideButton(cardDiv) {
    var infoDiv = getInfoDiv(cardDiv);
    if (!buttonAdded(cardDiv)) {
        infoDiv.innerHTML = "<a class=\"gs-hide-button\">🙈&nbsp;<a>" + infoDiv.innerHTML;
        var buttonDiv = getButtonDiv(cardDiv);
        buttonDiv.addEventListener("click", (e) => { hideButtonClickHandler(cardDiv, e); });
    }
}

function hideButtonClickHandler(cardDiv, e) {
    blockUser(getUserID(cardDiv), getNickname(cardDiv));
    removeCard(cardDiv);
    triggerResize();
}


// ----- functions for manipulating the web page -----

function triggerResize() {
    window.dispatchEvent(new Event('resize'));
}


function addAllHideButtons() {
    var cards = document.getElementsByTagName("member-card");
    for (var i = 0; i < cards.length; i++) {
        addHideButton(cards[i]);
    }
}

function hideBlockedUsers() {
    var cardDivs = document.getElementsByTagName("member-card");
    for (var i = 0; i < cardDivs.length; i++) {
        var cardDiv = cardDivs[i];
        if (getUserID(cardDiv) in blockedUsers) {
            removeCard(cardDiv);
        }
    }
    triggerResize();
}


// ----- this function starts the loops which add hide buttons and hide blocked users -----

window.addEventListener("load", function() {
    setInterval(addAllHideButtons, 2000);
    setInterval(hideBlockedUsers, 2000);
});