Untitled

mail@pastecode.io avatar
unknown
javascript
8 months ago
3.0 kB
5
Indexable
Never
// Constants for the threshold and time limits
const itemThreshold = 1; // Number of items
const timeLimitInSeconds = 1; // Time in seconds

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

    // Initialize the profileTextCount object for this profile text
    if (!profileTextCount[profileText]) {
      profileTextCount[profileText] = {
        count: 0,
        lastTimestamp: 0,
        itemsToRemove: [],
      };
    }

    const currentTime = Date.now();
    const lastTimestamp = profileTextCount[profileText].lastTimestamp;

    // Check if the user has created more than itemThreshold items with the same profile text within the time limit
    if (profileTextCount[profileText].count >= itemThreshold && currentTime - lastTimestamp <= timeLimitInSeconds * 1000) {
      // Add the item to the list of items to remove
      profileTextCount[profileText].itemsToRemove.push(item);

      // Debugging: Log the removed item and profile text
      console.log('Removed item with profile text:', profileText);
    } else {
      // Debugging: Log the item that was not removed
      console.log('Item not removed with profile text:', profileText);
    }

    // Update the count and timestamp for this profile text in the object
    profileTextCount[profileText].count += 1;
    profileTextCount[profileText].lastTimestamp = currentTime;

    // Remove all items associated with this profile text
    profileTextCount[profileText].itemsToRemove.forEach(itemToRemove => {
      itemToRemove.remove();

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

    // Clear the list of items to remove
    profileTextCount[profileText].itemsToRemove = [];
  }
}

// Create an object to store the count and timestamp 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 });