JavDB Preview Image Viewer

优化 JavDB 预览图显示,将 a 标签替换为 img 标签,支持横向滚动并统一高度

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         JavDB Preview Image Viewer
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  优化 JavDB 预览图显示,将 a 标签替换为 img 标签,支持横向滚动并统一高度
// @author       You
// @match        https://javdb.com/v/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function injectStyles() {
      const style = document.createElement('style');
      style.textContent = `
        /* 设置容器为 Flex 布局,实现横向滚动,并强制子元素不换行 */
        .preview-images {
          display: flex !important;
          overflow-x: auto !important;
          flex-wrap: nowrap !important;
          white-space: nowrap !important;
          align-items: center;
          gap: 10px; /* 添加一点间距让视觉更美观 */
          padding-bottom: 10px; /* 留出滚动条空间 */
        }

        /* 统一设置替换后的 img 元素高度为 400px */
        .preview-images img.tile-item {
          height: 400px;
          width: auto;
          flex-shrink: 0; /* 防止在 Flex 容器中被压缩 */
          object-fit: cover;
          display: block;
        }

        /* 确保容器内的其他直接子元素(如预告片部分)也不会被压缩 */
        .preview-images > * {
          flex-shrink: 0;
        }
      `;
      document.head.appendChild(style);
    }

    function replaceAnchorWithImage() {
      const container = document.querySelector('.preview-images');
      if (!container) return;

      const links = container.querySelectorAll('a.tile-item');
      links.forEach(link => {
        const img = document.createElement('img');

        // 获取 href 属性,去掉 HTML 字符串中可能存在的反引号和首尾空格
        let hrefVal = link.getAttribute('href');
        if (hrefVal) {
            hrefVal = hrefVal.replace(/[`]/g, '').trim();
            img.src = hrefVal;
        }

        // 保留原有的 class,以便样式不丢失
        img.className = 'tile-item';

        // 尝试将原 a 标签内 img 的 alt 属性也转移过来
        const innerImg = link.querySelector('img');
        if (innerImg && innerImg.alt) {
            img.alt = innerImg.alt;
        }

        // 将父节点中的 a 标签替换为新创建的 img 标签
        link.parentNode.replaceChild(img, link);
      });
    }

    // 注入 CSS 样式
    injectStyles();

    replaceAnchorWithImage();
})();