本帖最后由 Thending 于 2023-2-4 16:06 编辑
之前用的一个图片放大预览插件Imagus被edge标记风险了,于是删了自己写个简单版的插件,有兴趣的朋友可以优化监听函数或去请求更高清的地址
在这里简单分享下代码,另外也已经上传gitee和greasyfork
[JavaScript] 纯文本查看 复制代码 // ==UserScript==
// @name 自适应图片放大
// @namespace [url=https://gitee.com/huelse/js-scripts/blob/master/auto-scale-image.js]https://gitee.com/huelse/js-scri ... auto-scale-image.js[/url]
// @version 1.0.0
// @description 自动放大图片标签
// @AuThor THENDINGs
// @require [url=https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js]https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js[/url]
// @match *://*/*
// @Icon [url=https://img1.imgtp.com/2023/02/02/pm8lKWm6.ico]https://img1.imgtp.com/2023/02/02/pm8lKWm6.ico[/url]
// @grant unsafeWindow
// @run-at document-body
// @license GPLv3 License
// ==/UserScript==
(function() {
'use strict';
var _ = window._;
let imgs = [];
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;
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;
}
// console.log('listen', img.getAttribute("src"));
img.addEventListener("mouseenter", function (e) {
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`;
container.appendChild(bigImg);
});
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()) {
container.innerHTML = "";
container.style.border = "";
}
}
})();
例:
直接使用:https://greasyfork.org/zh-CN/scripts/459396
如果觉得好用的话欢迎给个免费的评分谢谢~
以前的作品
极简百度搜索过滤:https://greasyfork.org/zh-CN/scripts/427392
|