您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Greasy Fork is available in English.
自动放大图片标签
当前为
// ==UserScript== // @name 自适应图片放大 // @namespace https://gitee.com/huelse/js-scripts/blob/master/auto-scale-image.js // @version 1.0.1 // @description 自动放大图片标签 // @author THENDINGs // @require https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js // @match *://*/* // @icon https://img1.imgtp.com/2023/02/02/pm8lKWm6.ico // @grant unsafeWindow // @run-at document-body // @license GPLv3 License // ==/UserScript== (function() { 'use strict'; var _ = window._; // 小于这个尺寸的图片就不自动放大了 const min_px = 64; // 鼠标停留几毫秒后再显示,默认0.5秒 const waiting_time = 500; let imgs = []; let current_img = ""; const observer = new MutationObserver( _.throttle( function () { imgs = document.querySelectorAll("img"); // console.log('observer', imgs.length); imgListener(); }, 300, { leading: false, trailing: true } ) ); observer.observe(document.body, { attributes: false, childList: true, subtree: true, }); function getContainer() { const existContainer = document.querySelector(".big-img-container"); if (!existContainer) { const container = document.createElement("div"); container.className = "big-img-container"; container.style.position = "fixed"; container.style.top = 0; container.style.left = 0; container.style.zIndex = 999999; container.onclick = function () { clearContainer(container); }; document.querySelector("html").appendChild(container); return container; } return existContainer; } function imgListener() { const container = getContainer(); const halfWidth = window.innerWidth / 2; const halfHeight = window.innerHeight / 2; imgs.forEach((img) => { if (img.getAttribute("listener") === "true") { return; } if (img.width <= min_px || img.height <= min_px) { return; } // console.log('listen', img.getAttribute("src")); img.addEventListener("mouseenter", function (e) { current_img = e.target.src; let hover_img = e.target.src; setTimeout(() => { if (current_img === hover_img) { clearContainer(container); const { x, y } = e; const width = x > halfWidth ? x : window.innerWidth - x; const height = y > halfHeight ? y : window.innerHeight - y; const imgWidth = e.target.width; const imgHeight = e.target.height; const rate = imgWidth / imgHeight; const bigImgHeight = (width / rate < height ? width / rate : height) - 10; const bigImgWidth = (height * rate < width ? height * rate : width) - 10; const containerX = x > halfWidth ? x - bigImgWidth - 5 : x + 5; const containerY = y > halfHeight ? y - bigImgHeight - 5 : y + 5; container.style.left = `${containerX}px`; container.style.top = `${containerY}px`; container.style.border = "1px solid #000"; const bigImg = e.target.cloneNode(); bigImg.style.maxHeight = "unset"; bigImg.style.maxWidth = "unset"; bigImg.style.height = `${bigImgHeight}px`; bigImg.style.width = `${bigImgWidth}px`; bigImg.onclick = function () { clearContainer(container); }; container.appendChild(bigImg); } }, waiting_time); }); img.addEventListener("mouseleave", function (e) { // const bigImg = container.firstElementChild; // console.log(bigImg.width, bigImg.height); clearContainer(container); }); img.setAttribute("listener", "true"); }); } function clearContainer(container) { if (container.hasChildNodes()) { current_img = ""; container.innerHTML = ""; container.style.border = ""; } } })();