Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hcaptcha
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
hcaptcha
Merge requests
!3
Issue
#3248803
: Add basic support for 'invisible' widget size
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3248803
: Add basic support for 'invisible' widget size
issue/hcaptcha-3248803:3248803-invisible-widget
into
8.x-1.x
Overview
0
Commits
9
Pipelines
0
Changes
5
Open
John Herreño
requested to merge
issue/hcaptcha-3248803:3248803-invisible-widget
into
8.x-1.x
2 years ago
Overview
0
Commits
9
Pipelines
0
Changes
5
Expand
0
0
Merge request reports
Compare
8.x-1.x
version 6
61eb2a5f
4 months ago
version 5
4f5c2e4a
4 months ago
version 4
02fed45d
4 months ago
version 3
c42e74cd
7 months ago
version 2
a2ec45d2
7 months ago
version 1
ffd96408
2 years ago
8.x-1.x (HEAD)
and
latest version
latest version
18ceee04
9 commits,
4 months ago
version 6
61eb2a5f
8 commits,
4 months ago
version 5
4f5c2e4a
7 commits,
4 months ago
version 4
02fed45d
6 commits,
4 months ago
version 3
c42e74cd
5 commits,
7 months ago
version 2
a2ec45d2
4 commits,
7 months ago
version 1
ffd96408
2 commits,
2 years ago
5 files
+
136
−
2
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
js/hcaptcha-invisible.js
0 → 100644
+
122
−
0
Options
(
function
(
$
,
Drupal
,
drupalSettings
,
once
)
{
'
use strict
'
;
function
fetchHcaptchaToken
(
form
,
triggeringElemName
)
{
var
promise
=
$
.
Deferred
();
const
invisibleWidget
=
document
.
querySelector
(
`#
${
form
.
id
}
.h-captcha[data-size="invisible"]`
);
if
(
!
invisibleWidget
)
{
return
promise
.
resolve
(
true
);
}
const
triggerElem
=
form
.
querySelector
(
`[name="
${
triggeringElemName
}
"]`
);
if
(
triggerElem
)
{
// Disable the triggering element before
// Drupal.Ajax.prototype.beforeSend() does, since the hCaptcha token
// round-trip is potentially slow. Logic below copies some ideas from
// ...beforeSend().
const
triggerElemValue
=
(
$
.
fieldValue
&&
$
.
fieldValue
(
triggerElem
))
||
null
;
triggerElem
.
setAttribute
(
'
disabled
'
,
true
);
if
(
triggerElem
.
type
!==
'
submit
'
&&
triggerElemValue
!==
null
)
{
// This should only run for AJAX-submitted forms, triggering with
// non-submit elements.
const
ajaxOpPreserve
=
form
.
querySelector
(
'
.hcaptcha-ajax-op-preserve
'
)
||
document
.
createElement
(
'
input
'
);
ajaxOpPreserve
.
setAttribute
(
"
type
"
,
"
hidden
"
);
ajaxOpPreserve
.
className
=
'
hcaptcha-ajax-op-preserve
'
;
ajaxOpPreserve
.
name
=
triggerElem
.
name
;
ajaxOpPreserve
.
value
=
triggerElemValue
;
triggerElem
.
before
(
ajaxOpPreserve
);
}
}
hcaptcha
.
execute
(
invisibleWidget
.
dataset
.
hcaptchaWidgetId
,
{
async
:
true
})
.
then
(
function
({
response
})
{
[
"
h-captcha-response
"
,
'
g-recaptcha-response
'
]
.
map
(
elemName
=>
form
.
querySelector
(
`[name="
${
elemName
}
"]`
))
.
filter
(
v
=>
v
)
// keep only actual elements.
.
forEach
(
elem
=>
elem
.
value
=
response
);
promise
.
resolve
(
true
);
})
.
catch
(
err
=>
{
console
.
error
(
err
);
// Simplistic handling of close/expired challenge; resolving the
// promise lets the caller continue the submission flow, which must
// then fail due to the missing hcaptcha response.
promise
.
resolve
(
false
);
});
return
promise
;
}
// Handle ajax forms.
var
originalAjaxSubmit
;
Drupal
.
behaviors
.
hcaptchaAjaxSubmitOverride
=
{
attach
:
function
(
context
,
settings
)
{
if
(
originalAjaxSubmit
||
!
$
.
fn
.
ajaxSubmit
)
return
;
originalAjaxSubmit
=
$
.
fn
.
ajaxSubmit
;
$
.
fn
.
ajaxSubmit
=
function
(
options
,
data
,
dataType
,
onSuccess
)
{
var
self
=
this
;
fetchHcaptchaToken
(
self
[
0
],
options
.
data
.
_triggering_element_name
)
.
then
(()
=>
{
const
origComplete
=
options
.
complete
;
options
.
complete
=
function
(
xmlhttprequest
,
status
)
{
// Clean-up: remove hidden element used to mirror the disabled
// element.
self
[
0
].
querySelector
(
'
.hcaptcha-ajax-op-preserve
'
)?.
remove
();
origComplete
(
xmlhttprequest
,
status
);
};
originalAjaxSubmit
.
call
(
self
,
options
,
data
,
dataType
,
onSuccess
);
});
};
}
};
Drupal
.
behaviors
.
hcaptchaInvisible
=
{
attach
:
function
(
context
,
settings
)
{
// Handle non-ajax forms.
let
isFetchingToken
=
false
;
const
formsWithInvisible
=
Array
.
from
(
document
.
querySelectorAll
(
'
.h-captcha[data-size="invisible"]
'
))
.
map
((
elem
)
=>
elem
.
closest
(
'
form
'
));
$
(
formsWithInvisible
).
one
(
'
submit
'
,
function
(
e
)
{
const
self
=
this
;
if
(
e
.
originalEvent
.
submitter
.
getAttribute
(
'
formnovalidate
'
))
{
return
;
}
if
(
isFetchingToken
)
return
false
;
isFetchingToken
=
true
;
fetchHcaptchaToken
(
self
,
e
.
originalEvent
.
submitter
&&
e
.
originalEvent
.
submitter
.
getAttribute
(
'
name
'
))
.
then
(()
=>
{
isFetchingToken
=
false
;
self
.
submit
();
});
return
false
;
});
// Calling formElement.submit() (see above) causes the POST data to not
// include [type=submit] elements. Let's inject a hidden element to mimic
// name + value for the type=submit element that was clicked to trigger
// the form submission.
once
(
'
hcaptcha-op-preserve
'
,
formsWithInvisible
).
forEach
((
theForm
)
=>
{
const
opPreserve
=
document
.
createElement
(
"
input
"
);
opPreserve
.
setAttribute
(
"
type
"
,
"
hidden
"
);
$
(
theForm
).
on
(
'
click
'
,
'
[type=submit]
'
,
function
(
e
)
{
const
submitId
=
e
.
currentTarget
.
id
;
// Do not do this on AJAX-y submissions.
if
(
!
drupalSettings
.
ajax
||
!
drupalSettings
.
ajax
.
hasOwnProperty
(
submitId
))
{
opPreserve
.
name
=
this
.
name
;
opPreserve
.
value
=
this
.
value
;
theForm
.
appendChild
(
opPreserve
);
}
});
});
}
};
})(
jQuery
,
Drupal
,
drupalSettings
,
once
);
Loading