Skip to content

Instantly share code, notes, and snippets.

@ManuLinares
Created August 22, 2025 21:38
Show Gist options
  • Select an option

  • Save ManuLinares/77a160bb63956841b57a41ad268ae268 to your computer and use it in GitHub Desktop.

Select an option

Save ManuLinares/77a160bb63956841b57a41ad268ae268 to your computer and use it in GitHub Desktop.
const allATags = document.querySelectorAll('a');
const matchingATags = [];
allATags.forEach((aTag) => {
const href = aTag.getAttribute('href');
if (href && href.startsWith('/messages/t/')) {
matchingATags.push(aTag);
}
});
function findParentWithRoleRow(element) {
while (element && element.getAttribute('role') !== 'row') {
element = element.parentElement;
}
return element;
}
function openMenuFromRow(parentRow){
const gridCells = parentRow.querySelectorAll('div[role="gridcell"]');
const secondGridCell = gridCells[1];
const button = secondGridCell.querySelector("div[role=button]");
setTimeout(()=>{button.click(); console.log("Menu opened")}, 500)
}
function waitForMenu(retries = 10, delay = 300) {
return new Promise((resolve, reject) => {
const check = (attempt = 0) => {
let menu = document.querySelector("div[role=menu]");
if (menu) return resolve(menu);
if (attempt >= retries) return reject("Menu not found");
setTimeout(() => check(attempt + 1), delay);
};
check();
});
}
async function findMenuClickDelete() {
try {
let menu = await waitForMenu();
console.log("menu found.");
let menuItems = menu.querySelectorAll("div[role=menuitem]");
console.log("Clicking delete option");
for (let i = 0; i < menuItems.length; i++) {
let parentMenuItem = menuItems[i];
let spanWithinParent = parentMenuItem.querySelector("span");
if (spanWithinParent && spanWithinParent.textContent.trim() === "Delete chat") {
parentMenuItem.click();
break;
}
}
setTimeout(() => {
console.log("Confirming Delete");
let delDialogue = document.querySelectorAll('[aria-label="Delete chat"]');
if (delDialogue[2]) {
delDialogue[2].click();
} else {
console.warn("Delete confirmation not found.");
}
}, 700);
} catch (e) {
console.error(e);
}
}
async function processMatchingATags() {
for (let index = 0; index < matchingATags.length; index++) {
const aTag = matchingATags[index];
const delay = 1500;
await new Promise((resolve) => setTimeout(resolve, delay));
console.log(delay, "ms delay");
console.log("processing index", index ,"/", matchingATags.length);
let parentRow = findParentWithRoleRow(aTag);
await new Promise((resolve) => setTimeout(resolve, 1000));
openMenuFromRow(parentRow);
await new Promise((resolve) => setTimeout(resolve, 750));
findMenuClickDelete();
}
}
processMatchingATags();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment