Created
March 12, 2024 19:48
-
-
Save bstee615/1f4aa8a6d63dc74aff53dac17d287d91 to your computer and use it in GitHub Desktop.
JavaScript for Twitter Post Limiter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Twitter Post Limiter | |
// @namespace https://github.com/bstee615/ | |
// @version 0.2 | |
// @match https://twitter.com/home* | |
// @grant none | |
// @author Benjamin Steenhoek | |
// @description Number posts on the Twitter timeline and limit after a set number. | |
// | |
// 3/12/2024, 2:23 PM created | |
// 3/12/2024, 2:46 PM added "limit" feature to blur out posts after X number of posts | |
// ==/UserScript== | |
// Written (mostly) by https://claude.ai/chat/8ce991db-cf66-41ed-8f4f-d63d70f0513c | |
(function () { | |
const postIndexMap = new Map(); | |
let currentIndex = 1; | |
function numberPosts() { | |
const posts = document.querySelectorAll('article[data-testid="tweet"]:not([data-numbered])'); | |
posts.forEach(post => { | |
const postId = post.innerText; // or any other unique identifier | |
if (!postIndexMap.has(postId)) { | |
postIndexMap.set(postId, currentIndex); | |
currentIndex++; | |
} | |
const containerDiv = document.createElement('div'); | |
const numberDiv = document.createElement('div'); | |
const postIndex = postIndexMap.get(postId); | |
const inTheRed = postIndex > 5; | |
containerDiv.style.position = 'absolute'; | |
containerDiv.style.top = '10px'; | |
containerDiv.style.left = '10px'; | |
containerDiv.style.display = 'flex'; | |
containerDiv.style.justifyContent = 'center'; | |
containerDiv.style.alignItems = 'center'; | |
containerDiv.style.flexDirection = 'row'; | |
containerDiv.style.zIndex = '1'; | |
containerDiv.style.padding = '5px'; | |
containerDiv.style.borderRadius = '15px'; | |
if (inTheRed) { | |
containerDiv.style.width = (post.offsetWidth-20)+'px'; | |
containerDiv.style.height = (post.offsetHeight-20)+'px'; | |
containerDiv.style.backdropFilter = 'blur(10px)'; | |
containerDiv.style.fontSize = '64px'; | |
containerDiv.style.backgroundColor = 'rgba(255, 0, 0, 0.5)'; | |
} | |
else { | |
containerDiv.style.width = '15px'; | |
containerDiv.style.height = '15px'; | |
containerDiv.style.fontSize = '14px'; | |
containerDiv.style.backgroundColor = 'rgba(0, 0, 0, 0.5)'; | |
} | |
numberDiv.textContent = inTheRed ? `${postIndex} (past your limit!)` : postIndex; | |
numberDiv.style.color = 'white'; | |
numberDiv.style.fontWeight = 'bold'; | |
numberDiv.style.textAlign = 'center'; | |
// post.style.position = 'relative'; | |
containerDiv.prepend(numberDiv); | |
post.prepend(containerDiv); | |
post.dataset.numbered = true; // Mark the post as numbered | |
}); | |
} | |
// Initial call to number the posts | |
window.addEventListener('load', function() { | |
let interval = 0; | |
interval = setInterval(function() { | |
if (document.querySelector('article[data-testid="tweet"]')) { | |
clearInterval(interval); | |
numberPosts(); | |
} | |
}, 10); | |
}) | |
// Listen for scroll events | |
window.addEventListener('scroll', numberPosts); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to use it by importing it into ViolentMonkey. No warranty, run at your own risk :)
Here's what it looks like
Future plans/roadmap: