Platesmania Google Lens Integration

Add Google Lens button to Platesmania and handle image search

// ==UserScript==
// @name         Platesmania Google Lens Integration
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Add Google Lens button to Platesmania and handle image search
// @match        https://platesmania.com/*/add
// @match        https://www.google.com/?olud&src=pm
// @grant        GM.setValue
// @grant        GM.getValue
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function handlePlatesmania() {
        const button = document.createElement('button');
        button.textContent = 'Google Lens';
        button.style.cssText = 'margin-bottom: 10px; width: 100%; background-color: rgb(52, 152, 219); color: rgb(255, 255, 255); border: medium; cursor: pointer;';
        
        const targetContainer = document.querySelector('#zoomimgid');
        if (targetContainer) {
            const existingContainer = targetContainer.previousElementSibling;
            
            if (existingContainer && existingContainer.style.width === '260px') {
                existingContainer.appendChild(button);
            } else {
                const container = document.createElement('div');
                container.style.cssText = 'margin-left: 0px; width: 260px; display: inline-block;';
                container.appendChild(button);
                targetContainer.parentNode.insertBefore(container, targetContainer);
            }
        }

        function checkForImage() {
            const imgElement = document.querySelector('#zoomimg');
            if (imgElement && imgElement.src) {
                GM.setValue('platesmaniaImage', imgElement.src);
            }
        }

        button.addEventListener('click', () => {
            window.open('https://www.google.com/?olud&src=pm', '_blank');
        });

        setInterval(checkForImage, 100);
    }

    function handleGoogleImages() {
        let searchAttempted = false;

        async function attemptSearch() {
            if (searchAttempted) return;

            try {
                const imageData = await GM.getValue('platesmaniaImage', '');
                if (imageData) {
                    const inputField = document.querySelector('input[placeholder="Bildlink einfügen"]');
                    const searchButton = document.querySelector('.Qwbd3');

                    if (inputField && searchButton) {
                        inputField.value = imageData;
                        inputField.dispatchEvent(new Event('input', { bubbles: true }));
                        searchButton.click();
                        searchAttempted = true;
                    }
                }
            } catch (error) {
                console.error('Error handling Google Images:', error);
            }
        }

        const searchInterval = setInterval(() => {
            if (searchAttempted) {
                clearInterval(searchInterval);
            } else {
                attemptSearch();
            }
        }, 100);
    }

    if (window.location.href.includes('platesmania.com')) {
        handlePlatesmania();
    } else if (window.location.href === 'https://www.google.com/?olud&src=pm') {
        handleGoogleImages();
    }
})();