吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2397|回复: 26
收起左侧

[其他原创] 【油猴脚本】自适应图片放大预览

  [复制链接]
Thending 发表于 2023-2-3 23:03
本帖最后由 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 = "";
    }
  }

})();


例:
Snipaste_2023-02-03_23-02-53.jpg

直接使用:https://greasyfork.org/zh-CN/scripts/459396
如果觉得好用的话欢迎给个免费的评分谢谢~



以前的作品
极简百度搜索过滤:https://greasyfork.org/zh-CN/scripts/427392


免费评分

参与人数 4吾爱币 +4 热心值 +4 收起 理由
yanglinman + 1 谢谢@Thanks!
chmwyy + 2 + 1 谢谢@Thanks!
bpzm1987 + 1 + 1 热心回复!
wyxa + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| Thending 发表于 2023-3-14 22:41
现已经全面更新,能在更合适的时间内显示hover图片,并尝试加载原图

https://greasyfork.org/zh-CN/scripts/459396
debug_cat 发表于 2023-2-4 09:43
Kanye 发表于 2023-2-4 09:49
cnkiller 发表于 2023-2-4 09:56
谢谢分享
Bughack 发表于 2023-2-4 10:13
感谢您的分享!
songbing490 发表于 2023-2-4 11:06
先收藏了   谢谢分享
think0663 发表于 2023-2-4 11:47
比较小的图标都会被放大,能否过滤一下被放大的图片尺寸呢?
bpzm1987 发表于 2023-2-4 11:52
厉害,谢谢分享!
andyle 发表于 2023-2-4 11:53
小的有放大效果,看头像就是闪下就没了
id3721 发表于 2023-2-4 11:54
挺实用的
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-4 22:06

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表