JavDB Trailers

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

目前為 2025-01-19 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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),
            });
        });
    }
})();