Display Image Alt Text on Rank V6

Display the alt text of images on the Rank V6 page of Midjourney

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Display Image Alt Text on Rank V6
// @namespace    http://tampermonkey.net/
// @version      2023-12-16
// @description  Display the alt text of images on the Rank V6 page of Midjourney
// @author       GuiTx - ChatGPT
// @match        https://www.midjourney.com/rank-v6
// @icon         https://www.google.com/s2/favicons?sz=64&domain=midjourney.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to add alt text div to an image
    function addAltTextToImage(image) {
        if (image.parentNode.classList.contains('alt-text-added')) {
            return; // Skip if alt text already added
        }

        // Create a new div element to hold the alt text
        var altTextDiv = document.createElement('div');
        altTextDiv.textContent = image.alt;
        altTextDiv.style.cssText = `
            position: absolute;
            top: 0;
            left: 0;
            color: white;
            background-color: rgba(0, 0, 0, 0.7);
            padding: 5px;
            border-radius: 5px;
            font-size: 12px;
            max-width: 100%;
            word-wrap: break-word;
        `;

        // Position the div over the image
        var containerDiv = document.createElement('div');
        containerDiv.style.cssText = 'position: relative; display: inline-block;';
        image.parentNode.insertBefore(containerDiv, image);
        containerDiv.appendChild(image);
        containerDiv.appendChild(altTextDiv);
        containerDiv.classList.add('alt-text-added');
    }

    // Observer to handle dynamically loaded images
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            mutation.addedNodes.forEach(function(node) {
                if (node.nodeName === 'IMG' && node.alt) {
                    addAltTextToImage(node);
                }
            });
        });
    });

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

    // Initial run for images already on the page
    document.querySelectorAll('img[alt]').forEach(addAltTextToImage);
})();