ImageViewer

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

(function() {
    'use strict';
    var bgImage = `data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2NCIgaGVpZ2h0PSI2NCIgdmlld0JveD0iMCAwIDY0IDY0Ij4KICA8ZGVmcz4KICAgIDxzdHlsZT4KICAgICAgLmNscy0xIHsKICAgICAgICBmaWxsOiAjMzAzMDMwOwogICAgICB9CgogICAgICAuY2xzLTIgewogICAgICAgIGZpbGw6ICMyMDIwMjA7CiAgICAgIH0KICAgIDwvc3R5bGU+CiAgPC9kZWZzPgogIDxyZWN0IGlkPSJsdCIgY2xhc3M9ImNscy0xIiB3aWR0aD0iMzIiIGhlaWdodD0iMzIiLz4KICA8cmVjdCBpZD0icmIiIGNsYXNzPSJjbHMtMSIgeD0iMzIiIHk9IjMyIiB3aWR0aD0iMzIiIGhlaWdodD0iMzIiLz4KICA8cmVjdCBpZD0ibGIiIGNsYXNzPSJjbHMtMiIgeT0iMzIiIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIvPgogIDxyZWN0IGlkPSJydCIgY2xhc3M9ImNscy0yIiB4PSIzMiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjMyIi8+Cjwvc3ZnPgo=`;
    var ua = navigator.userAgent.toLowerCase();
    var doc = document.body || document.documentElement;
    var isQQBrowser = ua.indexOf('qqbrowser/') > -1;
    var isQQBrowserPS = function() {
        return document && document.body && document.body.classList.contains('qb-picture-ps')
    }
    function loaded() {
        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;
                    } else {
                        tagName = document.querySelector('img');
                        return tagName && tagName.tagName && tagName.tagName.toLowerCase();
                    }
                }
            }
        })(doc);

        var isViewerMode = docTagName == 'svg' || docTagName == 'img';
        if (isViewerMode) {
            if (document.head) {
                var styleText = [];
                if (isQQBrowserPS()) {
                    styleText.push(`body{box-sizing: border-box;background-attachment: fixed !important;background-repeat: repeat !important;}`);
                    styleText.push(`body{background: url(${bgImage}) !important;}`);
                } else {
                    styleText.push(`img{position: static !important;background: none !important;background-color: transparent !important;}`);
                }
                var style = document.createElement('style');
                style.setAttribute('type', 'text/css');
                style.innerHTML = styleText.join('');
                document.head.appendChild(style);
            }
            if (isQQBrowserPS()) {
                doc.style.position = 'static';
                doc.style.top = 'auto';
                doc.style.left = 'auto';
                doc.style.transform = 'none';
            } else {
                doc.style.backgroundImage = `url(${bgImage})`;
                doc.style.backgroundAttachment = 'fixed';
                doc.style.boxSizing = 'border-box';
                if (docTagName == 'svg') {
                    doc.style.position = 'absolute';
                    doc.style.top = '50%';
                    doc.style.left = '50%';
                    doc.style.transform = 'translate(-50%, -50%)';
                    doc.style.margin = '10px';
                    doc.style.width = 'auto';
                    doc.style.height = 'auto';
                    doc.style.maxWidth = '100%';
                    doc.style.maxHeight = '100%';
                }
            }
        }
    }
    if (document.contentType.startsWith('image/')) {
        if (isQQBrowser && doc.tagName !== 'svg') {
            setTimeout(function() {
                loaded();
            }, 666);
        } else {
            loaded();
        }
    }
})();