|
|
- /**
- * 微博聊天一键删除所有会话框 - 循环删除版
- *
- * 使用方法:
- * 1. 打开微博聊天网页版 https://api.weibo.com/chat#/chat 并登录
- * 2. 按 F12 打开开发者工具
- * 3. 切换到 Console(控制台)标签
- * 4. 粘贴此脚本并按回车
- * 5. 输入 deleteAllConversations() 并回车执行
- */
- (function() {
- 'use strict';
- // 配置
- const config = {
- deleteDelay: 800, // 每次删除后等待时间(毫秒)
- maxIterations: 100, // 最大循环次数
- showProgress: true // 显示进度
- };
- // 多种可能的选择器
- const selectors = [
- // 聊天列表项
- '.chat-item',
- '.conversation-item',
- '.session-item',
- '.list-item',
- '[class*="chatList"] > div',
- '[class*="conversation"] > div',
- '[class*="session"] > div',
- // 通用选择器
- 'ul.chat-list > li',
- 'div[class*="list"] > div[class*="item"]',
- // 查找包含聊天气泡或消息的元素
- '[class*="bubble"]',
- // 滚动列表中的项目
- '.scroll-item',
- '.item-wrapper'
- ];
- // 查找所有会话元素
- function findConversations() {
- let elements = [];
- // 尝试每种选择器
- for (const selector of selectors) {
- const found = document.querySelectorAll(selector);
- if (found.length > 0) {
- // 过滤掉非会话的元素(如广告、提示等)
- elements = Array.from(found).filter(el => {
- const text = el.textContent || '';
- const className = el.className || '';
- // 排除明显不是会话的元素
- if (text.includes('广告') || text.includes('推广')) return false;
- if (className.includes('header') || className.includes('footer')) return false;
- // 保留可能包含用户名/消息的元素
- return true;
- });
- if (elements.length > 0) {
- console.log(`使用选择器: ${selector}, 找到 ${elements.length} 个元素`);
- return elements;
- }
- }
- }
- // 最后尝试:查找所有可能是列表项的元素
- const allDivs = document.querySelectorAll('div');
- elements = Array.from(allDivs).filter(el => {
- const rect = el.getBoundingClientRect();
- return rect.width > 100 && rect.height > 30 && rect.height < 200;
- });
- return elements;
- }
- // 查找删除按钮
- function findDeleteButton(item) {
- // 方法1:直接查找带有删除相关文字/属性的按钮
- const allButtons = item.querySelectorAll('button, span[role="button"], div[role="button"]');
- for (const btn of allButtons) {
- const text = (btn.textContent || '').trim().toLowerCase();
- const title = (btn.title || '').toLowerCase();
- const className = (btn.className || '').toLowerCase();
- if (text.includes('删除') || text.includes('del') ||
- title.includes('删除') || className.includes('delete') ||
- className.includes('remove')) {
- return btn;
- }
- }
- // 方法2:查找图标按钮(删除通常是垃圾桶图标)
- const iconButtons = item.querySelectorAll('[class*="icon"], svg');
- for (const icon of iconButtons) {
- const parent = icon.closest('button') || icon.closest('[role="button"]');
- if (parent) return parent;
- }
- return null;
- }
- // 触发删除
- async function triggerDelete(item) {
- try {
- // 方法1:点击删除按钮
- const deleteBtn = findDeleteButton(item);
- if (deleteBtn) {
- deleteBtn.click();
- await wait(300);
- // 查找确认对话框
- const confirmButtons = document.querySelectorAll('button, div[role="button"], span[role="button"]');
- for (const btn of confirmButtons) {
- const text = (btn.textContent || '').trim().toLowerCase();
- if (text === '确定' || text === '确认' || text.includes('删除') || text === 'ok' || text === 'yes') {
- if (!text.includes('取消')) {
- btn.click();
- return true;
- }
- }
- }
- return true;
- }
- // 方法2:右键点击唤出菜单
- const rect = item.getBoundingClientRect();
- item.dispatchEvent(new MouseEvent('contextmenu', {
- bubbles: true,
- clientX: rect.left + rect.width / 2,
- clientY: rect.top + rect.height / 2
- }));
- await wait(500);
- // 查找菜单中的删除选项
- const menuItems = document.querySelectorAll('[class*="menu"] li, [class*="dropdown"] li, [class*="contextmenu"] li');
- for (const menuItem of menuItems) {
- const text = (menuItem.textContent || '').toLowerCase();
- if (text.includes('删除') || text.includes('del') || text.includes('remove')) {
- menuItem.click();
- await wait(300);
- // 再次确认
- const confirmButtons = document.querySelectorAll('button, div[role="button"]');
- for (const btn of confirmButtons) {
- const btnText = (btn.textContent || '').trim().toLowerCase();
- if (btnText === '确定' || btnText === '确认' || btnText === 'ok') {
- btn.click();
- return true;
- }
- }
- return true;
- }
- }
- // 方法3:直接移除元素(最后手段)
- if (item.remove) {
- item.remove();
- return true;
- } else if (item.parentNode) {
- item.parentNode.removeChild(item);
- return true;
- }
- return false;
- } catch (error) {
- console.error('删除出错:', error);
- return false;
- }
- }
- // 等待函数
- function wait(ms) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- // 主函数 - 循环删除所有会话
- async function deleteAllConversations() {
- console.log('%c 微博聊天 - 循环删除所有会话 ', 'background: #ff6b6b; color: white; font-size: 16px; padding: 10px;');
- // 确认
- const confirmed = confirm('确定要删除微博聊天的所有会话吗?\n\n脚本将循环删除直到全部删除完成。');
- if (!confirmed) {
- console.log('已取消');
- return;
- }
- let totalDeleted = 0;
- let iteration = 0;
- while (iteration < config.maxIterations) {
- iteration++;
- // 每次都重新获取会话列表
- const conversations = findConversations();
- if (conversations.length === 0) {
- console.log('%c ✓ 所有会话已删除完成! ', 'background: #4ecdc4; color: white; font-size: 14px; padding: 8px;');
- console.log(`总计删除: ${totalDeleted} 个会话`);
- alert(`删除完成!共删除 ${totalDeleted} 个会话`);
- return;
- }
- if (config.showProgress) {
- console.log(`第 ${iteration} 轮:还有 ${conversations.length} 个会话待删除,已删除 ${totalDeleted} 个`);
- }
- // 删除当前列表中的第一个会话
- const item = conversations[0];
- const success = await triggerDelete(item);
- if (success) {
- totalDeleted++;
- }
- // 等待DOM更新
- await wait(config.deleteDelay);
- }
- console.log(`已达到最大循环次数 ${config.maxIterations},已删除 ${totalDeleted} 个会话`);
- alert(`完成!共删除 ${totalDeleted} 个会话\n如果还有剩余,请刷新页面后重新执行`);
- }
- // 快速删除模式 - 每次删除多个
- async function fastDelete() {
- console.log('%c 微博聊天 - 快速删除模式 ', 'background: #45b7d1; color: white; font-size: 16px; padding: 10px;');
- const confirmed = confirm('快速删除模式:\n\n将尽可能快地批量删除所有会话');
- if (!confirmed) return;
- let totalDeleted = 0;
- let emptyCount = 0;
- while (emptyCount < 5) {
- const conversations = findConversations();
- if (conversations.length === 0) {
- emptyCount++;
- await wait(500);
- continue;
- }
- emptyCount = 0;
- // 每次尝试删除前3个
- const toDelete = conversations.slice(0, 3);
- for (const item of toDelete) {
- await triggerDelete(item);
- }
- totalDeleted += toDelete.length;
- console.log(`已删除 ${totalDeleted} 个...`);
- await wait(config.deleteDelay);
- }
- alert(`快速删除完成!共删除 ${totalDeleted} 个会话`);
- }
- // 导出函数
- window.deleteAllConversations = deleteAllConversations;
- window.fastDelete = fastDelete;
- console.log('%c 微博聊天删除脚本已加载 ', 'background: #4ecdc4; color: white; font-size: 14px; padding: 8px;');
- console.log('可用函数:');
- console.log(' deleteAllConversations() - 循环删除所有会话');
- console.log(' fastDelete() - 快速删除模式');
- })();
复制代码 |
|