Greasy Fork

Douban-Info-for-PTP

在电影详情页展示部分中文信息

目前为 2020-09-14 提交的版本。查看 最新版本

// ==UserScript==
// @name         Douban-Info-for-PTP
// @namespace    https://github.com/techmovie/Douban-Info-for-PTP
// @version      0.3
// @description  在电影详情页展示部分中文信息
// @author       Mekio
// @match        http*://passthepopcorn.me/torrents.php?id=*
// @grant        GM_xmlhttpRequest
// ==/UserScript==
(function () {
    'use strict';
    const API_KEY = '054022eaeae0b00e0fc068c0c0a2102a';
    const DOUBAN_API_URL = 'https://frodo.douban.com/api/v2';
    const DOUBAN_SEARCH_API = 'https://movie.douban.com/j/subject_suggest';
    const imdbLink = $('#imdb-title-link').attr('href');
    if (!imdbLink) {
        return
    }
    const imdbId = /tt\d+/.exec(imdbLink)[0];

    GM_xmlhttpRequest({
        method: 'GET',
        url: `${DOUBAN_SEARCH_API}?q=${imdbId}`,
        onload(res) {
            const data = JSON.parse(res.responseText);
            if (data.length > 0) {
                getMovieInfo(data[0].id)
            }
        }
    })

    const getMovieInfo = (movieId) => {
        GM_xmlhttpRequest({
            url: `${DOUBAN_API_URL}/movie/${movieId}?apiKey=${API_KEY}`,
            onload(res) {
                const data = JSON.parse(res.responseText);
                console.log(data);
                addInfoToPage(data);
            }
        })
    }
    const addInfoToPage = (data) => {
        if (isChinese(data.title)) {
            $('.page__title').prepend(`<a  target='_blank' href="${data.url}">[${data.title}] </a>`)
        }
        if (data.intro) {
            $('#synopsis').html(data.intro)
        }
        $('#movieinfo').before(`
        <div class="panel">
		<div class="panel__heading"><span class="panel__heading__title">电影信息</span></div>
        <div class="panel__body"> 
        <div><strong>导演:</strong> ${data.directors.map(item => {
            return ` <a class="artist-info-link" href="${item.url}" target="_blank">${item.name}</a>  `
        })}</div>
        <div><strong>类型:</strong> ${data.genres.join('/')}</div>
        <div><strong>制片国家/地区:</strong> ${data.countries.join('/')}</div>
        <div><strong>语言:</strong> ${data.languages.join('/')}</div>
		<div><strong>时长:</strong> ${data.durations.join('/')}</div>
		<div><strong>又名:</strong>  ${data.aka.join('/')}</div
	    </div>`)
        if (data.rating.value) {
            $('#movie-ratings-table tr').prepend(
                `<td colspan="1" style="width: 152px;">
            <center>
            <a target="_blank" class="rating" href="${data.url}" rel="noreferrer">
            <div style="font-size: 0;min-width: 105px;">
                <span class="icon-pt1" style="font-size: 14px;
                display: inline-block;
                text-align: center;
                border: 1px solid #41be57;
                background-color: #41be57;
                color: white;
                border-top-left-radius: 4px;
                border-bottom-left-radius: 4px;
                width: 24px;
                height: 24px;
                line-height: 24px;">豆</span>
                <span class="icon-pt2" style="font-size: 14px;
                display: inline-block;
                text-align: center;
                border: 1px solid #41be57;
                color: #3ba94d;
                background: #ffffff;
                border-top-right-radius: 4px;
                border-bottom-right-radius: 4px;
                width: 69px;
                height: 24px;
                line-height: 24px;">豆瓣评分</span>
            </div>
            </a>
            </center>
            </td>
            <td style="width: 153px;">
            <span class="rating">${data.rating.value}</span>
            <span class="mid">/</span> 
            <span class="outof"> ${data.rating.max}</span>
            <br>(${data.rating.count} votes)</td>`)
        }
        if (data.actors && data.actors.length) {
            const casts = data.actors.map(item => {
                const imageEl = item.cover_url
                    ? ` <image style="width: 100%;"
                src="${item.cover_url}"></image>`
                    : `<div style="background-image: url('https://www.themoviedb.org/assets/2/v4/glyphicons/basic/glyphicons-basic-4-user-grey-d8fe957375e70239d6abdd549fd7568c89281b2179b5f4470e2e12895792dfa5.svg'); width: auto;
                    display: flex;
                    align-content: center;
                    align-items: center;
                    flex-wrap: wrap;
                    overflow: hidden;
                    height: 100%;
                    background-position: center center;
                    background-repeat: no-repeat;
                    background-color: #dbdbdb;
                    box-sizing: border-box;
                    background-size: 50%;
                    text-overflow: ellipsis;"></div>`
                return `<li style="margin-top: 10px;
                margin-bottom: 10px;
                flex-shrink: 0;
                display: inline-block;
                margin-left: 10px;
                margin-right: 4px;
                width: 138px;
                background-color: #fff;
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
                padding-bottom: 10px;
                border-radius: 6px;
                overflow: hidden;">
                <a target="_blank" href="${item.url}" style="width: 138px;height: 175px;display:block;overflow: hidden;">
                   ${imageEl}
                </a>
                <div  style="color: #000;font-weight: bold;padding: 10px 10px 0;" class="actor-name">
                    <a target="_blank" style="color: #000;font-weight: bold" href="${item.url}">${item.name}</a></div>
            </li>`
            })
            const castPanelEl = `<div class="panel">
                <div class="panel__heading"><span class="panel__heading__title">主演</span></div>
                <div class="panel__body">
                    <ul style="width: 100%;overflow-x: auto;display: flex;padding-left:0;word-break: break-all;">
                        ${casts.join('')}
                    </ul>
                </div>
                </div>`;
            $('#synopsis-and-trailer').next('.panel').after(castPanelEl);
        }
    }
    const isChinese = (title) => {
        return /[\u4e00-\u9fa5]+/.test(title)
    }
})();