Google Drive Image Preview Rotation

Add buttons that allow rotation of preview in Google Drive

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Google Drive Image Preview Rotation
// @namespace    http://tampermonkey.net/
// @version      0.2
// @license      MIT
// @description  Add buttons that allow rotation of preview in Google Drive
// @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 });
})();