1.购买服务器阿里云:服务器购买地址https://t.aliyun.com/U/Bg6shY若失效,可用地址
阿里云:
服务器购买地址
https://t.aliyun.com/U/Bg6shY若失效,可用地址
https://www.aliyun.com/daily-act/ecs/activity_selection?source=5176.29345612&userCode=49hts92d腾讯云:
https://curl.qcloud.com/wJpWmSfU若失效,可用地址
https://cloud.tencent.com/act/cps/redirect?redirect=2446&cps_key=ad201ee2ef3b771157f72ee5464b1fea&from=console华为云
https://activity.huaweicloud.com/cps.html?fromacct=64b5cf7cc11b4840bb4ed2ea0b2f4468&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=2019052.部署教程
3.代码如下
(function () {'use strict';/** 配置区域 **/const COMMENT_TEXT = "测试"; // 评论内容const COMMENT_INTERVAL = 5000; // 评论间隔时间,单位为毫秒/** 工具函数 **/function sleep(ms) {return new Promise(resolve => setTimeout(resolve, ms));}function getCommentedIds() {return GM_getValue('commented_ids', []);}function saveCommentedId(id) {const commentedIds = getCommentedIds();commentedIds.push(id);GM_setValue('commented_ids', commentedIds);}function isAlreadyCommented(id) {const commentedIds = getCommentedIds();return commentedIds.includes(id);}function scrollToElement(element) {element.scrollIntoView({ behavior: 'smooth', block: 'center' });}/** 点击评论按钮并提交评论 **/async function commentOnWeibo(item) {const weiboId = item.getAttribute('mid'); // 微博唯一 IDif (!weiboId || isAlreadyCommented(weiboId)) {return;}scrollToElement(item);// 选择第二个按钮(评论按钮)const commentButtons = item.querySelectorAll('.card-act .woo-box-flex.woo-box-alignCenter.woo-box-justifyCenter');if (commentButtons.length < 3) {return}const commentButton = commentButtons[2]; // 第0和1个是转发,第二个按钮是评论按钮,最后是转发if (commentButton) {commentButton.click();await sleep(2000); // 等待评论框弹出const commentBox = item.querySelector('div.input textarea[node-type="textEl"]'); // 定位评论框const submitButton = item.querySelector('div.input + div a[action-type="post"'); // 提交按钮定位if (commentBox && submitButton) {commentBox.value = COMMENT_TEXT;// 触发 input 事件,确保输入内容生效commentBox.dispatchEvent(new Event('input', { bubbles: true }));submitButton.click();console.log(`评论成功: ${weiboId},评论内容:${COMMENT_TEXT}`);saveCommentedId(weiboId);} else {console.log(`未找到评论框或提交按钮,跳过: ${weiboId}`);}await sleep(COMMENT_INTERVAL); // 控制评论间隔}}/** 自动翻页并评论 **/async function autoComment() {let page = 1;while (true) {const weiboItems = document.querySelectorAll('.card-wrap');for (let item of weiboItems) {await commentOnWeibo(item);}// 翻页逻辑const nextPageButton = document.querySelector('a.next'); // 下一页按钮if (nextPageButton) {console.log('翻到下一页...');nextPageButton.click();await sleep(5000); // 等待新页面加载page++;} else {console.log('已到达最后一页,脚本结束。');break;}}}// 启动脚本autoComment();})();
该脚本为微博自动刷评论脚本。
主要作用
在微博搜索页(
https://weibo.com/*)上,自动遍历每一条搜索结果,为未评论过的微博自动发表评论(固定文案,默认"测试")。自动翻页继续对后续页面的微博逐条评论。
通过本地存储(Tampermonkey 的
GM_setValue/GM_getValue)记录已评论微博的 ID,避免重复评论。在评论之间加入固定间隔(默认 5 秒)。
主要方法
1) 配置常量
COMMENT_TEXT:评论内容。COMMENT_INTERVAL:两次评论的间隔(毫秒)。
2) 工具函数
sleep(ms):Promise 版延时工具,用于控制操作节奏。getCommentedIds()/saveCommentedId(id)/isAlreadyCommented(id):
读/写/判断本地已评论 ID 列表(使用GM_getValue/GM_setValue),实现"去重评论"。scrollToElement(element):滚动页面让目标微博出现在可视区域(有助于触发展开/渲染)。
3) 核心:对单条微博发表评论
async function commentOnWeibo(item) { ... }
通过
item.getAttribute('mid')取微博唯一 ID;若无 ID 或已评论过则跳过。定位卡片操作区,取第三个按钮作为评论按钮(依赖选择器:
.card-act .woo-box-flex.woo-box-alignCenter.woo-box-justifyCenter),点击后等待输入框出现。找评论输入框与提交按钮(分别用
textarea[node-type="textEl"]与a[action-type="post"]选择器),写入COMMENT_TEXT,触发input事件,再点击提交。记录已评论 ID,并在两次评论之间
sleep(COMMENT_INTERVAL)。
说明:这里对 DOM 结构(按钮序号与选择器)有强依赖,微博改版会失效。
4) 自动翻页与遍历
async function autoComment() { ... }
循环当前页面全部微博卡片(
.card-wrap),对每条调用commentOnWeibo。翻页:查找
a.next(下一页)并点击,等待 5 秒后继续下一页;若无"下一页"则结束。
5) 启动时机
@run-at document-end,文档尾部执行,立即调用autoComment()开始流程。
6) 头部权限/匹配
@match https://weibo.com/*:仅在搜索页生效。@grant GM_setValue / GM_getValue / GM_xmlhttpRequest:本脚本实际使用前两个(存取已评论 ID),GM_xmlhttpRequest此版本未用到。
注意:
本文部分变量已做脱敏处理,仅用于测试和学习研究,禁止用于商业用途,不能保证其合法性,准确性,完整性和有效性,请根据情况自行判断。技术层面需要提供帮助,可以通过打赏的方式进行探讨。
没有评论:
发表评论