Diskusie » Vývoj

极度科技-免费版AI自动微博监控最新帖子并可选自动点赞、评论、转发

§
Pridaný: 16.10.2024

// ==UserScript==
// @name 极度科技-免费版AI自动微博监控最新帖子并可选自动点赞、评论、转发
// @namespace http://cs.06001.cn/
// @version 1.6
// @description 免费版AI自动微博监控最新帖子并可选自动点赞、评论、转发
// @author 极度科技(157574701/jdwl06001)
// @match https://weibo.com/u/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=weibo.com
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @connect weibo.com
// @connect dashscope.aliyuncs.com
// @downloadURL https://update.greasyfork.org/scripts/512886/%E6%9E%81%E5%BA%A6%E7%A7%91%E6%8A%80-%E5%85%8D%E8%B4%B9%E7%89%88AI%E8%87%AA%E5%8A%A8%E5%BE%AE%E5%8D%9A%E7%9B%91%E6%8E%A7%E6%9C%80%E6%96%B0%E5%B8%96%E5%AD%90%E5%B9%B6%E5%8F%AF%E9%80%89%E8%87%AA%E5%8A%A8%E7%82%B9%E8%B5%9E%E3%80%81%E8%AF%84%E8%AE%BA%E3%80%81%E8%BD%AC%E5%8F%91.user.js
// @updateURL https://update.greasyfork.org/scripts/512886/%E6%9E%81%E5%BA%A6%E7%A7%91%E6%8A%80-%E5%85%8D%E8%B4%B9%E7%89%88AI%E8%87%AA%E5%8A%A8%E5%BE%AE%E5%8D%9A%E7%9B%91%E6%8E%A7%E6%9C%80%E6%96%B0%E5%B8%96%E5%AD%90%E5%B9%B6%E5%8F%AF%E9%80%89%E8%87%AA%E5%8A%A8%E7%82%B9%E8%B5%9E%E3%80%81%E8%AF%84%E8%AE%BA%E3%80%81%E8%BD%AC%E5%8F%91.meta.js
// ==/UserScript==

(function() {
'use strict';

console.log("Weibo Latest Post Monitor with Auto-Like, Comment, Repost 脚本已加载。");

// 初始化设置
const DEFAULT_INTERVAL = 5000; // 默认5秒
const SETTING_KEY = 'weibo_monitor_settings';
const LIKED_MIDS_KEY = 'weibo_liked_mids';
const COMMENTED_MIDS_KEY = 'weibo_commented_mids';
const REPOSTED_MIDS_KEY = 'weibo_reposted_mids';

// 获取或设置默认配置
let settings = GM_getValue(SETTING_KEY, {
interval: DEFAULT_INTERVAL,
autoLike: false,
autoComment: false,
autoRepost: false,
aiApiKey: ''
});

// 获取已执行的操作列表
let likedMids = GM_getValue(LIKED_MIDS_KEY, []);
let commentedMids = GM_getValue(COMMENTED_MIDS_KEY, []);
let repostedMids = GM_getValue(REPOSTED_MIDS_KEY, []);

// 1. 自动提取UID
const url = window.location.href;
const uidMatch = url.match(/https:\/\/weibo\.com\/u\/(\d+)/);
if (!uidMatch || uidMatch.length < 2) {
alert("无法从URL中提取UID,请确保URL格式为 https://weibo.com/u/UID");
console.error("URL不匹配或未找到UID:", url);
return;
}
const uid = uidMatch[1];
console.log("提取到的UID:", uid);

// 2. 获取当前页面的Cookie
const cookie = document.cookie;
if (!cookie) {
alert("无法获取Cookie,请确保已登录微博并重试。");
console.error("未获取到Cookie。");
return;
}

// 3. 从Cookie中解析XSRF-TOKEN
const xsrfMatch = cookie.match(/XSRF-TOKEN=([^;]+)/);
if (!xsrfMatch) {
alert("未找到XSRF-TOKEN,请检查Cookie是否包含该项。");
console.error("未找到XSRF-TOKEN:", cookie);
return;
}
const xsrfToken = decodeURIComponent(xsrfMatch[1]);
console.log("提取到的XSRF-TOKEN:", xsrfToken);

// 4. 创建配置面板
function createConfigPanel() {
// 检查是否已存在配置面板
if (document.getElementById('weibo-config-panel')) return;

const panel = document.createElement('div');
panel.id = 'weibo-config-panel';
panel.style.position = 'fixed';
panel.style.top = '10px';
panel.style.right = '10px';
panel.style.padding = '15px';
panel.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
panel.style.color = '#fff';
panel.style.borderRadius = '8px';
panel.style.zIndex = '10000';
panel.style.fontSize = '14px';
panel.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';
panel.style.maxWidth = '300px';

panel.innerHTML = `

微博监控设置


监控间隔 (秒):







自动点赞





自动评论





自动转发





千义通问 API Key:





保存设置
关闭




极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]千义通问 API Key申请地址:https://bailian.console.aliyun.com/?apiKey=1#/api-key
(需登录阿里云且实名账号,赠送100万Token 免费额度有效期为180天)

`;

document.body.appendChild(panel);

// 绑定事件
document.getElementById('save-settings').addEventListener('click', () => {
const intervalInput = document.getElementById('monitor-interval').value;
const autoLikeInput = document.getElementById('auto-like').checked;
const autoCommentInput = document.getElementById('auto-comment').checked;
const autoRepostInput = document.getElementById('auto-repost').checked;
const aiApiKeyInput = document.getElementById('ai-api-key').value.trim();

const newSettings = {
interval: parseInt(intervalInput) * 1000,
autoLike: autoLikeInput,
autoComment: autoCommentInput,
autoRepost: autoRepostInput,
aiApiKey: aiApiKeyInput
};

// 更新设置
GM_setValue(SETTING_KEY, newSettings);
settings = newSettings;
console.log("设置已保存:", settings);

alert("设置已保存,页面即将刷新以应用新设置。");
window.location.reload();
});

document.getElementById('close-panel').addEventListener('click', () => {
panel.style.display = 'none';
});
}

// 5. 创建信息显示区域
function createInfoDisplay() {
let infoDiv = document.getElementById('latest-post-info');
if (!infoDiv) {
infoDiv = document.createElement('div');
infoDiv.id = 'latest-post-info';
infoDiv.style.position = 'fixed';
infoDiv.style.bottom = '10px';
infoDiv.style.right = '10px';
infoDiv.style.padding = '10px';
infoDiv.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
infoDiv.style.color = '#fff';
infoDiv.style.borderRadius = '5px';
infoDiv.style.zIndex = '10000';
infoDiv.style.fontSize = '14px';
infoDiv.style.maxWidth = '300px';
infoDiv.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';
document.body.appendChild(infoDiv);
}
return infoDiv;
}

// 6. 转换时间格式
function convertTime(createdAt) {
const date = new Date(createdAt);
return date.toLocaleString();
}

// 7. 发送点赞请求
function sendLike(mid) {
const likeUrl = `https://weibo.com/ajax/statuses/setLike`;

const likePayload = JSON.stringify({ id: mid });

GM_xmlhttpRequest({
method: "POST",
url: likeUrl,
headers: {
"Content-Type": "application/json",
"X-XSRF-TOKEN": xsrfToken,
"Referer": `https://weibo.com/u/${uid}`,
"Origin": "https://weibo.com"
},
data: likePayload,
onload: function(response) {
if (response.status !== 200) {
console.error(`点赞请求失败,状态码:${response.status}`);
infoDiv.innerHTML += `
❌ 点赞失败 MID: ${mid}`;
return;
}

try {
const json = JSON.parse(response.responseText);
if (json.ok === 1) {
console.log(`点赞成功,MID: ${mid}`);
// 记录已点赞的mid
if (!likedMids.includes(mid)) {
likedMids.push(mid);
GM_setValue(LIKED_MIDS_KEY, likedMids);
}
// 更新显示信息
infoDiv.innerHTML += `
✅ 已点赞 MID: ${mid}`;
} else {
console.warn(`点赞失败,MID: ${mid}`);
infoDiv.innerHTML += `
❌ 点赞失败 MID: ${mid}`;
}
} catch (e) {
console.error("解析点赞响应数据时发生错误:", e);
}
},
onerror: function(error) {
console.error("点赞请求过程中发生错误:", error);
infoDiv.innerHTML += `
❌ 点赞请求错误 MID: ${mid}`;
}
});
}

// 8. 发送评论请求
function sendComment(mid, commentContent, retryCount = 0) {
const commentUrl = `https://weibo.com/ajax/comments/create`;

const commentPayload = `id=${mid}&comment=${encodeURIComponent(commentContent)}&pic_id=&is_repost=0&comment_ori=0&is_comment=0`;

GM_xmlhttpRequest({
method: "POST",
url: commentUrl,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"X-XSRF-TOKEN": xsrfToken,
"Referer": `https://weibo.com/u/${uid}`,
"Origin": "https://weibo.com"
},
data: commentPayload,
onload: function(response) {
if (response.status !== 200) {
console.error(`评论请求失败,状态码:${response.status}`);
if (retryCount < 3) {
console.log(`重试评论请求,MID: ${mid},第 ${retryCount + 1} 次`);
sendComment(mid, commentContent, retryCount + 1);
} else {
console.warn(`评论失败,MID: ${mid},已达到最大重试次数`);
infoDiv.innerHTML += `
❌ 评论失败 MID: ${mid}`;
}
return;
}

try {
const json = JSON.parse(response.responseText);
if (json.ok === 1) {
console.log(`评论成功,MID: ${mid}`);
// 记录已评论的mid
if (!commentedMids.includes(mid)) {
commentedMids.push(mid);
GM_setValue(COMMENTED_MIDS_KEY, commentedMids);
}
// 更新显示信息
infoDiv.innerHTML += `
💬 已评论 MID: ${mid}`;
} else {
console.warn(`评论失败,MID: ${mid},原因: ${json.message}`);
if (retryCount < 3) {
console.log(`重试评论请求,MID: ${mid},第 ${retryCount + 1} 次`);
sendComment(mid, commentContent, retryCount + 1);
} else {
console.warn(`评论失败,MID: ${mid},已达到最大重试次数`);
infoDiv.innerHTML += `
❌ 评论失败 MID: ${mid}`;
}
}
} catch (e) {
console.error("解析评论响应数据时发生错误:", e);
if (retryCount < 3) {
console.log(`重试评论请求,MID: ${mid},第 ${retryCount + 1} 次`);
sendComment(mid, commentContent, retryCount + 1);
} else {
console.warn(`评论失败,MID: ${mid},已达到最大重试次数`);
infoDiv.innerHTML += `
❌ 评论失败 MID: ${mid}`;
}
}
},
onerror: function(error) {
console.error("评论请求过程中发生错误:", error);
if (retryCount < 3) {
console.log(`重试评论请求,MID: ${mid},第 ${retryCount + 1} 次`);
sendComment(mid, commentContent, retryCount + 1);
} else {
console.warn(`评论失败,MID: ${mid},已达到最大重试次数`);
infoDiv.innerHTML += `
❌ 评论请求错误 MID: ${mid}`;
}
}
});
}

// 9. 发送转发请求
function sendRepost(mid, repostContent, retryCount = 0) {
const repostUrl = `https://weibo.com/ajax/statuses/normal_repost`;

const repostPayload = `id=${mid}&comment=${encodeURIComponent(repostContent)}&pic_id=&is_repost=0&comment_ori=0&is_comment=0&visible=0&share_id=`;

GM_xmlhttpRequest({
method: "POST",
url: repostUrl,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"X-XSRF-TOKEN": xsrfToken,
"Referer": `https://weibo.com/u/${uid}`,
"Origin": "https://weibo.com"
},
data: repostPayload,
onload: function(response) {
if (response.status !== 200) {
console.error(`转发请求失败,状态码:${response.status}`);
if (retryCount < 3) {
console.log(`重试转发请求,MID: ${mid},第 ${retryCount + 1} 次`);
sendRepost(mid, repostContent, retryCount + 1);
} else {
console.warn(`转发失败,MID: ${mid},已达到最大重试次数`);
infoDiv.innerHTML += `
❌ 转发失败 MID: ${mid}`;
}
return;
}

try {
const json = JSON.parse(response.responseText);
if (json.ok === 1) {
console.log(`转发成功,MID: ${mid}`);
// 记录已转发的mid
if (!repostedMids.includes(mid)) {
repostedMids.push(mid);
GM_setValue(REPOSTED_MIDS_KEY, repostedMids);
}
// 更新显示信息
infoDiv.innerHTML += `
🔁 已转发 MID: ${mid}`;
} else {
console.warn(`转发失败,MID: ${mid},原因: ${json.message}`);
if (retryCount < 3) {
console.log(`重试转发请求,MID: ${mid},第 ${retryCount + 1} 次`);
sendRepost(mid, repostContent, retryCount + 1);
} else {
console.warn(`转发失败,MID: ${mid},已达到最大重试次数`);
infoDiv.innerHTML += `
❌ 转发失败 MID: ${mid}`;
}
}
} catch (e) {
console.error("解析转发响应数据时发生错误:", e);
if (retryCount < 3) {
console.log(`重试转发请求,MID: ${mid},第 ${retryCount + 1} 次`);
sendRepost(mid, repostContent, retryCount + 1);
} else {
console.warn(`转发失败,MID: ${mid},已达到最大重试次数`);
infoDiv.innerHTML += `
❌ 转发失败 MID: ${mid}`;
}
}
},
onerror: function(error) {
console.error("转发请求过程中发生错误:", error);
if (retryCount < 3) {
console.log(`重试转发请求,MID: ${mid},第 ${retryCount + 1} 次`);
sendRepost(mid, repostContent, retryCount + 1);
} else {
console.warn(`转发失败,MID: ${mid},已达到最大重试次数`);
infoDiv.innerHTML += `
❌ 转发请求错误 MID: ${mid}`;
}
}
});
}

// 10. 使用AI生成评论内容
async function generateAIComment(postText) {
if (!settings.aiApiKey) {
console.warn("未提供AI API Key,使用默认评论。");
return "极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]";
}

try {
const response = await fetch('https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${settings.aiApiKey}`
},
body: JSON.stringify({
model: "qwen-turbo",
messages: [
{ role: 'system', content: '你是一个评论大师,按照这个帖子内容给我写一条十字左右的评论' },
{ role: 'user', content: postText }
]
})
});

if (!response.ok) {
console.error(`AI 请求失败,状态码:${response.status}`);
return "极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]";
}

const data = await response.json();
const aiComment = data.choices && data.choices[0].message.content.trim();
if (aiComment) {
console.log(`AI生成的评论: ${aiComment}`);
return aiComment;
} else {
console.warn("AI 未生成有效的评论,使用默认评论。");
return "极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]";
}
} catch (error) {
console.error("AI生成评论时发生错误:", error);
return "极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]";
}
}

// 11. 使用AI生成转发内容
async function generateAIRepost(postText) {
if (!settings.aiApiKey) {
console.warn("未提供AI API Key,使用默认转发内容。");
return "极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]";
}

try {
const response = await fetch('https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${settings.aiApiKey}`
},
body: JSON.stringify({
model: "qwen-turbo",
messages: [
{ role: 'system', content: '你是一个评论大师,按照这个帖子内容给我写一条十字左右的转发内容' },
{ role: 'user', content: postText }
]
})
});

if (!response.ok) {
console.error(`AI 请求失败,状态码:${response.status}`);
return "极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]";
}

const data = await response.json();
const aiRepost = data.choices && data.choices[0].message.content.trim();
if (aiRepost) {
console.log(`AI生成的转发内容: ${aiRepost}`);
return aiRepost;
} else {
console.warn("AI 未生成有效的转发内容,使用默认转发内容。");
return "极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]";
}
} catch (error) {
console.error("AI生成转发内容时发生错误:", error);
return "极度科技-免费版AI自动监控微博最新帖子并可选自动点赞、评论、转发联系157574701(备注免费js脚本)[功能:1.自动获取登录账号的Cookie2.自动筛选判断最新帖子3.可设置是否点赞、是否评论、是否转发5.可对接ai自动生成回复内容和转发内容]";
}
}

// 12. 获取并处理最新帖子
async function fetchLatestPost() {
const apiUrl = `https://weibo.com/ajax/statuses/mymblog?uid=${uid}&page=1&feature=0`;
console.log("请求URL:", apiUrl);

GM_xmlhttpRequest({
method: "GET",
url: apiUrl,
headers: {
"X-XSRF-TOKEN": xsrfToken,
"Referer": `https://weibo.com/u/${uid}`
},
onload: async function(response) {
console.log("请求完成,状态码:", response.status);
if (response.status !== 200) {
console.error(`请求失败,状态码:${response.status}`, response.responseText);
infoDiv.innerHTML = `请求失败,状态码:${response.status}`;
return;
}

try {
const json = JSON.parse(response.responseText);
console.log("响应JSON数据:", json);
if (!json.data || !json.data.list || json.data.list.length === 0) {
console.warn("未找到微博数据。");
infoDiv.innerHTML = `未找到微博数据。`;
return;
}

const posts = json.data.list;
let latestPost = null;
let latestDate = null;

// 遍历所有帖子,找出最新的
posts.forEach(post => {
const mid = post.mid;
const createdAt = post.created_at;
const date = new Date(createdAt);
console.log(`帖子 MID: ${mid}, 发布时间: ${createdAt}`);
if (!latestDate || date > latestDate) {
latestDate = date;
latestPost = post;
}
});

if (latestPost) {
const latestMid = latestPost.mid;
const latestCreatedAt = convertTime(latestPost.created_at);
const postText = latestPost.text;
// 检查是否已点赞、评论、转发
const alreadyLiked = likedMids.includes(latestMid);
const alreadyCommented = commentedMids.includes(latestMid);
const alreadyReposted = repostedMids.includes(latestMid);

// 显示结果
infoDiv.innerHTML = `最新微博:
MID: ${latestMid}
发布时间: ${latestCreatedAt}`;

// 自动点赞
if (settings.autoLike && !alreadyLiked) {
sendLike(latestMid);
} else if (alreadyLiked) {
infoDiv.innerHTML += `
👍 已点赞该微博。`;
}

// 自动评论
if (settings.autoComment && !alreadyCommented) {
const commentContent = await generateAIComment(postText);
sendComment(latestMid, commentContent);
} else if (alreadyCommented) {
infoDiv.innerHTML += `
💬 已评论该微博。`;
}

// 自动转发
if (settings.autoRepost && !alreadyReposted) {
const repostContent = await generateAIRepost(postText);
sendRepost(latestMid, repostContent);
} else if (alreadyReposted) {
infoDiv.innerHTML += `
🔁 已转发该微博。`;
}
} else {
console.warn("未找到最新的帖子。");
infoDiv.innerHTML = `未找到最新的帖子。`;
}

} catch (e) {
console.error("解析响应数据时发生错误:", e);
infoDiv.innerHTML = `解析响应数据时发生错误。`;
}
},
onerror: function(error) {
console.error("请求过程中发生错误:", error);
infoDiv.innerHTML = `请求过程中发生错误。`;
}
});
}

// 13. 创建配置按钮
function createConfigButton() {
let button = document.getElementById('weibo-config-button');
if (!button) {
button = document.createElement('button');
button.id = 'weibo-config-button';
button.innerText = '设置';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '20px'; // 调整位置,避免与设置面板重叠
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = '#fff';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
button.style.zIndex = '10000';
button.style.boxShadow = '0 0 5px rgba(0,0,0,0.3)';
document.body.appendChild(button);

button.addEventListener('click', () => {
createConfigPanel();
});
}
}

// 14. 创建信息显示区域
const infoDiv = createInfoDisplay();

// 15. 创建配置按钮
createConfigButton();

// 16. 初始调用
fetchLatestPost();

// 17. 设置定时任务
let monitorInterval = settings.interval;
setInterval(() => {
console.log("定时任务:获取最新帖子");
fetchLatestPost();
}, monitorInterval);

})();

Pridať odpoveď

Aby ste mohli pridať odpoveď, prihláste sa.