Skip to content

Instantly share code, notes, and snippets.

@ryanml
Created April 12, 2020 03:21
Show Gist options
  • Save ryanml/d1eb8f609469bc24f017fcf4fc81b1d6 to your computer and use it in GitHub Desktop.
Save ryanml/d1eb8f609469bc24f017fcf4fc81b1d6 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Instagram Delete All
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds an "Unsend All" button to a DM thread view.
// @author ryanml && christopher
// @match https://www.instagram.com/direct/inbox/
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict'
let urlInterval
let threadId = ''
const itemIds = []
const THREADS_ENDPOINT = 'https://www.instagram.com/direct_v2/web/threads/'
const webRequest = (url, method, callback) => {
const reqConfig = {
method: method,
url: url,
onload: callback
}
GM_xmlhttpRequest(reqConfig)
}
const fetchInitialData = () => {
const pathName = window.location.pathname
threadId = pathName.split('/direct/t/')[1]
const reqUrl = `${THREADS_ENDPOINT}${threadId}`
webRequest(reqUrl, 'GET', (response) => {
const respObj = JSON.parse(response.responseText)
respObj.thread.items.map((item) => {
itemIds.push(item['item_id'])
})
addUnsendAllButton()
})
}
const deleteAllMessages = () => {
itemIds.map((itemId) => {
const reqUrl = `https://www.instagram.com/direct_v2/web/threads/${threadId}/items/${itemId}/delete/`
webRequest(reqUrl, 'POST', (response) => {
console.log(`Deleted post: ${itemId}`)
})
})
}
const addUnsendAllButton = () => {
const unsendAllButton = document.createElement('button')
unsendAllButton.innerHTML = "Unsend All Messages"
unsendAllButton.addEventListener('click', deleteAllMessages)
document.body.appendChild(unsendAllButton)
}
window.onload = () => {
urlInterval = setInterval(() => {
const pathName = window.location.pathname
if (pathName.indexOf('/direct/t/') > -1) {
clearInterval(urlInterval)
fetchInitialData()
}
}, 500);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment