Skip to content

Instantly share code, notes, and snippets.

@programming-with-ia
Created November 2, 2025 20:54
Show Gist options
  • Select an option

  • Save programming-with-ia/083cec38b6a1ebadec870a7a6663d152 to your computer and use it in GitHub Desktop.

Select an option

Save programming-with-ia/083cec38b6a1ebadec870a7a6663d152 to your computer and use it in GitHub Desktop.
/**
* Helper function to wait for an element to appear or disappear.
* @param {string} selector - The CSS selector to query.
* @param {boolean} [shouldExist=true] - If true, waits for the element to exist. If false, waits for it to not exist.
* @param {number} [timeout=3000] - Max time to wait in milliseconds.
* @returns {Promise<Element|null>} A promise that resolves with the element (if found) or null (if timed out or disappearing).
*/
async function waitForElement(selector, shouldExist = true, timeout = 3000) {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const element = document.querySelector(selector);
// Case 1: We want the element, and we found it.
if (shouldExist && element) {
return element;
}
// Case 2: We want the element to disappear, and it's gone.
if (!shouldExist && !element) {
return null; // Success, it's gone.
}
// Wait 100ms before polling again
await new Promise(r => setTimeout(r, 100));
}
// If we're here, it timed out.
console.error(`Timeout: Waited ${timeout / 1000}s for element '${selector}' to ${shouldExist ? 'appear' : 'disappear'}.`);
return null;
}
/**
* Main function to find and delete all memory items.
*/
async function deleteAllMemories() {
const optionsButtons = document.querySelectorAll('tr.memory-row button.cdk-menu-trigger');
console.log(`Found ${optionsButtons.length} rows to delete.`);
if (optionsButtons.length === 0) {
console.log("No rows found. Make sure you are on the correct page.");
return;
}
for (const [index, button] of optionsButtons.entries()) {
console.log(`Processing item ${index + 1} of ${optionsButtons.length}...`);
// 1. Click the '...' button (opens the menu)
button.click();
// 2. Wait for the menu 'Delete' button to appear, then click it
const menuDeleteIcon = await waitForElement('mat-icon[svgicon="delete"]');
if (!menuDeleteIcon) {
console.error("Stopping script: Could not find menu 'Delete' icon.");
break;
}
const menuDeleteButton = menuDeleteIcon.closest('button');
if (!menuDeleteButton) {
console.error("Stopping script: Found menu 'Delete' icon but not its button.");
break;
}
menuDeleteButton.click();
// 3. Wait for the dialog's 'Delete' button to appear, then click it
const dialogDeleteButton = await waitForElement('div.dialog button.delete-button');
if (!dialogDeleteButton) {
console.error("Stopping script: Could not find dialog 'Delete' button.");
break;
}
dialogDeleteButton.click();
// 4. Wait for the confirmation dialog to CLOSE
console.log("Waiting for dialog to close...");
await waitForElement('div.dialog', false); // Wait for it to disappear
// Add a tiny buffer for the UI to settle before the next loop
await new Promise(r => setTimeout(r, 250));
}
console.log('Finished deleting all items.');
}
// Run the function
deleteAllMemories();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment