ImageViewer

浏览器图片查看效果增强 for Chrome/Edge/Firefox

目前為 2021-07-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name         ImageViewer
// @version      1.0.8
// @description  浏览器图片查看效果增强 for Chrome/Edge/Firefox
// @author       Jack.Chan ([email protected])
// @namespace    http://fulicat.com
// @url          https://greasyfork.org/zh-CN/scripts/429054-imageviewer
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    if (document.contentType.startsWith('image/')) {
        var doc = document.body || document.documentElement;
        var docTagName = (function(tag){
                var tagName = tag && tag.tagName.toLowerCase();
                if (tagName) {
                    if (tagName == 'svg') {
                        return tagName;
                    }
                    if (tagName == 'body' && tag.children && tag.children.length) {
                        tagName = tag.children[0].tagName.toLowerCase();
                        if (tagName == 'img') {
                            return tagName;
                        }
                    }
                }
        })(doc);
        var isViewerMode = docTagName == 'svg' || docTagName == 'img';
        if (isViewerMode) {
            if (document.head) {
                var style = document.createElement('style');
                style.setAttribute('type', 'text/css');
                style.innerHTML = `img{position: static !important;background: none !important;background-color: transparent !important;}`;
                document.head.appendChild(style);
            }
            doc.style.backgroundImage = 'url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2NCIgaGVpZ2h0PSI2NCIgdmlld0JveD0iMCAwIDY0IDY0Ij4KICA8ZGVmcz4KICAgIDxzdHlsZT4KICAgICAgLmNscy0xIHsKICAgICAgICBmaWxsOiAjMzAzMDMwOwogICAgICB9CgogICAgICAuY2xzLTIgewogICAgICAgIGZpbGw6ICMyMDIwMjA7CiAgICAgIH0KICAgIDwvc3R5bGU+CiAgPC9kZWZzPgogIDxyZWN0IGlkPSJsdCIgY2xhc3M9ImNscy0xIiB3aWR0aD0iMzIiIGhlaWdodD0iMzIiLz4KICA8cmVjdCBpZD0icmIiIGNsYXNzPSJjbHMtMSIgeD0iMzIiIHk9IjMyIiB3aWR0aD0iMzIiIGhlaWdodD0iMzIiLz4KICA8cmVjdCBpZD0ibGIiIGNsYXNzPSJjbHMtMiIgeT0iMzIiIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIvPgogIDxyZWN0IGlkPSJydCIgY2xhc3M9ImNscy0yIiB4PSIzMiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjMyIi8+Cjwvc3ZnPgo=)';
            doc.style.backgroundAttachment = 'fixed';
            doc.style.boxSizing = 'border-box';
            doc.style.position = 'absolute';
            doc.style.top = '50%';
            doc.style.left = '50%';
            doc.style.transform = 'translate(-50%, -50%)';
            if (docTagName == 'svg') {
                doc.style.margin = '10px';
                doc.style.width = 'auto';
                doc.style.height = 'auto';
                doc.style.maxWidth = '100%';
                doc.style.maxHeight = '100%';
            }
        }
    }
})();