Greasy Fork

Greasy Fork is available in English.

Picture Auto-Resize on Chrome

Auto-Resize on Chrome Picture Preview Tab

当前为 2016-07-15 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Picture Auto-Resize on Chrome
// @name:zh-cn    图片在Chrome中自动缩放
// @namespace    http://greasyfork.icu/users/2646
// @version      0.3
// @description  Auto-Resize on Chrome Picture Preview Tab
// @description:zh-cn  让大图在Chrome窗体内自动调整到适合的缩放大小,一览图片全貌(不会影响图片保存)
// @author       2016+, CLE
// @include      *
// @grant        none
// ==/UserScript==

if( ! document.contentType.match(/^image\//i) ) return;

var img = document.getElementsByTagName("img")[0];

function defsize(){
    img.height = img.naturalHeight;
    img.width = img.naturalWidth;
}

function autoresize() {
    if ( img.naturalHeight > window.innerHeight || img.naturalWidth > window.innerWidth ) {
		var hb = 0; var zb = 0; var rat = 0;
		
		if(img.naturalWidth > window.innerWidth) hb = img.naturalWidth / window.innerWidth;
		if(img.naturalHeight > window.innerHeight) zb = img.naturalHeight / window.innerHeight;
		
		if(hb !== 0 && zb !== 0){
			if(hb >= zb) rat = hb; else rat = zb;
		} else if (hb !==0) {
			rat = hb;
		} else if (zb !==0) {
			rat = zb;
		}
		
		if (rat !==0 ){
			img.width = img.naturalWidth / rat;
			img.height = img.naturalHeight / rat;
		}
    }
}

autoresize();
window.onresize = autoresize;


var defm = false;
window.onkeydown = function(event){
    switch(event.keyCode) {
        case 13:
            if(defm){
                window.onresize = autoresize;
                autoresize();
            }else{
                window.onresize = null;
                defsize();
            }
            defm = !defm;
            break;
        case 27:
            window.close();
            break;
    }
};