🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2025/12/27 12:48:47
🔄 下次更新时间:2025/12/27 13:48:47
手动刷新缓存

Toggle Zoom

Extend image zoom modes with scale-to-width and scale-to-height of window

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey, Greasemonkey или Violentmonkey.

Для установки этого скрипта вам необходимо установить расширение, такое как Tampermonkey.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey или Violentmonkey.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey или Userscripts.

Чтобы установить этот скрипт, сначала вы должны установить расширение браузера, например Tampermonkey.

Чтобы установить этот скрипт, вы должны установить расширение — менеджер скриптов.

(у меня уже есть менеджер скриптов, дайте мне установить скрипт!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

(у меня уже есть менеджер стилей, дайте мне установить скрипт!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name        Toggle Zoom
// @namespace   Nickel
// @description Extend image zoom modes with scale-to-width and scale-to-height of window
// @version     0.5
// @license     GNU General Public License v3
// @copyright   2022, Nickel
// @author      Nickel
// @grant       none
// @run-at      document-start
// @noframes
// @include     *
// ==/UserScript==


(function(){

function unscaled() {
	img.removeAttribute( "width" );
	img.removeAttribute( "height" );
	mode = "unscaled";
	document.title = titleBase;
}
function scaleToWidth() {
	img.removeAttribute( "height" );
	img.width = document.documentElement.clientWidth;
	// do it again due to scrollbars that may have (dis)appeared
	img.width = document.documentElement.clientWidth;
	mode = "scaleToWidth";
	document.title = titleBase + " — " + mode + " (" + (100*img.width/img.naturalWidth).toFixed(0) + "%)";
}
function scaleToHeight() {
	img.removeAttribute( "width" );
	img.height = document.documentElement.clientHeight;
	// do it again due to scrollbars that may have (dis)appeared
	img.height = document.documentElement.clientHeight;
	mode = "scaleToHeight";
	document.title = titleBase + " — " + mode + " (" + (100*img.width/img.naturalWidth).toFixed(0) + "%)";
}

function toggle() {
	if( (img.naturalWidth / img.naturalHeight) > (document.documentElement.clientWidth / document.documentElement.clientHeight) ) {
		if( mode == "unscaled" ) {
			scaleToHeight();
			// next is scaleToWidth
			img.style.cursor = "zoom-out";
		}
		else if( mode == "scaleToHeight" || mode == "doScaleToFit" ) {
			scaleToWidth();
			// next is unscaled
			if( img.naturalWidth > document.documentElement.clientWidth ) img.style.cursor = "zoom-in";
			else img.style.cursor = "zoom-out";
		}
		else {
			unscaled();
			// next is scaleToHeight
			img.style.cursor = "zoom-in";
		}
	}
	else {
		if( mode == "unscaled" ) {
			scaleToWidth();
			// next is scaleToHeight
			img.style.cursor = "zoom-out";
		}
		else if( mode == "scaleToWidth" || mode == "doScaleToFit" ) {
			scaleToHeight();

			// next is unscaled
			if( img.naturalHeight > document.documentElement.clientHeight ) img.style.cursor = "zoom-in";
			else img.style.cursor = "zoom-out";
		}
		else {
			unscaled();
			// next is scaleToWidth
			img.style.cursor = "zoom-in";
		}
	}
}

function initialize() {
	if( initialized == true ) return;
	initialized = true;

	// Firefox does some weird shit
	img.style.marginTop = "0";

	titleBase = document.title.replace( / — Scaled.*$/, "" );

	// set initial mode by setting do* and toggling
	if( (img.naturalWidth > document.documentElement.clientWidth) || (img.naturalHeight > document.documentElement.clientHeight) ) mode = "doScaleToFit";
    else mode = "doUnscaled";
    toggle();

	// add new left mouse button event listener
	// NOTE: would prefer to put listener on `img` instead of `document` to not react to clicks on blank space
	document.addEventListener( "click", function(e) {
		if( e.button === 0 ) {
			e.stopPropagation();
			toggle();
		}
	}, true );
}

var initialized = false;
var img = document.images[0];
if( typeof img === 'undefined' ) return;
if( img.src != window.location.href ) return;

// get image metadata as early as possible
wait = setInterval( function() {
	if( img.naturalWidth !== 0 && img.naturalHeight !== 0 && document.title !== "" ) {
		clearInterval(wait);
		initialize();
	}
}, 10 );
// also try initializing when image finishes loading
img.onload = initialize;

})();