Great catch! This is a common issue with Material-UI buttons: They often replace the button element in the DOM when clicked, so the event listener you attached to the original button is lost after the first click.
Solution: Use Event Delegation or a Mutation Observer
Option 1: Use Event Delegation (Recommended for Simplicity)
Attach a click listener to the document, and check if the click target matches your button.
This way, even if the button is replaced, the listener still works.
javascript
(function() {
// Helper to get the table
function getTable() {
return document.querySelector('.table_table__8D7gp');
}
// Helper to check if the clicked element is your button
function isOurButton(el) {
return el && el.classList && el.classList.contains('MuiButton-containedSecondary');
}
// Download function
function downloadTableAsHtml() {
const table = getTable();
if (!table) return;
const tableHtml = table.outerHTML;
const blob = new Blob([tableHtml], { type: 'text/html' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `table_${new Date().toISOString().replace(/:/g, '-').split('.')[0]}.html`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
}
// Listen for clicks on the whole document
function handleClick(e) {
// If the clicked element or its parent is the button
let el = e.target;
while (el && el !== document.body) {
if (isOurButton(el)) {
// Wait for table to update, then download
setTimeout(downloadTableAsHtml, 200);
break;
}
el = el.parentElement;
}
}
document.addEventListener('click', handleClick);
// Download the initial table
downloadTableAsHtml();
// Return a stop function
return function stopScript() {
document.removeEventListener('click', handleClick);
console.log('Table download script stopped.');
};
})();
Option 2: Mutation Observer (Advanced, if needed)
If the above doesn't work (e.g., the button is deeply re-rendered), you can use a MutationObserver to re-attach the event listener every time the button is replaced.
But for most Material-UI setups, Option 1 is simpler and works well.
How to Use
Paste the code above into your browser console.
Every time you click the "next" button, the table will be downloaded as an HTML file.
If your button's class changes, adjust the isOurButton function to match a unique class or attribute.
Let me know if you need it to match a different selector!