Google Drive Image Preview Rotation

Add rotate buttons to image preview

当前为 2024-01-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google Drive Image Preview Rotation
// @namespace    http://tampermonkey.net/
// @version      0.1
// @license      MIT
// @description  Add rotate buttons to image preview
// @author       Sr.Generoso
// @match        https://drive.google.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to create a new child element
    function createChild(parentElement, text, className, clickHandler) {
        const child = document.createElement('button');
        child.textContent = text;
        child.classList.add(className);

        child.addEventListener('click', clickHandler);
        // Apply styles
        child.style.backgroundColor = 'rgba(0,0,0,.75)';
        child.style.border = 'none';
        parentElement.appendChild(child);
    }

    // Function to rotate the image container to the left
    function rotateLeft() {
        const imageContainer = findVisibleImageContainer();
        if (imageContainer) {
            // Adjust the rotation angle as needed
            const currentRotation = (imageContainer.style.transform.match(/(-?\d+)/) || [0])[0];
            const newRotation = (parseInt(currentRotation) - 90) % 360;
            imageContainer.style.transform = `rotate(${newRotation}deg)`;
        }
    }
    // Function to rotate the image container to the right
    function rotateRight() {
        const imageContainer = findVisibleImageContainer();
        if (imageContainer) {
            // Adjust the rotation angle as needed
            const currentRotation = (imageContainer.style.transform.match(/(\d+)/) || [0])[0];
            const newRotation = (parseInt(currentRotation) + 90) % 360;
            imageContainer.style.transform = `rotate(${newRotation}deg)`;
        }
    }

    // Function to find the visible image container
    function findVisibleImageContainer() {
        const imageContainers = document.querySelectorAll('.a-b-Sh-ng:not([style*="display: none;"])');
        for (const container of imageContainers) {
            if (container.offsetHeight > 0 && container.offsetWidth > 0) {
                return container;
            }
        }
        return null;
    }

    // Function to handle changes in the DOM
    function handleMutations(mutationsList, observer) {
        mutationsList.forEach(mutation => {
            if (mutation.type === 'childList') {
                // Check if the target element with class 'a-b-vo' has been added
                const targetDiv = document.querySelector('.a-b-vo');
                if (targetDiv) {
                    // Check if buttons with the custom classes already exist
                    const leftButton = targetDiv.querySelector('.my-rotate-left');
                    const rightButton = targetDiv.querySelector('.my-rotate-right');

                    if (!leftButton) {
                        // Create a "Rotate Left" button
                        createChild(targetDiv, '↪️', 'my-rotate-left', rotateLeft);
                    }

                    if (!rightButton) {
                        // Create a "Rotate Right" button
                        createChild(targetDiv, '↩️', 'my-rotate-right', rotateRight);
                    }

                    // Disconnect the observer after adding buttons
                    observer.disconnect();
                }
            }
        });
    }


    // Create a MutationObserver to watch for changes in the DOM
    const observer = new MutationObserver(mutationsList => handleMutations(mutationsList, observer));

    // Start observing the body for childList changes
    observer.observe(document.body, { childList: true, subtree: true });
})();