JavDB Trailer

在 JavDB 页面上显示预告片信息

اعتبارا من 19-01-2025. شاهد أحدث إصدار.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         JavDB Trailer
// @namespace    http://tampermonkey.net/
// @version      2025-01-03
// @description  在 JavDB 页面上显示预告片信息
// @author       haoyinhaoyin
// @match        https://javdb.com/v/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=javdb.com
// @grant        GM_xmlhttpRequest
// @connect      javtrailers.com
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // 在 JavDB 页面上获取包含视频 ID 的元素
    const spanElement = document.querySelector('.panel-block.first-block .value a');
    if (spanElement != null) {
        // 获取视频 ID
        const rawJavId = spanElement.parentElement.textContent.trim();
        const formattedJavId = standardizeJavId(rawJavId);
        fetchPreview(formattedJavId);
    } else {
        console.log("JavDB 页面中未找到视频 ID。");
    }

    // 标准化视频 ID 格式(移除破折号并补零)
    function standardizeJavId(javId) {
        const parts = javId.split('-');
        if (parts.length === 2 && /^[A-Za-z]+$/.test(parts[0]) && /^[0-9]+$/.test(parts[1])) {
            const prefix = parts[0].toUpperCase();
            const number = parts[1].padStart(5, '0');
            return `${prefix}${number}`;
        }
        return javId.replace(/-/g, '').toUpperCase();
    }

    // 通过 API 获取预告片信息并插入页面
    function fetchPreview(javId) {
        const url = `https://search.javtrailers.com/indexes/videos/search?q=${javId}&page=1&sort=releaseDate:desc&hitsPerPage=1`;

        doGet(url).then(data => {
            try {
                const result = JSON.parse(data);
                if (result.hits && result.hits.length > 0) {
                    const hit = result.hits[0];
                    const contentId = hit['contentId'];
                    const matchedId = hit['id'] || hit['title'];

                    // 宽松匹配:检查返回的 ID 是否包含标准化后的 javId
                    if (matchedId && matchedId.toLowerCase().includes(javId.toLowerCase())) {
                        insertTrailerLink(javId, contentId);
                    } else {
                        insertNoTrailerMessage(javId);
                    }
                } else {
                    insertNoTrailerMessage(javId);
                }
            } catch (error) {
                console.error("解析 API 响应失败:", error);
                insertNoTrailerMessage(javId);
            }
        });
    }

    // 在页面中插入预告片链接
    function insertTrailerLink(javId, contentId) {
        const trailerUrl = `https://javtrailers.com/video/${contentId}`;
        const existingDiv = document.querySelector('.panel-block.first-block');

        if (existingDiv) {
            const trailerHTML = `
                <div class="panel-block">
                    <strong>预览:</strong>
                    &nbsp;<span class="value"><a href="${trailerUrl}" target="_blank">${javId}</a></span>
                </div>
            `;
            existingDiv.insertAdjacentHTML('afterend', trailerHTML);
        } else {
            console.warn("页面中未找到插入位置。");
        }
    }

    // 如果没有预告片,插入提示信息
    function insertNoTrailerMessage(javId) {
        const existingDiv = document.querySelector('.panel-block.first-block');

        if (existingDiv) {
            const noTrailerHTML = `
                <div class="panel-block">
                    <strong>预览:</strong>
                    &nbsp;<span class="value">未找到与 <strong>${javId}</strong> 对应的预告片。</span>
                </div>
            `;
            existingDiv.insertAdjacentHTML('afterend', noTrailerHTML);
        } else {
            console.warn("页面中未找到插入位置。");
        }
    }

    // 封装 GET 请求
    async function doGet(url) {
        return new Promise(resolve => {
            GM.xmlHttpRequest({
                method: "GET",
                headers: { "authorization": "Bearer 6b4bd3e560e994a5b009023e1d21f51e95dbb86ca3f47cb03f34f8a2cb9a93f2" },
                url,
                onload: response => resolve(response.responseText),
            });
        });
    }
})();