// Function to create a mutation observer that removes a specific div when it appears
function createDivRemover(targetSelector, callback = null) {
// Create the mutation observer
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
// Check if nodes were added
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach((node) => {
// Check if the added node is an element
if (node.nodeType === Node.ELEMENT_NODE) {
// Check if the node matches our target selector
if (node.matches && node.matches(targetSelector)) {
console.log('Target div found:', node);
// Execute callback if provided
if (callback && typeof callback === 'function') {
callback(node);
}
// Remove the element
node.remove();
console.log('Target div removed');
}
// Also check children of the added node
const targetElements = node.querySelectorAll ? node.querySelectorAll(targetSelector) : [];
targetElements.forEach((targetElement) => {
console.log('Target div found in children:', targetElement);
// Execute callback if provided
if (callback && typeof callback === 'function') {
callback(targetElement);
}
// Remove the element
targetElement.remove();
console.log('Target div removed from children');
});
}
});
}
});
});
// Start observing
observer.observe(document.body, {
childList: true,
subtree: true
});
// Return the observer so it can be disconnected if needed
return observer;
}
// Function to remove existing elements that match the selector
function removeExistingElements(targetSelector, callback = null) {
const existingElements = document.querySelectorAll(targetSelector);
existingElements.forEach((element) => {
console.log('Existing target div found:', element);
// Execute callback if provided
if (callback && typeof callback === 'function') {
callback(element);
}
// Remove the element
element.remove();
console.log('Existing target div removed');
});
}
// Main function to set up the div remover
function setupDivRemover(targetSelector, callback = null, removeExisting = true) {
// Remove existing elements if requested
if (removeExisting) {
removeExistingElements(targetSelector, callback);
}
// Set up the mutation observer for future elements
const observer = createDivRemover(targetSelector, callback);
return observer;
}
// Example usage:
// More complex selector
const observer4 = setupDivRemover('.fc-message-root', (element) => {
console.log('Element with funding in class or id detected:', element);
document.body.style.overflow = 'auto';
});
// To stop observing (if needed)
// observer1.disconnect();
// Alternative: One-liner function for quick setup
const quickDivRemover = (selector) => setupDivRemover(selector, null, true);