Greasy Fork

Greasy Fork is available in English.

WhatsApp Web Sohbet Arşivle Butonu

WhatsApp Web sohbet listesine arşivleme simgesi ekler.

// ==UserScript==
// @name WhatsApp Web Sohbet Arşivle Butonu
// @namespace http://tampermonkey.net/ or https://violentmonkey.github.io
// @version 0.6 // Versiyonu 0.6'ya yükseltelim, ikon düzeltmesi
// @description WhatsApp Web sohbet listesine arşivleme simgesi ekler.
// @author CustME
// @match https://web.whatsapp.com/
// @grant none
// @icon data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBzdHJva2U9IiM1NTUiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNMjEgOFY0YTIgMiAwIDAgMC0yLTJIMWEyIDIgMCAwIDAtMi0ydjRtMTggMHYxNGEyIDIgMCAwIDEtMiAySDVhMiAyIDAgMCAxLTItMlY4bTE4IDBIM3ptMTIgNGwtMyAzLTMtMyIvPjwvc3ZnPg==
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // SVG namespace constant
    const svgNS = 'http://www.w3.org/2000/svg';

    // Add archive button to chat element
    function addArchiveButton(chatElement) {
        // Prevent adding button multiple times
        if (chatElement.classList.contains('archive-button-added')) {
            return;
        }
        chatElement.classList.add('archive-button-added');

        // Find time/status container (selector may change!)
        const timeStatusContainer = chatElement.querySelector('._ak8j');
        if (!timeStatusContainer) {
             console.warn('WhatsApp: Time/status container (._ak8j) not found:', chatElement);
             return;
        }

        // Check if button already exists in container
         if (timeStatusContainer.querySelector('.whatsapp-archive-button')) {
             return;
         }

        // Create button
        const button = document.createElement('button');
        button.className = 'whatsapp-archive-button';
        button.title = 'Sohbeti Arşivle';
        button.style.cssText = `
            width: 18px;
            height: 18px;
            min-width: 18px;
            background-color: #e9e9eb;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            color: #555;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0;
            margin-left: 5px;
            opacity: 0.8;
            transition: opacity 0.1s ease-in-out;
            flex-shrink: 0;
        `;

        // Create archive icon SVG
        const svg = document.createElementNS(svgNS, 'svg');
        svg.setAttribute('viewBox', '0 0 24 24'); // SVG'nin orijinal boyutları
        svg.setAttribute('width', '100%'); // Butonun içine sığması için genişlik
        svg.setAttribute('height', '100%'); // Butonun içine sığması için yükseklik
        svg.setAttribute('fill', 'none'); // Simgenin iç dolgusu yok
        svg.setAttribute('stroke', 'currentColor'); // Rengi butonun yazı renginden al
        svg.setAttribute('stroke-width', '2'); // Çizgi kalınlığı
        svg.setAttribute('stroke-linecap', 'round'); // Çizgi bitişleri yuvarlak
        svg.setAttribute('stroke-linejoin', 'round'); // Çizgi köşeleri yuvarlak

        // Define icon shape
        const path = document.createElementNS(svgNS, 'path');
        path.setAttribute('d', 'M21 8V4a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v4m18 0v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8m18 0H3m12 4l-3 3-3-3');

        // Append SVG to button
        svg.appendChild(path);
        button.appendChild(svg);

        // Hover effect
        button.addEventListener('mouseover', () => { button.style.opacity = '1'; });
        button.addEventListener('mouseout', () => { button.style.opacity = '0.8'; });

        // Add click listener to button
        button.addEventListener('click', (event) => {
            // Prevent interference with chat item click
            event.stopPropagation();
            event.preventDefault();

            const targetChat = event.target.closest('._ak8l');
            if (!targetChat) return;

            // Simulate Right-Click and Menu Item Click
            const chatRect = targetChat.getBoundingClientRect();
            const contextMenuEvent = new MouseEvent('contextmenu', {
                bubbles: true,
                cancelable: true,
                // Position click near element for correct menu placement
                clientX: chatRect.left + chatRect.width - 20,
                clientY: chatRect.top + chatRect.height / 2
            });
            targetChat.dispatchEvent(contextMenuEvent);

            // Wait for menu and click Archive item
            setTimeout(() => {
                // Find context menu container (selector may change!)
                const contextMenu = document.querySelector('._ak4w');

                if (contextMenu) {
                    // Find 'Sohbeti arşivle' menu item (selector/aria-label may change!)
                    const archiveMenuItem = contextMenu.querySelector('li div[aria-label="Sohbeti arşivle"]');

                    if (archiveMenuItem) {
                        archiveMenuItem.click();
                        console.log('WhatsApp: Archive button clicked, menu item found and clicked.');
                    } else {
                         console.error('WhatsApp: "Sohbeti arşivle" menu item not found.');
                    }
                } else {
                     console.error('WhatsApp: Context menu container (._ak4w) not found.');
                }
            }, 50); // Delay to allow menu render

        });

        // Append button to time/status container
        timeStatusContainer.appendChild(button);
    }

    // --- MutationObserver to watch for chat list changes ---

    // Selector for main chat list container (may change with WA updates!)
    const chatListContainerSelector = 'div[aria-label="Sohbet listesi"][role="grid"]';

    // Observer to add buttons to new chat elements
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === 1) { // If added node is an element
                         // Check node itself or its children for chat elements
                         if (node.classList && node.classList.contains('_ak8l')) {
                             addArchiveButton(node);
                         }
                         if (node.querySelectorAll) {
                             node.querySelectorAll('._ak8l').forEach(addArchiveButton);
                         }
                    }
                });
            }
        });
    });

    // Find container and start observing
    function findContainerAndObserve() {
        const chatListContainer = document.querySelector(chatListContainerSelector);
        if (chatListContainer) {
            console.log('WhatsApp: Chat list container found. Observing and adding buttons to existing chats.');
            // Add buttons to chats already present on load
            chatListContainer.querySelectorAll('._ak8l').forEach(addArchiveButton);
            // Start observing for new chats (direct children)
            observer.observe(chatListContainer, { childList: true, subtree: false });

        } else {
            console.log('WhatsApp: Chat list container (', chatListContainerSelector, ') not found. Retrying...');
            // Retry if container not found immediately
            setTimeout(findContainerAndObserve, 1000); // Retry every 1 second
        }
    }

    // Start process after page load
    window.addEventListener('load', () => {
         setTimeout(findContainerAndObserve, 500); // Wait 0.5 seconds
    });

})();