Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions sdk/src/main/html/hcaptcha.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,37 @@
}
return theme;
}
/**
* hCaptcha renders the challenge into a fixed 400x600 overlay appended to
* `<body>`, positioned near the checkbox. On a short viewport (landscape,
* or a display size scaled up by the user) 600px does not fit, and the
* overlay overflows past the top edge where the prompt cannot be scrolled
* back into view. Scale it down so the whole challenge stays on screen.
*/
function fitChallengeToViewport() {
var overlay = null;
var children = document.body.children;
for (var i = 0; i < children.length; i++) {
var el = children[i];
if (el.tagName === 'DIV' && el.id !== 'hcaptcha-container' &&
el.querySelector('iframe')) {
overlay = el;
}
}
if (!overlay) {
return;
}
// offsetHeight is the untransformed layout height, so this stays
// correct when we are re-scaling an already scaled overlay.
var needed = overlay.offsetHeight;
if (!needed) {
return;
}
var scale = Math.min(1, window.innerHeight / needed);
overlay.style.transformOrigin = 'top center';
overlay.style.transform = scale < 1 ? 'scale(' + scale + ')' : '';
}

function getRenderConfig() {
return {
sitekey: bridgeConfig.siteKey,
Expand Down Expand Up @@ -153,6 +184,7 @@
}
},
'open-callback': function openCallback() {
fitChallengeToViewport();
return BridgeObject.onOpen();
}
};
Expand Down Expand Up @@ -216,6 +248,9 @@
}
});

window.addEventListener('resize', fitChallengeToViewport);
new MutationObserver(fitChallengeToViewport).observe(document.body, { childList: true });

loadApi();
</script>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ public void onStart() {
if (dialog != null && webViewHelper != null) {
final Window window = dialog.getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Dialog theme wraps the window to its content; pin it so the page viewport
// doesn't track what hCaptcha renders (#227)
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
defaultDimAmount = window.getAttributes().dimAmount;
if (!readyForInteraction && Boolean.FALSE.equals(webViewHelper.getConfig().getLoading())) {
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Expand Down Expand Up @@ -439,7 +442,8 @@ private void prepareWebView(@NonNull View rootView, @NonNull HCaptchaConfig conf
if (sPreloadWebView.getParent() != null) {
((ViewGroup) sPreloadWebView.getParent()).removeView(sPreloadWebView);
}
parent.addView(sPreloadWebView, index);
// Keep the stub's match_parent, otherwise the WebView is wrap_content (#227)
parent.addView(sPreloadWebView, index, stub.getLayoutParams());
if (Boolean.FALSE.equals(config.getLoading())) {
sPreloadWebView.setOnTouchListener((view, event) -> {
if (!readyForInteraction && isAdded()) {
Expand Down
Loading