Greasy Fork

Greasy Fork is available in English.

在线电影添加豆瓣评分

在主流电影网站上显示豆瓣评分。

当前为 2024-05-14 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         DoubanRatingForMovie
// @name:zh-CN   在线电影添加豆瓣评分
// @namespace    https://ciphersaw.me/
// @version      1.0.0
// @description  Display Douban rating for online movies.
// @description:zh-CN  在主流电影网站上显示豆瓣评分。
// @author       CipherSaw
// @match        *://*.olevod.com/index.php*
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @connect      douban.com
// @license      GPL-3.0
// @grant        GM_xmlhttpRequest
// ==/UserScript==

'use strict';

const LOG_LEVELS = {
    NONE: 0,
    ERROR: 1,
    INFO: 2,
    DEBUG: 3
};

class Logger {
    constructor(initialLevel = 'INFO') {
        this.currentLogLevel = LOG_LEVELS[initialLevel] || LOG_LEVELS.INFO;
    }
    debug(...args) {
        if (this.currentLogLevel >= LOG_LEVELS.DEBUG) {
            console.debug(...args);
        }
    }
    info(...args) {
        if (this.currentLogLevel >= LOG_LEVELS.INFO) {
            console.info(...args);
        }
    }
    error(...args) {
        if (this.currentLogLevel >= LOG_LEVELS.ERROR) {
            console.error(...args);
        }
    }
}

const logger = new Logger('INFO');
const DOUBAN_RATING_API = 'https://www.douban.com/search?cat=1002&q=';

(function () {
    const host = location.hostname;
    if (host === 'www.olevod.com') {
        OLEVOD_setRating()
    }
})();

function OLEVOD_setRating() {
    const title = OLEVOD_getTitle();
    getDoubanRating(title)
        .then(data => {
            logger.info(`getDoubanRating: title=${title} result=${data}`);
            OLEVOD_setMainRating(data);
        })
        .catch(err => {
            logger.error(`getDoubanRating: title=${title} error=${err}`);
            OLEVOD_setMainRating("N/A");
        });
}

function OLEVOD_getTitle() {
    let title = $('h2.title').clone();
    title.children().remove();
    return title.text().trim();
}

function OLEVOD_setMainRating(rating) {
    const ratingObj = $('.content_detail .data>.text_muted:first-child');
    const text = ratingObj.text().trim();
    ratingObj.text(text + rating);
}

async function getDoubanRating(title) {
    const url = DOUBAN_RATING_API + title;
    logger.info(`requestDoubanRating: title=${title} url=${url}`);

    const ratingNums = await new Promise((resolve, reject) => {
        GM_xmlhttpRequest({
            "method": "GET",
            "url": url,
            "onload": (r) => {
                const response = $($.parseHTML(r.response));
                if (r.status !== 200) {
                    reject(new Error(`StatusError: response status is ${r.status} and message is ${r.statusText}`));
                } else {
                    try {
                        let msg = resolveDoubanRatingResult(response);
                        resolve(msg);
                    } catch (error) {
                        reject(error);
                    }
                }
            }
        });
    });
    return ratingNums;
};

function resolveDoubanRatingResult(data) {
    const s = data.find('.result-list .result:first-child');
    if (s.length === 0) {
        throw Error("ResolveError: search result not found");
    }
    const ratingNums = s.find('.rating_nums').text() || '暂无评分';
    return ratingNums;
}