Skip to content

Instantly share code, notes, and snippets.

@lmj0011
Created July 25, 2019 09:31
Show Gist options
  • Select an option

  • Save lmj0011/0e2d8ff47e4171a562ebfb546fbde6e6 to your computer and use it in GitHub Desktop.

Select an option

Save lmj0011/0e2d8ff47e4171a562ebfb546fbde6e6 to your computer and use it in GitHub Desktop.

Revisions

  1. lmj0011 created this gist Jul 25, 2019.
    127 changes: 127 additions & 0 deletions textnow-conversation-purger.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    (async function () {
    let lastGlobalId;
    let fetchUrl;
    let bigOleArrayOfContacts = [];

    const USERNAME_QUERY_SELECTOR = "#recent-header > div.account-details > div.name";
    const CSRF_TOKEN_QUERY_SELECTOR = "meta[name=csrf-token]";

    console.log("Script has started.");

    if (!document.querySelector(USERNAME_QUERY_SELECTOR)) {
    throw Error("was unable to find the username on this HTML page")
    }

    if (!document.querySelector(CSRF_TOKEN_QUERY_SELECTOR)) {
    throw Error("was unable to find the csrf-token on this HTML page")
    }

    // update these options, as needed, before running this script
    const opts = {
    TEXTNOW_USERNAME: document.querySelector(USERNAME_QUERY_SELECTOR).innerText,

    // perseve conversations that you have given a name to from use of the "Edit Contact Name" option
    DELETE_NAMED_CONVERSATIONS: true,

    // should not need to change this
    CSRF_TOKEN: document.querySelector(CSRF_TOKEN_QUERY_SELECTOR).content,
    }


    console.log("Gathering contacts.\n\n");

    do {

    if(!lastGlobalId) {
    fetchUrl = 'https://www.textnow.com/api/v3/contacts?page_size=100';
    } else {
    fetchUrl = `https://www.textnow.com/api/v3/contacts?page_size=100&global_id_start=${lastGlobalId}`;
    }

    try {
    await fetch(fetchUrl,
    {"credentials":"include",
    "headers":{
    "accept":"application/json, text/javascript, */*;q=0.01",
    "accept-language":"en-US,en;q=0.9",
    "cache-control":"no-cache",
    "pragma":"no-cache",
    "x-requested-with":"XMLHttpRequest"
    },
    "referrer":"https://www.textnow.com/messaging",
    "referrerPolicy":"no-referrer-when-downgrade",
    "body": null,
    "method":"GET",
    "mode":"cors"
    })
    .then(res => res.json())
    .then(data => {
    if(data.result.length) {
    bigOleArrayOfContacts = bigOleArrayOfContacts.concat(data.result);
    lastGlobalId = data.result[(data.result.length - 1)].global_id;
    } else {
    lastGlobalId = null
    }
    })
    } catch(e) {
    console.log(e);
    }

    } while (lastGlobalId);


    /////////////////////////

    const contactsToDelete = bigOleArrayOfContacts.filter(contact => {
    if (!opts.DELETE_NAMED_CONVERSATIONS && (contact.name && contact.name.match(/[a-zA-Z]+/g))){
    return false;
    } else {
    return true;
    }
    });

    console.log(`Preparing to delete ${contactsToDelete.length} conversations.\n\n`);

    for (const contact of contactsToDelete) {

    let number;
    if(contact.contact_value[0] === "+") {
    number = contact.contact_value.slice(1)
    } else {
    number = contact.contact_value
    }

    try {
    await fetch(`https://www.textnow.com/api/users/${opts.TEXTNOW_USERNAME}/conversations/%2B${number}`,
    {"credentials":"include",
    "headers":{
    "accept":"application/json, text/javascript, */*; q=0.01",
    "accept-language":"en-US,en;q=0.9",
    "cache-control":"no-cache",
    "pragma":"no-cache",
    "x-csrf-token": opts.CSRF_TOKEN,
    "x-requested-with":"XMLHttpRequest"
    },
    "referrer":"https://www.textnow.com/messaging",
    "referrerPolicy":"no-referrer-when-downgrade",
    "body":null,
    "method":"DELETE",
    "mode":"cors"
    });

    // add a delay to avoid spamming server
    await new Promise(resolve => {
    setTimeout(() => { resolve() }, 1000);
    });

    console.log("deleted! ", contact);

    } catch (e) {
    console.log(e);
    }

    }

    console.log("DONE!");

    })();