JavDB Trailer

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

Versione datata 19/01/2025. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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),
            });
        });
    }
})();