Skip to content

Instantly share code, notes, and snippets.

@jwmann
Last active July 20, 2023 06:07
Show Gist options
  • Save jwmann/d59bc9c35d30ad96717808f70588a999 to your computer and use it in GitHub Desktop.
Save jwmann/d59bc9c35d30ad96717808f70588a999 to your computer and use it in GitHub Desktop.
Auto-Like Strava Posts for TamperMonkey userscripts
// ==UserScript==
// @name Auto-Like Strava Posts
// @namespace http://tampermonkey.net/
// @version 0.2.0
// @description Like all the posts!
// @author James W Mann
// @match https://www.strava.com/dashboard
// @icon https://www.google.com/s2/favicons?sz=64&domain=strava.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
const debug = true;
const log = (...params) => debug ? console.log(...params) : null;
log('Auto-Like Strava Posts Script Initialized.');
const YourStravaFullName = 'James Mann'
const pageLoadWait = 1000 // Amount of time in milliseconds to wait until the page is loaded
const waitLimit = 60; // 30 Seconds timeout for how long to wait for loading more posts
const emptyLimit = 1; // How many times it double checks for any more unliked posts by loading more
const container = undefined; // Normally this should be left undefined unless the site scrolls within a container/frame
const heightPadding = 50; // Amount of pixels to accomodate for when scrolling down
const waitTick = 500; // Amount of time in milliseconds to wait to check if posts have loaded yet
let emptyAmount = 0;
let postsAmount = 0
const getScrollHeight = () => {
const scrollHeight = container ? container.scrollHeight : document.body.scrollHeight;
log(`Getting a scroll height of: ${scrollHeight}px`, { container });
return scrollHeight;
}
const scrollToY = (y = 0) => new Promise( (r, j) => {
log(`Attempting to scroll to: ${y}px position on the page.`, { container });
if (container) container.scrollTo(0, y);
else window.scrollTo(0, y);
log('Waiting for scroll to complete...');
setTimeout(() => r(log('Scrolling complete.')), waitTick || 500);
});
const getPosts = () => document.querySelectorAll('main#main .feed-ui > div');
const clickPosts = async () => {
let unclickedPosts = 0;
const posts = getPosts();
postsAmount = posts.length;
log(`${postsAmount} posts currently found.`);
posts.forEach( el => {
const author = el.querySelector('[data-testid="owners-name"]')?.innerText;
if (author && author !== YourStravaFullName) {
const likeSvg = el.querySelector('[data-testid="unfilled_kudos"]');
if (likeSvg) {
unclickedPosts++;
const likeBtn = likeSvg.parentElement;
likeBtn.click();
}
}
});
if (unclickedPosts === 0) {
if (emptyAmount < emptyLimit) {
emptyAmount++;
log(`All posts already liked. Loading more posts to double check... ${emptyAmount} out of ${emptyLimit} times`);
await loadMorePosts();
} else {
log('Done Liking');
await scrollToY(); // Scroll to the top of the page
}
} else await loadMorePosts();
}
const loadMorePosts = async () => {
let waitAmount = 0;
log(`Loading more posts...`);
await scrollToY(getScrollHeight() + (heightPadding || 0)); // Scroll to the bottom of the page
const checkPosts = () => setTimeout(() => {
if (waitAmount < waitLimit) waitAmount++;
else {
log('Waiting too long, halting...');
return;
}
const newPostsAmount = getPosts().length;
if (postsAmount === newPostsAmount) checkPosts();
else clickPosts();
}, waitTick || 500);
checkPosts();
};
setTimeout(clickPosts, pageLoadWait || 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment