// ==UserScript== // @name 微博一键删除 // @namespace https://www.dominickk.top/ // @version 0.1.0 // @description 登录后在自己每一条微博下方增加“一键删除”按钮,用于省略删除点击步骤,慎用! // @author 🌹Dominic·KK🌹 // @match https://weibo.com/* // @icon https://d.sinaimg.cn/prd/106/1683/2025/08/28/feed_icon_2025kaixueji@2x.png // @grant none // ==/UserScript== (function() { 'use strict'; // 目标工具栏的类名 const targetClass = 'woo-box-flex woo-box-alignCenter toolbar_left_2vlsY toolbar_main_3Mxwo'; const processedElements = new Set(); // 获取当前登录用户的UUID function getCurrentUserUuid() { try { // 从导航栏中查找当前活跃的用户标签 const activeTab = document.querySelector('.Ctrls_main_35c80 a[href*="/u/"]'); if (activeTab) { const href = activeTab.getAttribute('href'); const match = href.match(/\/u\/(\d+)/); if (match) { // console.log('成功获取当前登录用户UUID:', match[1]); return match[1]; } } // console.warn('无法从导航栏获取当前登录用户UUID'); return null; } catch (e) { // console.error('获取当前用户UUID时发生错误:', e); return null; } } if(!getCurrentUserUuid()){ console.error('未登录或未能获取当前登录用户UUID,脚本终止'); return; } // 获取微博作者UUID(从微博头部的用户链接中提取) function getWeiboAuthorUuid(articleElement) { try { // 从微博头部查找作者链接 const authorLink = articleElement.querySelector('a[href*="/u/"]'); if (authorLink) { const href = authorLink.getAttribute('href'); const match = href.match(/\/u\/(\d+)/); if (match) { // console.log('成功获取微博作者UUID:', match[1]); return match[1]; } } // console.warn('无法从微博元素获取作者UUID'); return null; } catch (e) { // console.error('获取微博作者UUID时发生错误:', e); return null; } } // 检查是否应该为当前微博显示删除按钮 function shouldShowDeleteButton(articleElement) { const currentUserUuid = getCurrentUserUuid(); const weiboAuthorUuid = getWeiboAuthorUuid(articleElement); // console.log('权限检查 - 当前用户:', currentUserUuid, '微博作者:', weiboAuthorUuid); // 只有当当前用户是微博作者时才显示删除按钮 const shouldShow = currentUserUuid && weiboAuthorUuid && currentUserUuid === weiboAuthorUuid; if (!shouldShow) { // console.log('权限检查不通过,不显示删除按钮'); } else { // console.log('权限检查通过,显示删除按钮'); } return shouldShow; } // 创建一键删除按钮的函数 function createAuthorButton() { const authorContainer = document.createElement('div'); authorContainer.innerHTML = `
`; return authorContainer; } // 查找并点击更多菜单中的删除选项 function clickDeleteInMoreMenu(articleElement) { // 查找删除按钮 (文本内容为"删除"的元素) const deleteButtons = articleElement.querySelectorAll('.woo-pop-item-main'); for (let button of deleteButtons) { if (button.textContent.includes('删除')) { // console.log('找到删除按钮,正在点击...'); simulateClick(button); return true; } } // console.log('未找到删除按钮'); return false; } // 点击确认对话框中的确定按钮 function clickConfirmButton() { // 等待对话框完全加载 setTimeout(() => { const confirmButtons = document.querySelectorAll('.woo-button-content'); for (let button of confirmButtons) { if (button.textContent.includes('确定')) { // console.log('找到确认按钮,正在点击...'); simulateClick(button); break; } } }, 500); } // 模拟点击事件(解决元素点击被拦截问题) function simulateClick(element) { if (!element) return false; try { // 方法1: 直接调用click方法 element.click(); return true; } catch (e) { try { // 方法2: 使用dispatchEvent创建鼠标事件 const event = new MouseEvent('click', { view: window, bubbles: true, cancelable: true }); element.dispatchEvent(event); return true; } catch (error) { console.log('点击元素失败:', error); return false; } } } // 处理一键删除按钮点击 function handleDeleteClick(event) { event.stopPropagation(); const articleElement = event.target.closest('article'); if (!articleElement) { console.log('未找到对应的文章元素'); return; } // 再次验证权限(防止意外点击) if (!shouldShowDeleteButton(articleElement)) { console.log('无删除权限,操作已阻止'); return; } // 1. 首先点击更多按钮 const moreButton = articleElement.querySelector('.woo-font.woo-font--angleDown.morepop_action_bk3Fq'); if (moreButton) { // console.log('找到更多按钮,正在点击...'); simulateClick(moreButton); // 2. 等待菜单展开后点击删除选项 setTimeout(() => { if (clickDeleteInMoreMenu(articleElement)) { // 3. 等待确认对话框出现后点击确定按钮 setTimeout(() => { clickConfirmButton(); }, 500); } }, 500); } else { console.log('未找到更多按钮'); } } // 处理单个工具栏元素 function processElement(element) { if (processedElements.has(element)) return; // 找到对应的article元素 const articleElement = element.closest('article'); if (!articleElement) { console.log('未找到对应的article元素'); return; } // 检查权限:只有当前用户的微博才显示删除按钮 if (!shouldShowDeleteButton(articleElement)) { // console.log('无权限添加删除按钮,跳过此元素'); return; } const authorButton = createAuthorButton(); element.appendChild(authorButton); // 绑定点击事件 const deleteButton = authorButton.querySelector('button'); if (deleteButton) { deleteButton.addEventListener('click', handleDeleteClick); } processedElements.add(element); // console.log('已添加一键删除按钮'); } // 初始处理现有元素 function initialProcess() { const existingElements = document.querySelectorAll('.' + targetClass.split(' ').join('.')); // console.log(`找到 ${existingElements.length} 个目标工具栏元素`); existingElements.forEach(processElement); console.log('初始化完成'); } // 设置MutationObserver监听动态内容 function setupMutationObserver() { const observer = new MutationObserver(function(mutationsList) { for (let mutation of mutationsList) { if (mutation.type === 'childList') { mutation.addedNodes.forEach(function(node) { if (node.nodeType === Node.ELEMENT_NODE) { // 检查新增元素是否为目标工具栏 if (node.classList && node.classList.contains('woo-box-flex') && node.classList.contains('toolbar_left_2vlsY') && node.classList.contains('toolbar_main_3Mxwo')) { setTimeout(() => processElement(node), 100); } // 检查子元素中的目标工具栏 const childTargets = node.querySelectorAll ? node.querySelectorAll('.' + targetClass.split(' ').join('.')) : []; childTargets.forEach((element) => { setTimeout(() => processElement(element), 100); }); } }); } } }); observer.observe(document.body, { childList: true, subtree: true }); // console.log('MutationObserver 已启动'); return observer; } // 页面加载初始化 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function() { initialProcess(); setupMutationObserver(); }); } else { initialProcess(); setupMutationObserver(); } })();