JavDB Trailers

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

当前为 2025-01-19 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         JavDB Trailers
// @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 javId = spanElement.parentElement.textContent.trim();
        fetchPreview(javId);
    } else {
        console.log("JavDB 页面中未找到视频 ID。");
    }

    // 通过 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'];

                    // 确认匹配结果是否与请求的 javId 一致
                    if (matchedId && matchedId.toLowerCase().includes(javId.toLowerCase().replace('-', ''))) {
                        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),
            });
        });
    }
})();