The following code get's loaded after the page is done:
<div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-wrapper" id="edit-actions">
<input data-drupal-selector="edit-submit" type="submit" id="edit-submit--6" name="op" value="Save" class="button button--primary js-form-submit form-submit">
</div>
I'm trying to attach an event to this SAVE button by monitoring the page for this button to get injected. My code has the following: node.value === 'Save' to see if the input button is injected, but doesnt detect it. It looks like the "node" injects the entire FORM block, which is why it's not working.
This is my mutation code that is most likely incorrect somewhere:
// Your ready function
function ready(fn) {
if (document.readyState !== 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
// Function to handle button click
function buttonClickHandler() {
event.preventDefault();
console.log(event.target.form.reportValidity())
if (event.target.form.reportValidity()) {
sendMessageToTelegram('000000000', '-999999999');
event.target.form.submit();
}
}
// Function to add an event listener to the button when it's added to the DOM
function addButtonEventListener() {
console.log('add Button Event List......')
// Create a MutationObserver instance to observe changes in the DOM
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
// Check if the added node is an input button with the value "Save"
console.log(node)
if (node.value === 'Save') {
// Add the event listener to the button
node.addEventListener('click', buttonClickHandler);
// Disconnect the observer once the button is found
observer.disconnect();
}
});
}
});
});
// Configure the MutationObserver to watch for changes in the entire DOM tree
const observerConfig = { childList: true, subtree: true };
var commentForm = document.querySelector('.comment-wrapper');
// Start observing the DOM
observer.observe(commentForm, observerConfig);
}
// Execute the addButtonEventListener function when the DOM is ready
ready(addButtonEventListener);