1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

Fix form detection for password managerä

Original Author: Davide

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2023-10-17 21:39:13 +02:00
parent 563f81f5cf
commit 27c59ed16a
Signed by: SGOrava
GPG Key ID: 13660A3F1D9F093B

View File

@ -74,31 +74,37 @@ QString Scripts::setupWebChannel()
QString Scripts::setupFormObserver() QString Scripts::setupFormObserver()
{ {
QString source = QL1S("(function() {" QString source = QL1S("(function() {"
"function findUsername(inputs) {" " let eFormsOld = [],"
" var usernameNames = ['user', 'name', 'login'];" " eFormsDone = [];"
" for (var i = 0; i < usernameNames.length; ++i) {" ""
" for (var j = 0; j < inputs.length; ++j)" " function findUsername(inputs) {"
" let usernameNames = ['user', 'name', 'login'];"
""
" for (let i = 0; i < usernameNames.length; ++i) {"
" for (let j = 0; j < inputs.length; ++j)"
" if (inputs[j].type == 'text' && inputs[j].value.length && inputs[j].name.indexOf(usernameNames[i]) != -1)" " if (inputs[j].type == 'text' && inputs[j].value.length && inputs[j].name.indexOf(usernameNames[i]) != -1)"
" return inputs[j].value;" " return inputs[j].value;"
" }" " }"
" for (var i = 0; i < inputs.length; ++i)" ""
" for (let i = 0; i < inputs.length; ++i)"
" if (inputs[i].type == 'text' && inputs[i].value.length)" " if (inputs[i].type == 'text' && inputs[i].value.length)"
" return inputs[i].value;" " return inputs[i].value;"
" for (var i = 0; i < inputs.length; ++i)" ""
" for (let i = 0; i < inputs.length; ++i)"
" if (inputs[i].type == 'email' && inputs[i].value.length)" " if (inputs[i].type == 'email' && inputs[i].value.length)"
" return inputs[i].value;" " return inputs[i].value;"
" return '';"
"}"
"" ""
"function registerForm(form) {" " return '';"
" form.addEventListener('submit', function() {" " }"
" var form = this;" ""
" var data = '';" " function processForm(eForm) {"
" var password = '';" " let data = '';"
" var inputs = form.getElementsByTagName('input');" " let password = '';"
" for (var i = 0; i < inputs.length; ++i) {" " let inputs = eForm.getElementsByTagName('input');"
" var input = inputs[i];" ""
" var type = input.type.toLowerCase();" " for (let i = 0; i < inputs.length; ++i) {"
" let input = inputs[i];"
" let type = input.type.toLowerCase();"
" if (type != 'text' && type != 'password' && type != 'email')" " if (type != 'text' && type != 'password' && type != 'email')"
" continue;" " continue;"
" if (!password && type == 'password')" " if (!password && type == 'password')"
@ -108,28 +114,59 @@ QString Scripts::setupFormObserver()
" data += encodeURIComponent(input.value);" " data += encodeURIComponent(input.value);"
" data += '&';" " data += '&';"
" }" " }"
""
" if (!password)" " if (!password)"
" return;" " return;"
""
" if (eFormsDone.every(e => e != eForm)) {"
" eFormsDone.push(eForm);"
" } else {"
" return;"
" }"
""
" data = data.substring(0, data.length - 1);" " data = data.substring(0, data.length - 1);"
" var url = window.location.href;" " let url = window.location.href;"
" var username = findUsername(inputs);" " let username = findUsername(inputs);"
" external.autoFill.formSubmitted(url, username, password, data);" " external.autoFill.formSubmitted(url, username, password, data);"
" }, true);" " }"
"}"
"" ""
"if (!document.documentElement) return;" " function undoForm(eForm) {"
" let i = eFormsDone.indexOf(eForm);"
"" ""
"for (var i = 0; i < document.forms.length; ++i)" " if (i >= 0) {"
" registerForm(document.forms[i]);" " eFormsDone.splice(i, 1);"
" }"
" }"
"" ""
"var observer = new MutationObserver(function(mutations) {" " function registerForm(eForm) {"
" for (var mutation of mutations)" " let eInputs = eForm.getElementsByTagName('input');"
" for (var node of mutation.addedNodes)"
" if (node.tagName && node.tagName.toLowerCase() == 'form')"
" registerForm(node);"
"});"
"observer.observe(document.documentElement, { childList: true, subtree: true });"
"" ""
" eForm.addEventListener('submit', () => processForm(eForm), true);"
""
" for (let eInput of eInputs) {"
" let type = eInput.type.toLowerCase();"
" "
" if (type == 'password') {"
" eInput.addEventListener('blur', () => processForm(eForm), true);"
" eInput.addEventListener('input', () => undoForm(eForm), true);"
" eInput.addEventListener('keydown', () => event.keyCode === 13 && processForm(eForm), true);"
" }"
" }"
" }"
""
" setInterval(() => {"
" try {"
" let eFormsNew = Array.from(document.forms);"
""
" for (let eFormNew of eFormsNew) {"
" if (eFormsOld.every(e => e != eFormNew)) {"
" eFormsOld.push(eFormNew);"
" registerForm(eFormNew);"
" }"
" }"
""
" } catch (e) {}"
" }, 100);"
"})()"); "})()");
return source; return source;