Skip to content
This repository has been archived by the owner on Apr 17, 2021. It is now read-only.

Commit

Permalink
Fixes 1962: only modify input submit on whitelisted sites
Browse files Browse the repository at this point in the history
  • Loading branch information
severinrudie authored and mcomella committed Nov 21, 2019
1 parent 6cf0040 commit 7e76d4f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It diverges in the following ways:
## 4.6 - ?
### Changed
- After successful Firefox Account sign in, the page the user was on before sign in is restored (#2805)
- Improved behavior of soft keyboard `submit` button (#1962)
- Improved behavior of soft keyboard `submit` button on whitelisted sites (#1962)

## 4.5 - 2019-09-23
### Added
Expand Down
64 changes: 48 additions & 16 deletions app/src/main/java/org/mozilla/tv/firefox/ext/Js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -255,22 +255,54 @@ var _firefoxTV_isPlaybackStateObserverLoaded;
* to many user flows (for example, on google.com, people expect a search to kick off after
* clicking submit, but it doesn't). This script forces a `submit` call after tab events are
* received.
*
* This behavior is added to all <input> elements any time new elements are added to the DOM.
*/
val ADD_SUBMIT_LISTENER_TO_ALL_INPUTS = """
|if (typeof MOZ_FFTV_inputListener === 'undefined') {
| MOZ_FFTV_inputListener = (event) => {
| if (event && event.key === 'Tab') {
| // Get the nearest <form> ancestor
| var formWrapper = Array(...event.path).find((it) => it.tagName === 'FORM')
| if (formWrapper) formWrapper.submit()
| }
| }
|}
|
|Array(...document.getElementsByTagName('input'))
| .forEach((input) => {
| input.removeEventListener("keydown", MOZ_FFTV_inputListener);
| input.addEventListener("keydown", MOZ_FFTV_inputListener);
| });
""".trimMargin()
(function () {
if (typeof _firefoxTV_inputAddedObserver !== 'undefined') return;
function inputSubmitListener(event) {
console.log("SEVTEST: keypress detected");
console.log(event)
if (event && event.key === 'Tab') {
// Get the nearest <form> ancestor
var formWrapper = Array.from(event.path).find((it) => it.tagName === 'FORM')
if (formWrapper) formWrapper.submit()
};
};
function attachListeners() {
console.log("SEVTEST: listeners attached");
Array.from(document.getElementsByTagName('input'))
.forEach((input) => {
input.removeEventListener("keydown", inputSubmitListener);
input.addEventListener("keydown", inputSubmitListener);
});
};
function nodeContainsInput(node) {
return node.nodeName.toLowerCase() === 'input' ||
((node instanceof Element) && !!node.querySelector('input'));
}
_firefoxTV_inputAddedObserver = new MutationObserver(mutationList => {
const wasInputAdded = mutationList.some(mutation => {
return mutation.type === 'childList' &&
(Array.from(mutation.addedNodes).some(nodeContainsInput));
});
console.log("SEVTEST: mutation. wasInputAdded: " + wasInputAdded);
if (wasInputAdded) {
/* This may traverse the whole DOM so let's only call it if it's necessary. */
attachListeners();
}
});
_firefoxTV_inputAddedObserver.observe(document, {subtree: true, childList: true});
attachListeners();
})();
""".trimIndent()
}
13 changes: 13 additions & 0 deletions app/src/main/java/org/mozilla/tv/firefox/ext/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,16 @@ val String.isUriYouTubeTvVideo: Boolean

val String.isUriFxaSignIn: Boolean
get() = this.startsWith("https://accounts.firefox.com/authorization")

private val addInputScriptWhitelist = setOf(
"https://www.google.",
"http://www.bing.",
"https://www.bing.",
"https://www.amazon.",
"https://www.reddit."
)

val String.isUrlWhitelistedForSubmitInputHack: Boolean
get() = addInputScriptWhitelist.any {
this.startsWith(it)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.mozilla.tv.firefox.architecture.FirefoxViewModelProviders
import org.mozilla.tv.firefox.ext.addSubmitListenerToInputElements
import org.mozilla.tv.firefox.ext.couldScrollInDirection
import org.mozilla.tv.firefox.ext.focusedDOMElement
import org.mozilla.tv.firefox.ext.isUrlWhitelistedForSubmitInputHack
import org.mozilla.tv.firefox.ext.isYoutubeTV
import org.mozilla.tv.firefox.ext.maybeGoBackBeforeFxaSignIn
import org.mozilla.tv.firefox.ext.observeScrollPosition
Expand Down Expand Up @@ -123,11 +124,15 @@ class WebRenderFragment : EngineViewLifecycleFragment(), Session.Observer {
}

override fun onLoadingStateChanged(session: Session, loading: Boolean) {
engineView?.addSubmitListenerToInputElements()
if (!loading) {
// If the page isn't finished loading, our observers won't be attached to capture the scroll position
// and the fix won't work. Unfortunately, I've spent too much time on this so I did not prepare a fix.
engineView?.observeScrollPosition()

if (session.url.isUrlWhitelistedForSubmitInputHack) {
engineView?.addSubmitListenerToInputElements()
}

youtubeBackHandler.onLoadComplete()
}
}
Expand Down

0 comments on commit 7e76d4f

Please sign in to comment.