Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (async function deleteOldReplies() {
- const ONE_YEAR_AGO = new Date();
- ONE_YEAR_AGO.setFullYear(ONE_YEAR_AGO.getFullYear() - 1);
- const MORE = "Mais"; // Aria label of the three-dots menu button at each post
- const DELETE = "Excluir"; // Text of the delete buttons
- const REPLYING = 'Respondendo'; // Text marking every post that is a reply
- console.log(`🟢 Script started. Deleting replies older than ${ONE_YEAR_AGO.toISOString().split('T')[0]}...`);
- function wait(ms) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- async function deleteReply(replyElement) {
- try {
- const timeElement = replyElement.querySelector('time');
- if (!timeElement || !timeElement.hasAttribute("datetime")) {
- console.warn("⚠️ Could not find time element for a reply. Skipping deletion.");
- return 0;
- }
- const postDate = new Date(timeElement.getAttribute("datetime"));
- console.log(`🟡 Attempting to delete reply from ${postDate.toISOString().split('T')[0]}...`);
- // Find the "More options" button (which is a div with a title inside)
- const optionsSvg = replyElement.querySelector(`svg[aria-label="${MORE}"]`);
- if (!optionsSvg) {
- console.warn("⚠️ 'More options' button (div with title 'Mais') not found. Skipping.");
- return 0;
- }
- optionsSvg.parentNode.click();
- await wait(5000);
- const spansDel = [...document.querySelectorAll('span')];
- const deleteButton = spansDel.find(btn => btn.textContent.includes(DELETE));
- if (!deleteButton) {
- console.warn("⚠️ Delete button not found. Skipping.");
- return 0;
- }
- deleteButton.parentNode.click();
- await wait(5000);
- const confirmButton = [...document.querySelectorAll('span')].find(btn => btn.textContent.includes(DELETE)).parentNode;
- if (!confirmButton) {
- console.warn("⚠️ Confirm button not found. Skipping.");
- return 0;
- }
- confirmButton.click();
- await wait(2000);
- console.log("✅ Reply deleted successfully.");
- return 1;
- } catch (error) {
- console.error("❌ Error deleting a reply:", error);
- }
- return 0;
- }
- async function processReplies() {
- let deleted = 0;
- let cycle = 0;
- console.log(`🔄 Scanning for replies...`);
- const times = [...document.querySelectorAll('time')];
- console.log(`🟠 Found ${times.length} time elements.`);
- const posts = times.map(time => {
- let ancestor = time.parentNode;
- while (ancestor) {
- if (ancestor.querySelector(`svg[aria-label="${MORE}"]`)) {
- return ancestor;
- }
- ancestor = ancestor.parentNode;
- }
- console.warn("⚠️ No ancestor with a <time> tag found. Skipping.");
- return null;
- }).filter(Boolean); // Remove null values
- console.log(`🔍 Found ${posts.length} reply posts.`);
- if (posts.length === 0) {
- console.log("🚫 No more posts found. Exiting loop.");
- return;
- }
- let checked = 0;
- for (const post of posts) {
- const timeElement = post.querySelector('time');
- if (!timeElement || !timeElement.hasAttribute("datetime")) {
- console.warn("⚠️ Post has no valid time element. Skipping.");
- continue;
- }
- const postDate = new Date(timeElement.getAttribute("datetime"));
- checked++;
- if (postDate >= ONE_YEAR_AGO) {
- console.log(`➡️ Skipping reply from ${postDate.toISOString().split('T')[0]} (less than a year old).`);
- continue;
- }
- deleted += await deleteReply(post);
- console.log(`✅ Deleted: ${deleted}`);
- }
- console.log(`📊 Checked ${checked} replies in this cycle. Deleted so far: ${deleted}.`);
- // Scroll down to load more replies
- // window.scrollTo(0, document.body.scrollHeight);
- // console.log("⬇️ Scrolling down to load more replies...");
- await wait(2000);
- console.log(`🏁 Finished! Total replies deleted: ${deleted}.`);
- }
- processReplies();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement