Strava自动点赞
// ==UserScript==
// @name Strava自动点赞
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.2.1
// @description 自动给Strava好友点赞
// @author jansfer
// @connect strava.com
// @match *://www.strava.com/dashboard*
// @match *://www.strava.com/athletes/*
// @match *://www.strava.com/clubs/*
// @icon https://www.strava.com/favicon.ico
// @grant GM_addStyle
// @grant GM_notification
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
let isRunning = false;
let processedCount = 0;
// 更全面的选择器,包含英文和中文界面
const kudosSelectors = [
'button[data-testid="kudos_button"]',
'button[title*="Give kudos"]',
'button[title*="点赞"]',
'button[title*="成为第一个点赞的人"]',
'button[title*="Be the first to give kudos"]',
'button.btn-sm.btn-primary[title*="kudos"]',
'.kudos-button:not(.selected)',
'button[aria-label*="kudos"]'
];
// 检查按钮是否已经点赞过
function isAlreadyKudoed(button) {
// 检查各种已点赞的标识
return button.classList.contains('selected') ||
button.classList.contains('active') ||
button.classList.contains('kudoed') ||
button.disabled ||
button.getAttribute('aria-pressed') === 'true' ||
button.title.includes('取消点赞') ||
button.title.includes('Undo kudos') ||
button.title.includes('查看所有赞');
}
// 查找所有可点赞的按钮
function findKudosButtons() {
let buttons = [];
kudosSelectors.forEach(selector => {
const elements = document.querySelectorAll(selector);
elements.forEach(button => {
if (!isAlreadyKudoed(button) && button.offsetParent !== null) {
buttons.push(button);
}
});
});
// 去重
return [...new Set(buttons)];
}
// 点击按钮的函数
function clickKudosButton(button) {
return new Promise((resolve) => {
try {
setTimeout(() => {
try {
// 方法1:直接调用click()方法
button.click();
processedCount++;
resolve(true);
} catch (clickError) {
// 方法2:使用简化的MouseEvent
try {
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true
});
button.dispatchEvent(event);
processedCount++;
resolve(true);
} catch (eventError) {
// 方法3:使用传统的事件创建方式
try {
const event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
button.dispatchEvent(event);
processedCount++;
resolve(true);
} catch (legacyError) {
console.error('所有点击方法都失败了:', legacyError);
resolve(false);
}
}
}
}, 200);
} catch (error) {
console.error('点击按钮时出错:', error);
resolve(false);
}
});
}
// 主要的点赞函数
async function performAutoKudos() {
if (isRunning) {
console.log('自动点赞已在运行中...');
return;
}
isRunning = true;
processedCount = 0;
try {
// 等待页面完全加载
await new Promise(resolve => setTimeout(resolve, 2000));
const buttons = findKudosButtons();
console.log(`找到 ${buttons.length} 个可点赞的按钮`);
if (buttons.length === 0) {
console.log('没有找到可点赞的内容');
isRunning = false;
return;
}
// 逐个点击按钮
for (let i = 0; i < buttons.length; i++) {
const button = buttons[i];
// 再次检查按钮状态(防止在等待过程中状态改变)
if (!isAlreadyKudoed(button)) {
console.log(`正在点赞第 ${i + 1}/${buttons.length} 个`);
await clickKudosButton(button);
// 随机等待时间,模拟人工操作
const waitTime = Math.random() * 1000 + 500; // 0.5-1.5秒
await new Promise(resolve => setTimeout(resolve, waitTime));
}
}
// 只有成功点赞了至少一个才显示通知
if (processedCount > 0) {
GM_notification(`点赞完成!共点赞了 ${processedCount} 个`, '成功', 'https://www.strava.com/favicon.ico');
}
} catch (error) {
console.error('自动点赞过程中出错:', error);
GM_notification('点赞过程中出现错误', '错误', 'https://www.strava.com/favicon.ico');
} finally {
isRunning = false;
}
}
// 添加手动触发按钮
function addControlButton() {
if (document.getElementById('strava-auto-kudos-btn')) return;
const button = document.createElement('button');
button.id = 'strava-auto-kudos-btn';
button.innerHTML = '🔥 自动点赞';
button.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 9999;
background: #fc4c02;
color: white;
border: none;
border-radius: 4px;
padding: 8px 12px;
font-size: 12px;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
`;
button.addEventListener('click', performAutoKudos);
document.body.appendChild(button);
}
// 页面变化监听器
function observePageChanges() {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
// 延迟执行,等待新内容加载完成
setTimeout(() => {
// 只在首页或仪表板页面自动执行
if (!isRunning) {
performAutoKudos();
}
}, 3000);
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
// 初始化
function init() {
console.log('Strava自动点赞脚本已加载');
// 添加控制按钮
addControlButton();
// 监听页面变化
observePageChanges();
// 延迟3秒后自动执行一次
setTimeout(performAutoKudos, 3000);
}
// 等待页面完全加载
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();