Untitled

mail@pastecode.io avatarunknown
javascript
a month ago
2.4 kB
4
Indexable
Never
// ==UserScript==
// @name         KG_HideSpamRaces
// @namespace    http://klavogonki.ru
// @version      0.1
// @description  This script will hide all the races what are created for bad purposes
// @author       Patcher
// @match        *://klavogonki.ru/gamelist/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=klavogonki.ru
// @grant        none
// ==/UserScript==

const profileCount = 5;

// Function to process a single .item element
function processItem(item) {
  const profileElement = item.querySelector('.profile');
  if (profileElement) {
    const profileText = profileElement.textContent.trim();

    // Check if the count for this profile text is greater than or equal to (N)
    if (profileTextCount[profileText] >= profileCount) {
      // Remove all items with the same profile text
      document.querySelectorAll('#gamelist .item').forEach(itemToRemove => {
        const itemProfileElement = itemToRemove.querySelector('.profile');
        if (itemProfileElement && itemProfileElement.textContent.trim() === profileText) {
          itemToRemove.remove();
        }
      });

      // Remove the associated comment
      const commentSibling = item.nextElementSibling;
      if (commentSibling && commentSibling.nodeType === Node.COMMENT_NODE) {
        commentSibling.remove();
      }
    }

    // Increment the count for this profile text in the object
    profileTextCount[profileText] = (profileTextCount[profileText] || 0) + 1;
  }
}

// Create an object to store the count of each profile text
const profileTextCount = {};

// Iterate through existing .item elements
document.querySelectorAll('#gamelist .item').forEach(item => {
  processItem(item);
});

// Create a MutationObserver to watch for changes in #gamelist
const observer = new MutationObserver(mutationsList => {
  mutationsList.forEach(mutation => {
    if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
      // Iterate through newly added .item elements
      mutation.addedNodes.forEach(addedNode => {
        if (addedNode.nodeType === 1 && addedNode.classList.contains('item')) {
          processItem(addedNode);
        }
      });
    }
  });
});

// Start observing #gamelist for changes
const gamelist = document.getElementById('gamelist');
observer.observe(gamelist, { childList: true, subtree: true });