Greasy Fork is available in English.

Mass Delete Facebook Messenger Tool - WORDLY FREE by Darkside

Mass Delete Messages

// ==UserScript==
// @name         Mass Delete Facebook Messenger Tool - WORDLY FREE by Darkside
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Mass Delete Messages
// @author       Projekt Darkside - DevIT Brno - EU
// @match        https://www.facebook.com/messages/*
// @grant        GM_addStyle
// @license      MIT; https://opensource.org/licenses/MIT
// @require      https://code.jquery.com/jquery-3.7.1.min.js
// ==/UserScript==

/* global $ */

// ███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████
// ███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████

(function() {
    'use strict';

    // Add CSS for the icon in the bottom right
    GM_addStyle(`
        #cleanerIcon {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: #0078D4;
            color: white;
            border-radius: 50%;
            padding: 10px;
            font-size: 24px;
            cursor: pointer;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        #cleanerIcon:hover {
            background-color: #005a8f;
        }
    `);

    // Function to create and append the icon if not already present
    const createIcon = () => {
        // Check if the icon already exists
        if (!document.getElementById('cleanerIcon')) {
            const icon = document.createElement('div');
            icon.id = 'cleanerIcon';
            icon.textContent = '🧹'; // You can use any icon here
            document.body.appendChild(icon);

            // Add click event listener to trigger the script
            icon.addEventListener('click', () => {
                console.log('Starting Facebook Chat Cleaner...');
                startInterval(); // Start the interval for cleaning and removing
            });
        }
    };

    // Function to remove unwanted elements
    const SELECTORS_REMOVE = [
        "div[class='xds687c x1pi30zi x1e558r4 xixxii4 x13vifvy xzkaem6']:eq(0)",
        "div[class='layout-footer']:eq(0)"
    ];

    const REMOVE_INTERVAL = 1200; // Interval for repeating the cleaning process
    let removedCount = 0; // Counter for removed elements

    // Language mappings for various labels
    const LANG_MAP = {
        menu: {
            en: "Menu",
            cs: "Nabídka",
            sk: "Ponuka",
            fr: "Menu",
            es: "Menú",
            de: "Menü",
            it: "Menu",
            pt: "Menu",
            ru: "Меню",
            ja: "メニュー"
        },
        deleteChat: {
            en: "Delete Chat",
            cs: "Odstranění chatu",
            sk: "Odstránenie chatu",
            fr: "Supprimer la discussion",
            es: "Eliminar chat",
            de: "Löschen Chat",
            it: "Elimina chat",
            pt: "Excluir chat",
            ru: "Удалить чат",
            ja: "チャットを削除"
        },
        confirmDelete: {
            en: "Delete chat",
            cs: "Odstranit chat",
            sk: "Odstrániť chat",
            fr: "Supprimer le chat",
            es: "Eliminar el chat",
            de: "Chat löschen",
            it: "Elimina chat",
            pt: "Excluir o chat",
            ru: "Удалить чат",
            ja: "チャットを削除"
        }
    };

    // Function to clean unwanted elements
    const cleanElements = () => {
        $(SELECTORS_REMOVE.join(", ")).each((index, element) => {
            console.log('%c  Removing element ', 'background: black; color: yellow;', element);
            $(element).remove();
            removedCount++;
        });
        console.log(`%c Total elements removed: ${removedCount}`, 'background: black; color: cyan; font-size: 14px;');
    };

    // Function to handle chat removal process
    const removeChats = async () => {
        console.log("Starting chat removal process...");

        try {
            // Step 1: Click "Menu" (localized)
            const menuButtonLabel = Object.values(LANG_MAP.menu).map(label => `[aria-label^='${label}']:eq(0)`).join(", ");
            const menuButton = $(menuButtonLabel);
            if (menuButton.is(':visible')) {
                console.log("Kliknutí na Nabídka - 1");
                menuButton.click();
                await wait(1000);
            }

            // Step 2: Click "Delete Chat" (localized)
            const deleteButtonLabel = Object.values(LANG_MAP.deleteChat).map(label => `span:contains('${label}')`).join(", ");
            const $deleteBtn = $(deleteButtonLabel + ":eq(0)");
            if ($deleteBtn.is(':visible')) {
                console.log("Kliknutí na SMAZAT - 2");
                $deleteBtn.click();
                await wait(1000);
            } else {
                console.warn("Tlačítko 'Odstranění chatu' není viditelné nebo není v podporovaném jazyce.");
                return;
            }

            // Step 3: Confirm deletion (localized)
            const confirmButtonLabel = Object.values(LANG_MAP.confirmDelete).map(label => `span:contains('${label}')`).join(", ");
            const $confirmBtn = $(confirmButtonLabel + ":eq(0)");
            if ($confirmBtn.is(':visible')) {
                console.log("Potvrzení Smazání - 3");
                $confirmBtn.click();
                removedCount++;
                console.log(`%c Chats removed: ${removedCount}`, 'background: black; color: lime; font-size: 14px;');
                await wait(1000);
            } else {
                console.warn("Tlačítko 'Potvrdit' není viditelné nebo není v podporovaném jazyce.");
            }

        } catch (error) {
            console.error("Chyba při mazání chatu: ", error);
        }
    };

    // Utility function for delay
    const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));

    // Periodic cleaning and removal
    const startInterval = () => {
        setInterval(() => {
            cleanElements(); // Clean unwanted elements
            removeChats();   // Trigger chat removal process
        }, REMOVE_INTERVAL);
    };

    // Check if we are on the Facebook messages page
    const isFacebookMessagesPage = () => window.location.hostname === "www.facebook.com" && window.location.pathname.startsWith("/messages/");

    // Function to check and handle icon visibility based on URL
    const checkPageAndCreateIcon = () => {
        if (isFacebookMessagesPage()) {
            createIcon(); // Create the icon only on the message page
        } else {
            const icon = document.getElementById('cleanerIcon');
            if (icon) {
                icon.remove(); // Remove icon if we're not on the correct page
            }
        }
    };

    // Constantly check every second (1000ms)
    setInterval(checkPageAndCreateIcon, 1000);

    console.log('%c Script initialized and running...', 'background: black; color: green; font-size: 14px;');
})();

// ███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████
// ███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████