豆瓣电影看过信息展示

在豆瓣电影页面中,将“看过”信息显示到评分区域顶部,样式和位置按照需求调整。

// ==UserScript==
// @name        豆瓣电影看过信息展示
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  在豆瓣电影页面中,将“看过”信息显示到评分区域顶部,样式和位置按照需求调整。
// @author       YourName
// @match        https://movie.douban.com/subject/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // 使用 MutationObserver 动态监听页面内容
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            // 查找需要的元素
            const starsElement = document.querySelector('.j.a_stars');
            const ratingWrap = document.querySelector('.rating_wrap[rel="v:rating"]');

            // 如果找到“我看过”信息并尚未插入
            if (starsElement && ratingWrap && !document.querySelector('.watched-info')) {
                const watchedDateElement = starsElement.querySelector('.collection_date');
                const watchedDate = watchedDateElement ? watchedDateElement.textContent.trim() : null;

                if (watchedDate) {
                    // 创建并插入“看过”信息
                    const watchedInfo = document.createElement('div');
                    watchedInfo.className = 'watched-info';
                    watchedInfo.textContent = `看过: ${watchedDate}`;
                    watchedInfo.style.cssText = `
                        color: rgb(102, 102, 102);
                        font-size: 19px;
                        margin-top: 15px;
                    `;

                    // 插入到评分区域的顶部
                    ratingWrap.prepend(watchedInfo);
                }
            }
        });
    });

    // 监听整个页面的变化
    const config = { childList: true, subtree: true };
    observer.observe(document.body, config);

    // 防止资源浪费,当不需要时可以停止观察
    window.addEventListener('unload', () => observer.disconnect());
})();