Picture Auto-Resize on Chrome

Auto-Resize on Chrome Picture Preview Tab

Από την 20/01/2016. Δείτε την τελευταία έκδοση.

// ==UserScript==
// @name         Picture Auto-Resize on Chrome
// @name:zh-cn    图片在Chrome中自动缩放
// @namespace    https://greasyfork.org/users/2646
// @version      0.1
// @description  Auto-Resize on Chrome Picture Preview Tab
// @description:zh-cn  让大图在Chrome窗体内自动调整到适合的缩放大小,一览图片全貌(不会影响图片保存)
// @author       2016+, CLE
// @include      /\.(jpe?g|png|gif|bmp|webp)$/
// @grant        none
// ==/UserScript==

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 rat;
        if ( img.naturalHeight >= img.naturalWidth) {
            rat = img.naturalHeight / window.innerHeight;
        } else {
            rat = img.naturalWidth / window.innerWidth;
        }
        img.height = img.naturalHeight / rat;
        img.width = img.naturalWidth / 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;
    }
}