NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name 豆瓣小组助手
// @name:en Douban Group Helper
// @namespace https://greasyfork.org/scripts/douban-helper
// @version 1.0.0
// @description 帮助管理豆瓣小组帖子和评论,支持批量删除自己的评论
// @description:en Help manage Douban group posts and comments, support batch deletion of your own comments
// @author 豆瓣助手
// @match https://www.douban.com/group/topic/*
// @icon https://www.douban.com/favicon.ico
// @grant none
// @license MIT
// @supportURL https://github.com/[您的GitHub用户名]/douban-helper/issues
// @homepageURL https://github.com/[您的GitHub用户名]/douban-helper
// ==/UserScript==
/* jshint esversion: 8 */
/* globals window, document, fetch */
/*
* 豆瓣小组助手
*
* 功能:
* 1. 批量删除自己发布的评论
* 2. 可自定义删除间隔时间
* 3. 安全可靠,仅在豆瓣小组页面运行
*
* 使用说明:
* 1. 安装脚本后访问豆瓣小组帖子页面
* 2. 在页面右上角设置删除间隔时间(建议5秒以上)
* 3. 点击"开始删除评论"按钮
* 4. 等待删除操作完成
*
* 注意事项:
* 1. 请合理使用,避免频繁操作
* 2. 建议设置适当的删除间隔时间
* 3. 本脚本仅支持删除自己发布的评论
* 4. 使用本脚本时请遵守豆瓣网站的使用条款
*/
(function() {
'use strict';
// 创建UI
function createUI() {
const container = document.createElement('div');
container.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 9999;
font-size: 14px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
`;
container.innerHTML = `
<div style="margin-bottom: 10px;">
<label style="display: block; margin-bottom: 5px;">删除间隔(秒):</label>
<input type="number" id="deleteInterval" value="5" min="1" style="
width: 60px;
padding: 4px;
border: 1px solid #ddd;
border-radius: 4px;
">
</div>
<button id="startDelete" style="
background: #007722;
color: white;
border: none;
padding: 8px 12px;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 14px;
">开始删除评论</button>
<div id="deleteStatus" style="
margin-top: 10px;
color: #666;
font-size: 12px;
"></div>
`;
document.body.appendChild(container);
return container;
}
// 获取页面信息
function getPageInfo() {
const ck = document.cookie.split(';').find(c => c.trim().startsWith('ck=')).split('=')[1];
const topicId = window.location.pathname.split('/').filter(p => p)[2];
return { ck, topicId };
}
// 触发删除评论
async function triggerDeleteComment(commentId) {
try {
const { ck, topicId } = getPageInfo();
const response = await fetch(`https://www.douban.com/group/topic/${topicId}/remove_comment`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest'
},
body: `cid=${commentId}&ck=${ck}`
});
console.log('删除请求状态:', response.status);
const responseText = await response.text();
console.log('删除响应内容:', responseText);
if (!response.ok) {
throw new Error(`删除请求失败: ${response.status}`);
}
return true;
} catch (error) {
console.error('删除请求失败:', error);
return false;
}
}
// 删除评论
async function deleteComment(commentItem) {
try {
const commentId = commentItem.getAttribute('data-cid');
console.log('找到评论ID:', commentId);
const success = await triggerDeleteComment(commentId);
if (success) {
commentItem.style.display = 'none';
return true;
}
return false;
} catch (error) {
console.error('删除评论出错:', error);
return false;
}
}
// 开始删除流程
async function startDeleting() {
const statusDiv = document.getElementById('deleteStatus');
const intervalInput = document.getElementById('deleteInterval');
const startButton = document.getElementById('startDelete');
const interval = Math.max(1, parseInt(intervalInput.value) || 5) * 1000;
let failCount = 0;
// 禁用按钮
startButton.disabled = true;
startButton.style.backgroundColor = '#999';
startButton.textContent = '删除中...';
// 获取当前用户的评论
const currentUserLink = document.querySelector('.user-info a')?.href;
if (!currentUserLink) {
statusDiv.textContent = '请先登录豆瓣';
startButton.disabled = false;
startButton.style.backgroundColor = '#007722';
startButton.textContent = '开始删除评论';
return;
}
const comments = Array.from(document.querySelectorAll('.comment-item')).filter(item => {
const authorLink = item.querySelector('.comment-info a');
return authorLink && authorLink.href === currentUserLink;
});
if (comments.length === 0) {
statusDiv.textContent = '没有找到您的评论';
startButton.disabled = false;
startButton.style.backgroundColor = '#007722';
startButton.textContent = '开始删除评论';
return;
}
statusDiv.textContent = `找到 ${comments.length} 条评论,开始删除...`;
for (const comment of comments) {
statusDiv.textContent = `正在删除评论...(剩余 ${comments.length - comments.indexOf(comment)} 条)`;
const success = await deleteComment(comment);
if (!success) {
failCount++;
if (failCount >= 5) {
statusDiv.textContent = '删除失败次数过多,已停止';
break;
}
}
// 等待指定时间
await new Promise(resolve => setTimeout(resolve, interval));
}
// 恢复按钮状态
startButton.disabled = false;
startButton.style.backgroundColor = '#007722';
startButton.textContent = '开始删除评论';
statusDiv.textContent = `删除完成(成功:${comments.length - failCount},失败:${failCount})`;
}
// 初始化
function init() {
// 检查是否在正确的页面
if (!window.location.pathname.includes('/group/topic/')) {
return;
}
// 创建UI并绑定事件
createUI();
document.getElementById('startDelete').addEventListener('click', startDeleting);
}
// 启动脚本
init();
})();