-
Notifications
You must be signed in to change notification settings - Fork 998
/
Copy pathHCaptchaHtml.swift
165 lines (146 loc) · 5.36 KB
/
HCaptchaHtml.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// HCaptchaHtml.swift (Autogenerated)
// HCaptcha
//
// Copyright © 2024 HCaptcha. All rights reserved.
//
import Foundation
struct HCaptchaHtml {
static var template = """
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
<style>
* {
padding: 0;
margin: 0;
}
body {
display: table;
width: 100%;
height: 100%;
text-align: center;
}
#hcaptcha-container {
margin-top: 5px;
display: table-cell;
vertical-align: middle;
}
/* overwrite hCaptcha iframe overlay which adds a #FFF background with opacity 0.05 */
div > div:nth-child(2) {
opacity: 0 !important;
}
</style>
<script type="text/javascript">
JSON.parse('${debugInfo}' || "[]").forEach(function (value, i) { window[value] = true; });
</script>
<script type="text/javascript">
var script = document.createElement('script');
script.src = "${endpoint}";
script.onerror = function () {
// network issue
post({ error: 7 });
};
document.head.appendChild(script);
</script>
<script type="text/javascript">
var post = function(value) {
window.webkit.messageHandlers.hcaptcha.postMessage(value);
};
var getExecuteOpts = function() {
var opts;
var rqdata = "${rqdata}";
if (rqdata) {
opts = {'rqdata': rqdata};
}
return opts;
};
console.log = function(message) {
post({ log: message });
};
var execute = function() {
console.log("challenge executing");
try {
if ("${size}" === 'invisible') {
hcaptcha.execute(getExecuteOpts());
} else {
post({ action: "showHCaptcha" });
}
console.log("showing challenge");
} catch (e) {
console.log("failed to show challenge");
post({ error: 29 });
}
};
var reset = function() {
console.log("challenge resetting");
hcaptcha.reset();
post({ action: "didLoad" });
};
var onPass = function(token) {
console.log("challenge got token");
console.log(token);
post({ token: token });
};
var expiredCallback = function(action) {
return function() {
console.log("expired challenge");
post({ error: 15 });
post({ action: action });
};
};
var errorCallback = function(error) {
console.log("challenge error:", error || "probably rate limit exceeded");
post({ error: 31 });
};
var closeCallback = function() {
console.log("challenge closed by user");
post({ error: 30 });
post({ action: "onClose" });
};
var openCallback = function(e) {
console.log("challenge opened", e);
post({ action: "showHCaptcha" });
post({ action: "onOpen" });
};
var onloadCallback = function() {
try {
console.log("challenge onload starting");
hcaptcha.render("hcaptcha-container", {
"sitekey": "${apiKey}",
"size": "${size}",
"orientation": "${orientation}",
"theme": ${theme},
"callback": onPass,
"close-callback": closeCallback,
"expired-callback": expiredCallback("onExpired"),
"chalexpired-callback": expiredCallback("onChallengeExpired"),
"error-callback": errorCallback,
"open-callback": openCallback,
});
post({ action: "didLoad" });
} catch (e) {
console.log("challenge failed to render");
post({ error: 29 });
}
};
document.addEventListener('DOMContentLoaded', function () {
var container = document.getElementById("hcaptcha-container");
container.addEventListener("click", function () {
if (window.hcaptcha) {
// Allows dismissal of checkbox view
window.hcaptcha.close();
} else {
post({ error: 30 });
}
});
});
</script>
</head>
<body>
<div id="hcaptcha-container">
</div>
</body>
</html>
"""
}