// ==UserScript== // @name 豆瓣电影短评助手 // @namespace https://greasyfork.org/en/users/214530 // @version 1 // @description 在豆瓣电影短评页面增加一些实用功能,例如:短评字数统计、自动翻页、快速标记、快速复制等等。 // @author NaN // @match https://movie.douban.com/subject//comments // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // @grant GM_notification // @grant GM_xmlhttpRequest // @grant GM_registerMenuCommand // ==/UserScript==

(function() { 'use strict';

// 统计短评字数
function countWords() {
    const commentList = document.querySelectorAll('.comment-item');
    if (commentList) {
        commentList.forEach(function(comment) {
            const contentEl = comment.querySelector('.short');
            if (contentEl) {
                const content = contentEl.innerText.trim();
                const count = content.length;
                const countEl = comment.querySelector('.comment-count');
                if (countEl) {
                    countEl.innerText = count + '字';
                } else {
                    const newCountEl = document.createElement('span');
                    newCountEl.className = 'comment-count';
                    newCountEl.innerText = count + '字';
                    contentEl.parentNode.insertBefore(newCountEl, contentEl.nextSibling);
                }
            }
        });
    }
}

// 自动翻页
function autoLoadMore() {
    const loadMoreBtn = document.querySelector('.more');
    if (loadMoreBtn) {
        loadMoreBtn.click();
    }
}

// 快速标记
function quickMark() {
    const commentList = document.querySelectorAll('.comment-item');
    if (commentList) {
        commentList.forEach(function(comment) {
            const markEl = comment.querySelector('.comment-mark');
            if (markEl) {
                return;
            } else {
                const newMarkEl = document.createElement('span');
                newMarkEl.className = 'comment-mark';
                newMarkEl.innerText = '标记';
                comment.querySelector('.actions').appendChild(newMarkEl);
                newMarkEl.addEventListener('click', function() {
                    markComment(comment);
                });
            }
        });
    }
}

// 标记短评
function markComment(comment) {
    const id = comment.getAttribute('data-cid');
    const mark = prompt('请输入标记内容:');
    if (mark) {
        GM_setValue(id, mark);
        const markEl = comment.querySelector('.comment-mark');
        markEl.innerText = mark;
    }
}

// 快速复制
function quickCopy() {
    const commentList = document.querySelectorAll('.comment-item');
    if (commentList) {
        commentList.forEach(function(comment) {
            const copyEl = comment.querySelector('.comment-copy');
            if (copyEl) {
                return;
            } else {
                const newCopyEl = document.createElement('span');
                newCopyEl.className = 'comment-copy';
                newCopyEl.innerText = '复制';
                comment.querySelector('.actions').appendChild(newCopyEl);
                newCopyEl.addEventListener('click', function() {
                    copyComment(comment);
                });
            }
        });
    }
}

// 复制短评
function copyComment(comment) {
    const contentEl = comment.querySelector('.short');
    if (contentEl) {
        const content = contentEl.innerText.trim();
        const input = document.createElement('input');
        input.setAttribute('readonly', 'readonly');
        input.setAttribute('value', content);
        document.body.appendChild(input);
        input.select();
        document.execCommand('copy');
        document.body.removeChild(input);
        GM_notification('已复制短评:' + content, '复制成功');
    }
}

// 添加样式
function addStyles() {
    GM_addStyle(`
    .comment-count {
        color: #666;
        font-size: 12px;
        margin-left: 10px;
    }
    .comment-mark {
        color: #666;
        font-size: 12px;
        margin-left: 10px;
        cursor: pointer;
    }
    .comment-copy {
        color: #666;
        font-size: 12px;
        margin-left: 10px;
        cursor: pointer;
    }
    `);
}

// 初始化
function init() {
    countWords();
    addStyles();
    quickMark();
    quickCopy();
    setInterval(autoLoadMore, 1000);
}

// 执行初始化
init();

})();


原文地址: https://www.cveoy.top/t/topic/nsOf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录