Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Instagram Comment Auto-Deleter
- // @namespace http://tampermonkey.net/
- // @version 1.0
- // @description Automatically deletes all Instagram comments
- // @author you
- // @match https://www.instagram.com/your_activity/interactions/comments*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- const BATCH_SIZE = 6;
- const PAUSE_BETWEEN_BATCHES = 15000;
- const MAX_BATCHES = 10000;
- const STUCK_TIMEOUT = 20000;
- const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
- const saveProgress = (batchCount, totalDeleted) => {
- sessionStorage.setItem('ig_delete_running', 'true');
- sessionStorage.setItem('ig_delete_batch', batchCount);
- sessionStorage.setItem('ig_delete_total', totalDeleted);
- };
- const loadProgress = () => {
- return {
- running: sessionStorage.getItem('ig_delete_running') === 'true',
- batchCount: parseInt(sessionStorage.getItem('ig_delete_batch') || '0'),
- totalDeleted: parseInt(sessionStorage.getItem('ig_delete_total') || '0')
- };
- };
- const clearProgress = () => {
- sessionStorage.removeItem('ig_delete_running');
- sessionStorage.removeItem('ig_delete_batch');
- sessionStorage.removeItem('ig_delete_total');
- };
- const humanClick = (el) => {
- const rect = el.getBoundingClientRect();
- const x = rect.left + rect.width / 2;
- const y = rect.top + rect.height / 2;
- const eventOptions = {
- bubbles: true,
- cancelable: true,
- view: window,
- clientX: x,
- clientY: y,
- screenX: x,
- screenY: y,
- buttons: 1
- };
- el.dispatchEvent(new MouseEvent('mouseover', eventOptions));
- el.dispatchEvent(new MouseEvent('mouseenter', eventOptions));
- el.dispatchEvent(new MouseEvent('mousemove', eventOptions));
- el.dispatchEvent(new MouseEvent('mousedown', eventOptions));
- el.dispatchEvent(new MouseEvent('mouseup', eventOptions));
- el.dispatchEvent(new MouseEvent('click', eventOptions));
- };
- const findSelectButton = () => {
- return Array.from(document.querySelectorAll('[data-bloks-name="bk.components.Flexbox"]'))
- .find(el => {
- const style = el.getAttribute('style') || '';
- return el.textContent.trim() === 'Select' && style.includes('cursor: pointer');
- });
- };
- const findDeleteButton = () => {
- return Array.from(document.querySelectorAll('[data-bloks-name="bk.components.Flexbox"]'))
- .find(el => {
- const style = el.getAttribute('style') || '';
- return el.textContent.trim() === 'Delete' && style.includes('cursor: pointer');
- });
- };
- const findConfirmDeleteButton = () => {
- return Array.from(document.querySelectorAll('button'))
- .find(el => el.textContent.trim() === 'Delete');
- };
- const checkForErrorPopup = () => {
- const okBtn = Array.from(document.querySelectorAll('button'))
- .find(el => el.textContent.trim() === 'OK');
- if (okBtn) {
- console.log('⚠️ Error popup detected! Clicking OK and continuing...');
- humanClick(okBtn);
- return true;
- }
- return false;
- };
- const waitForSelectButton = async () => {
- const startTime = Date.now();
- console.log('Waiting for Select button...');
- while (true) {
- if (checkForErrorPopup()) {
- await sleep(3000);
- }
- const selectBtn = findSelectButton();
- if (selectBtn) return selectBtn;
- if (Date.now() - startTime > STUCK_TIMEOUT) {
- console.log('⚠️ Page stuck! Saving progress and refreshing...');
- return null;
- }
- await sleep(1000);
- }
- };
- const getCommentRows = () => {
- return Array.from(document.querySelectorAll('[role="button"][data-bloks-name="bk.components.Flexbox"]'))
- .filter(el => {
- const style = el.getAttribute('style') || '';
- const text = el.textContent.trim();
- return style.includes('flex-grow: 1') &&
- style.includes('cursor: pointer') &&
- text.length > 0;
- });
- };
- async function deleteLoop() {
- // Wait for page to fully load before doing anything
- await sleep(5000);
- const progress = loadProgress();
- let batchCount = progress.batchCount;
- let totalDeleted = progress.totalDeleted;
- if (progress.running) {
- console.log(`♻️ Resuming after page refresh — Batch ${batchCount + 1}, ${totalDeleted} deleted so far`);
- } else {
- console.log('🚀 Instagram Comment Deleter starting...');
- }
- while (batchCount < MAX_BATCHES) {
- console.log(`\n--- Batch ${batchCount + 1} ---`);
- if (checkForErrorPopup()) {
- await sleep(3000);
- }
- const selectBtn = await waitForSelectButton();
- if (!selectBtn) {
- saveProgress(batchCount, totalDeleted);
- console.log('Refreshing page, will auto-resume...');
- await sleep(1000);
- window.location.reload();
- return;
- }
- humanClick(selectBtn);
- console.log('Clicked Select, waiting for UI...');
- await sleep(1500);
- if (checkForErrorPopup()) {
- await sleep(3000);
- continue;
- }
- const rows = getCommentRows();
- console.log(`Found ${rows.length} comment rows in DOM`);
- if (rows.length === 0) {
- console.log('No comment rows found. Done.');
- clearProgress();
- break;
- }
- let selectedCount = 0;
- for (let row of rows) {
- if (selectedCount >= BATCH_SIZE) break;
- humanClick(row);
- selectedCount++;
- await sleep(300);
- }
- console.log(`Selected ${selectedCount} comments, clicking Delete...`);
- await sleep(1000);
- if (checkForErrorPopup()) {
- await sleep(3000);
- continue;
- }
- const deleteBtn = findDeleteButton();
- if (!deleteBtn) {
- console.log('Delete button not found. Stopping.');
- clearProgress();
- break;
- }
- humanClick(deleteBtn);
- console.log('Clicked Delete, waiting for confirmation popup...');
- await sleep(3000);
- if (checkForErrorPopup()) {
- await sleep(3000);
- continue;
- }
- let confirmed = false;
- for (let attempt = 0; attempt < 5; attempt++) {
- if (checkForErrorPopup()) {
- await sleep(3000);
- confirmed = true;
- break;
- }
- const confirmBtn = findConfirmDeleteButton();
- if (confirmBtn) {
- console.log(`Found native Delete button, clicking... (attempt ${attempt + 1})`);
- humanClick(confirmBtn);
- await sleep(2000);
- if (checkForErrorPopup()) {
- await sleep(3000);
- confirmed = true;
- break;
- }
- const stillThere = findConfirmDeleteButton();
- if (!stillThere) {
- console.log('Popup dismissed successfully!');
- confirmed = true;
- break;
- } else {
- console.log('Popup still visible, retrying...');
- }
- } else {
- console.log(`Confirmation button not found yet, retrying (${attempt + 1}/5)...`);
- await sleep(1000);
- }
- }
- if (!confirmed) {
- console.log('Could not dismiss confirmation popup. Stopping.');
- clearProgress();
- break;
- }
- totalDeleted += selectedCount;
- batchCount++;
- console.log(`✓ Batch ${batchCount} done (${totalDeleted} total deleted). Pausing ${PAUSE_BETWEEN_BATCHES/1000}s...`);
- await sleep(PAUSE_BETWEEN_BATCHES);
- }
- console.log(`\n✅ Finished! Batches: ${batchCount}, Total deleted: ${totalDeleted}`);
- clearProgress();
- }
- deleteLoop();
- })();
Advertisement
Add Comment
Please, Sign In to add comment