Search code examples
javascriptdomsettimeoutconsole.log

Deleting tweets with JS console


I am trying to delete tweets in the JS console on my Twitter page. If I do it manually by selecting the button and then doing a click, I can manually run 3 different functions.

const delButton=()=>{
    document.querySelectorAll('[data-testid="caret"]')[0].click()
};
const clickDel=()=>{
    document.querySelector("#layers > div.css-1dbjc4n.r-1d2f490.r-105ug2t.r-u8s1d.r-zchlnj.r-ipm5af > div > div > div > div:nth-child(2) > div.css-1dbjc4n.r-yfoy6g.r-z2wwpe.r-xnswec.r-1ekmkwe.r-1udh08x.r-u8s1d > div > div > div > div:nth-child(1) > div.css-1dbjc4n.r-16y2uox.r-1wbh5a2 > div > span");
};
const confirmDel=()=>{
    document.querySelector("#layers > div:nth-child(2) > div > div > div > div > div > div.css-1dbjc4n.r-1awozwy.r-1kihuf0.r-18u37iz.r-1pi2tsx.r-1777fci.r-1pjcn9w.r-fxte16.r-1xcajam.r-ipm5af.r-9dcw1g > div.css-1dbjc4n.r-1awozwy.r-yfoy6g.r-1867qdf.r-1jgb5lz.r-pm9dpa.r-1ye8kvj.r-1rnoaur.r-d9fdf6.r-1sxzll1.r-13qz1uu > div.css-1dbjc4n.r-18u37iz.r-13qz1uu > div.css-18t94o4.css-1dbjc4n.r-1dgebii.r-42olwf.r-sdzlij.r-1phboty.r-rs99b7.r-16y2uox.r-1w2pmg.r-1vuscfd.r-1dhvaqw.r-1ny4l3l.r-1fneopy.r-o7ynqc.r-6416eg.r-lrvibr > div > span > span").click()
} 

delButton();
clickDel();
confirmDel();

I have tried a few different ways, if I run them manually by running each function in the console, it works. However when i try to put them into a function that runs all 3, the second one doesn't click because it isn't ready yet. I tried using a setTimeout before firing them but it still hasn't worked. I also tried using async and await but I think my understanding of the problem is limited.

const runEmAll=()=>{
    setTimeout(delButton(), 1000); 
    setTimeout(clickDel(), 1000);
    setTimeout(confirmDel(), 1000);
}
runEmAll();

// or I also tried...
const runDel = async ()=>{
    await delButton();
}
runDel().then(clickDel());

Again that didn't work.. The error I get is

VM1649:2 Uncaught TypeError: Cannot read property 'click' of null

referring to the clickDel function


Solution

  • . open Chrome or whatever browser.

    (click order and buttons may different vary if browser is not Chrome).

    . open twitter.

    . click profile or click tweets and replies

    . press F12 (developer options)

    . click Console Tab

    . paste this codes (twitter language must be English)

    var delTweets = function () {
    var tweetsRemaining = 
    document.querySelectorAll('[role="heading"]+div')[1].textContent;
    console.log('Remaining: ', tweetsRemaining);
    window.scrollBy(0, 10000);
    document.querySelectorAll('[aria-label="More"]').forEach(function 
    (v, i, a) {
        v.click();
        document.querySelectorAll('span').forEach(function (v2, i2, a2) {
            if (v2.textContent === 'Delete') {
                v2.click();
                document.querySelectorAll('[data-testid="confirmationSheetConfirm"]').forEach(function (v3, i3, a3) {
                    v3.click();
                });
            }
            else {
                document.body.click();
            }
        });
    });
    setTimeout(delTweets, 4000); //less than 4000 might be rate limited or account suspended. increase timeout if any suspend or rate limit happens
    }
    delTweets();