Skip to content

Instantly share code, notes, and snippets.

@nazariyv
Created August 12, 2024 20:14
Show Gist options
  • Save nazariyv/ad61f944ba730e56bb9b286ab0d01556 to your computer and use it in GitHub Desktop.
Save nazariyv/ad61f944ba730e56bb9b286ab0d01556 to your computer and use it in GitHub Desktop.
mass unlike twitter browser script
// Mass Unlikes All Your Likes on a given date
// 1. open your likes tab
// 2. input the dates for which you'd like to unlike tweets
// 3. drop this into browser console and hit Enter
(function() {
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const targetDates = ['Aug 8', 'Aug 9', 'Aug 10', 'Aug 11']; // Add or remove dates as needed
function isElementInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
async function unlikeTargetDateTweets() {
let processedTweets = new Set();
let unlikedCount = 0;
let noNewTweetsCount = 0;
while (noNewTweetsCount < 3) { // Stop if we don't find new tweets after 3 attempts
const tweets = document.querySelectorAll('article[data-testid="tweet"]');
let newTweetsFound = false;
for (const tweet of tweets) {
if (processedTweets.has(tweet)) continue;
if (!isElementInViewport(tweet)) continue;
newTweetsFound = true;
processedTweets.add(tweet);
// Scroll the tweet into view
tweet.scrollIntoView({ behavior: 'smooth', block: 'center' });
// Wait for any lazy-loaded content
await delay(500);
// Check if the date is in the target dates
const dateElement = tweet.querySelector('time');
if (dateElement && targetDates.some(date => dateElement.textContent.includes(date))) {
// Find the interaction group
const interactionGroup = tweet.querySelector('div[role="group"]');
if (interactionGroup) {
// Find the third div in the interaction group
const divs = interactionGroup.querySelectorAll(':scope > div');
if (divs.length >= 3) {
const likeButtonContainer = divs[2];
const likeButton = likeButtonContainer.querySelector('button');
if (likeButton) {
likeButton.click();
unlikedCount++;
console.log(`Unliked tweet #${unlikedCount} from target dates`);
} else {
console.log('Like button not found within the container');
}
} else {
console.log('Not enough divs found in the interaction group');
}
} else {
console.log('Interaction group not found for target date tweet');
}
}
// Wait for 1 second before moving to the next tweet
await delay(1000);
}
if (!newTweetsFound) {
noNewTweetsCount++;
console.log(`No new tweets found. Attempt ${noNewTweetsCount} of 3`);
} else {
noNewTweetsCount = 0;
}
// Scroll to load more tweets
window.scrollBy(0, window.innerHeight / 2);
await delay(2000); // Wait for new tweets to load
}
console.log(`Finished processing tweets. Unliked ${unlikedCount} tweets from target dates.`);
}
unlikeTargetDateTweets();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment