Image On Hover

Umożliwia podgląd obrazka po najechaniu.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Image On Hover
// @namespace    https://wilchan.org
// @version      0.2
// @description  Umożliwia podgląd obrazka po najechaniu.
// @author       Anonimas
// @match        https://wilchan.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wilchan.org
// @grant        none
// @license      MIT
// ==/UserScript==

if (viewConfiguration.boardViewType === BoardViewType.ClassicBoard || viewConfiguration.boardViewType === BoardViewType.ClassicThread) {
    let iCss = document.createElement("style");
    iCss.innerHTML = `#hover-container > img{border-radius:4px;filter: drop-shadow(0 0 4px rgba(0,0,0,.5));display:none;pointer-events:none;position:fixed;z-index:300;}#hover-containter{z-index:300;}`;
    document.head.appendChild(iCss);

    document.querySelectorAll(".file[data-mime] > a").forEach(el => {
        el.addEventListener("mouseenter", imageHover);
    })

    let div = document.createElement("div");
    div.setAttribute("id", "hover-container");
    document.body.appendChild(div);

    let navH = document.querySelector("body > nav").offsetHeight;

    function imageHover(event) {
        let target = event.currentTarget,
            src = target.getAttribute("href"),
            mime = target.parentNode.getAttribute("data-mime"),
            image = ["image/jpeg", "image/png", "image/gif", "image/svg+xml"];

        div.innerHTML = "";
        if (image.indexOf(mime) > -1) {
            let img = document.createElement("img");
            img.setAttribute("src", src);
            div.appendChild(img);
            target.addEventListener("mouseleave", () => {
                img.remove();
            });

            img.addEventListener("load", () => {
                let width, height, left, top, maxWidth, maxHeight, scale, rect, pos;
                rect = target.getBoundingClientRect();
                left = rect.left + rect.width + 10;
                maxWidth = window.document.body.scrollWidth - left - 10;
                maxHeight = window.innerHeight - (navH + 10 + 25);
                width = img.width;
                height = img.height;
                scale = Math.min(1, maxWidth / width, maxHeight / height);

                pos = (rect.top - (navH + 10))/(maxHeight - (rect.height));
                if (pos > 1) pos = 1;
                else if (pos < 0) pos = 0;
                top = navH + 10 + pos * (maxHeight - height * scale);

                img.style.left = left + "px";
                img.style.top = top + "px";
                img.style.maxHeight = maxHeight + "px";
                img.style.maxWidth = maxWidth + "px";
                img.style.display = "unset";
            });
        }
    }

    window.addEventListener("after-create-thread-article-element-event", function (event) {
        let section = event.detail.element;
        let file = section.querySelector(".file[data-mime] > a");
        if(file)
            file.addEventListener("mouseenter", imageHover);
    }, false);

    window.addEventListener("after-create-post-section-element-event", function (event) {
        let section = event.detail.element;
        let file = section.querySelector(".file[data-mime] > a");
        if(file)
            file.addEventListener("mouseenter", imageHover);
    }, false);
}