From b8db949ecc1c92c2e3c4d298958927138657c099 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 1 Aug 2018 14:23:07 -0400 Subject: [PATCH] try to get submit button from parent modal if not in form --- src/content/notificationBar.ts | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/content/notificationBar.ts b/src/content/notificationBar.ts index be63c90714..c40243864a 100644 --- a/src/content/notificationBar.ts +++ b/src/content/notificationBar.ts @@ -244,7 +244,7 @@ document.addEventListener('DOMContentLoaded', (event) => { function listen(form: HTMLFormElement) { form.removeEventListener('submit', formSubmitted, false); form.addEventListener('submit', formSubmitted, false); - const submitButton = getFormSubmitButton(form, logInButtonNames); + const submitButton = getSubmitButton(form, logInButtonNames); if (submitButton != null) { submitButton.removeEventListener('click', formSubmitted, false); submitButton.addEventListener('click', formSubmitted, false); @@ -305,6 +305,15 @@ document.addEventListener('DOMContentLoaded', (event) => { let form: HTMLFormElement = null; if (e.type === 'click') { form = (e.target as HTMLElement).closest('form'); + if (form == null) { + const parentModal = (e.target as HTMLElement).closest('div.modal'); + if (parentModal != null) { + const modalForms = parentModal.querySelectorAll('form'); + if (modalForms.length === 1) { + form = modalForms[0]; + } + } + } } else { form = e.target as HTMLFormElement; } @@ -349,7 +358,7 @@ document.addEventListener('DOMContentLoaded', (event) => { curPass = passwords[2]; } } else if (formData[i].passwordEls.length === 2 && passwords.length === 2) { - const buttonText = getButtonText(getFormSubmitButton(form, changePasswordButtonNames)); + const buttonText = getButtonText(getSubmitButton(form, changePasswordButtonNames)); const matches = Array.from(changePasswordButtonContainsNames) .filter((n) => buttonText.indexOf(n) > -1); if (matches.length > 0) { @@ -374,8 +383,11 @@ document.addEventListener('DOMContentLoaded', (event) => { } } - function getFormSubmitButton(form: HTMLFormElement, buttonNames: Set) { - let submitButton = form.querySelector('input[type="submit"], input[type="image"], ' + + function getSubmitButton(wrappingEl: HTMLElement, buttonNames: Set) { + if (wrappingEl == null) { + return null; + } + let submitButton = wrappingEl.querySelector('input[type="submit"], input[type="image"], ' + 'button[type="submit"], button:not([type])') as HTMLElement; if (submitButton != null && submitButton.getAttribute('type') == null) { const buttonText = getButtonText(submitButton); @@ -384,7 +396,7 @@ document.addEventListener('DOMContentLoaded', (event) => { } } if (submitButton == null) { - const possibleSubmitButtons = Array.from(form.querySelectorAll('a, span, button[type="button"], ' + + const possibleSubmitButtons = Array.from(wrappingEl.querySelectorAll('a, span, button[type="button"], ' + 'input[type="button"], button:not([type])')) as HTMLElement[]; possibleSubmitButtons.forEach((button) => { if (submitButton != null || button == null || button.tagName == null) { @@ -392,7 +404,7 @@ document.addEventListener('DOMContentLoaded', (event) => { } const buttonText = getButtonText(button); if (buttonText != null) { - if (button.tagName.toLowerCase() === 'button' && submitButton.getAttribute('type') == null && + if (button.tagName.toLowerCase() === 'button' && button.getAttribute('type') == null && !cancelButtonNames.has(buttonText.trim().toLowerCase())) { submitButton = button; } else if (buttonNames.has(buttonText.trim().toLowerCase())) { @@ -401,6 +413,16 @@ document.addEventListener('DOMContentLoaded', (event) => { } }); } + if (submitButton == null && wrappingEl.tagName.toLowerCase() === 'form') { + // Maybe it's in a modal? + const parentModal = wrappingEl.closest('div.modal') as HTMLElement; + if (parentModal != null) { + const modalForms = parentModal.querySelectorAll('form'); + if (modalForms.length === 1) { + submitButton = getSubmitButton(parentModal, buttonNames); + } + } + } return submitButton; }