Skip to content

Commit 9b897b9

Browse files
committed
feat: migrate to ES5 compatible loader
1 parent 8bc3cf6 commit 9b897b9

File tree

4 files changed

+56
-31
lines changed

4 files changed

+56
-31
lines changed

sdk/build.gradle

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
}
1212

1313
ext {
14-
hcaptchaLoaderVersion = "1.1.3"
14+
hcaptchaLoaderVersion = "1.2.3"
1515
}
1616

1717
android {
@@ -126,14 +126,20 @@ project.afterEvaluate {
126126
}
127127
}
128128

129-
long MAX_AAR_SIZE_KB = 200
129+
long MAX_AAR_SIZE_KB = 250
130130

131-
tasks.register('downloadHCaptchaLoaderJs', Download) {
132-
src "https://www.unpkg.com/@hcaptcha/loader@${hcaptchaLoaderVersion}/dist/index.mjs"
133-
dest layout.buildDirectory.file("generated/assets/hcaptcha/loader.mjs")
131+
tasks.register('downloadPolyfillsJs', Download) {
132+
src "https://www.unpkg.com/@hcaptcha/loader@${hcaptchaLoaderVersion}/dist/polyfills.js"
133+
dest layout.buildDirectory.file("generated/assets/hcaptcha/polyfills.js")
134134
onlyIfModified true
135135
}
136136

137+
tasks.register('downloadHCaptchaLoaderJs', Download) {
138+
src "https://www.unpkg.com/@hcaptcha/loader@${hcaptchaLoaderVersion}/dist/index.es5.js"
139+
dest layout.buildDirectory.file("generated/assets/hcaptcha/loader.js")
140+
onlyIfModified true
141+
}.get().dependsOn('downloadPolyfillsJs')
142+
137143
android.sourceSets.main.assets.srcDirs += [layout.buildDirectory.file("generated/assets")]
138144

139145
android.libraryVariants.all { variant ->
@@ -159,7 +165,7 @@ android.libraryVariants.all { variant ->
159165
var aarFile = variant.packageLibraryProvider.get().archiveFile.get().getAsFile()
160166
long aarSizeKb = aarFile.length() / 1024
161167
if (aarSizeKb > MAX_AAR_SIZE_KB) {
162-
throw new GradleException("${aarPath} size exceeded! ${aarSizeKb}Kbyte > ${MAX_AAR_SIZE_KB}Kbyte")
168+
throw new GradleException("${aarFile} size exceeded! ${aarSizeKb}Kbyte > ${MAX_AAR_SIZE_KB}Kbyte")
163169
}
164170
}
165171
})

sdk/src/main/html/hcaptcha.html

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
window.sysDebug = JSON.parse(window.JSDI.getSysDebug());
3737
}
3838
</script>
39-
<script type="module">
40-
import { hCaptchaLoader } from 'https://www.unpkg.com/@hcaptcha/loader@@LOADER_VERSION@/dist/index.mjs';
41-
39+
<script type="text/javascript" src="https://unpkg.com/@hcaptcha/loader@@LOADER_VERSION@/dist/polyfills.js"></script>
40+
<script type="text/javascript" src="https://unpkg.com/@hcaptcha/loader@@LOADER_VERSION@/dist/index.es5.js"></script>
41+
<script>
4242
// Android will inject this bridge object as `JSInterface`
4343
// Browser is missing it, so we mock it
4444
var BridgeObject = window.JSInterface || {
@@ -75,9 +75,9 @@
7575
/**
7676
* Called programmatically from HCaptchaWebViewHelper.
7777
*/
78-
async function resetAndExecute() {
78+
function resetAndExecute() {
7979
hcaptcha.reset();
80-
await execute();
80+
execute();
8181
}
8282
window.resetAndExecute = resetAndExecute;
8383
function reset() {
@@ -175,17 +175,16 @@
175175
BridgeObject.onError(30);
176176
}
177177
});
178-
async function execute() {
179-
try {
180-
var { response } = await hcaptcha.execute(getScriptParams(bridgeConfig));
178+
function execute() {
179+
hcaptcha.execute(getScriptParams(bridgeConfig)).then(function(result) {
180+
var response = result.response;
181181
BridgeObject.onPass(response);
182-
} catch (error) {
182+
}).catch(function(error) {
183183
errorCallback(error);
184-
}
184+
});
185185
}
186-
async function loadApi(config) {
187-
try {
188-
window.hcaptcha = await hCaptchaLoader(getLoaderParams(config));
186+
function loadApi(config) {
187+
hCaptchaLoader(getLoaderParams(config)).then(function(hcaptcha) {
189188
var renderConfig = getRenderConfig(config);
190189
hcaptcha.render("hcaptcha-container", renderConfig);
191190
BridgeObject.onLoaded();
@@ -197,12 +196,12 @@
197196
// We want to auto execute in case of `invisible` checkbox.
198197
// But not in case of `hideDialog` since verification process
199198
// might be desired to happen at a later time.
200-
await execute();
199+
execute();
201200
}
202-
} catch (e) {
203-
console.error("loadApi error", e);
201+
}).catch(function(error) {
202+
console.error("loadApi error", error);
204203
BridgeObject.onError(29);
205-
}
204+
});
206205
}
207206
loadApi(bridgeConfig);
208207
</script>

sdk/src/main/java/com/hcaptcha/sdk/HCaptchaWebViewHelper.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import java.io.IOException;
2626
import java.util.Collections;
27+
import java.util.HashMap;
28+
import java.util.Map;
2729
import java.util.Objects;
2830

2931
final class HCaptchaWebViewHelper {
@@ -130,9 +132,7 @@ public boolean shouldRetry(HCaptchaException exception) {
130132

131133
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
132134
private class HCaptchaWebClient extends WebViewClient {
133-
private final Uri loaderUri = Uri.parse(
134-
"https://www.unpkg.com/@hcaptcha/loader@" + BuildConfig.LOADER_VERSION + "/dist/index.mjs"
135-
);
135+
private final Map<Uri, String> assetsCache = new HashMap<>();
136136

137137
@NonNull
138138
private final Handler handler;
@@ -143,6 +143,10 @@ private class HCaptchaWebClient extends WebViewClient {
143143
HCaptchaWebClient(@NonNull Handler handler, @NonNull HCaptchaStateListener listener) {
144144
this.handler = handler;
145145
this.listener = listener;
146+
147+
final String baseUrl = "https://unpkg.com/@hcaptcha/loader@" + BuildConfig.LOADER_VERSION + "/dist";
148+
assetsCache.put(Uri.parse(baseUrl + "/index.es5.js"), "hcaptcha/loader.js");
149+
assetsCache.put(Uri.parse(baseUrl + "/polyfills.js"), "hcaptcha/polyfills.js");
146150
}
147151

148152
private String stripUrl(String url) {
@@ -152,7 +156,8 @@ private String stripUrl(String url) {
152156
@Override
153157
public WebResourceResponse shouldInterceptRequest (final WebView view, final WebResourceRequest request) {
154158
final Uri requestUri = request.getUrl();
155-
if (loaderUri.equals(requestUri)) {
159+
final String assetPath = assetsCache.get(requestUri);
160+
if (assetPath != null) {
156161
try {
157162
return new WebResourceResponse(
158163
"application/javascript",
@@ -161,10 +166,10 @@ public WebResourceResponse shouldInterceptRequest (final WebView view, final Web
161166
"OK",
162167
Collections.singletonMap("Access-Control-Allow-Origin",
163168
Objects.toString(config.getHost(), "null")),
164-
view.getContext().getAssets().open("hcaptcha/loader.mjs")
169+
view.getContext().getAssets().open(assetPath)
165170
);
166171
} catch (IOException e) {
167-
HCaptchaLog.w("WebViewHelper wasn't able to load loader.mjs from assets");
172+
HCaptchaLog.w("WebViewHelper wasn't able to load " + assetPath + " from assets");
168173
}
169174
} else if (requestUri != null && requestUri.getScheme() != null && requestUri.getScheme().equals("http")) {
170175
handler.post(() -> {

test/src/androidTest/java/com/hcaptcha/sdk/HCaptchaWebViewHelperTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import static org.mockito.Mockito.mock;
88

99
import android.content.Context;
10+
import android.content.res.AssetManager;
1011
import android.os.Handler;
1112
import android.os.Looper;
1213

1314
import androidx.test.core.app.ActivityScenario;
15+
import androidx.test.core.app.ApplicationProvider;
1416
import androidx.test.ext.junit.rules.ActivityScenarioRule;
1517
import androidx.test.platform.app.InstrumentationRegistry;
1618

@@ -21,6 +23,7 @@
2123
import org.junit.Test;
2224

2325
import java.io.IOException;
26+
import java.io.InputStream;
2427
import java.util.concurrent.CountDownLatch;
2528
import java.util.concurrent.TimeUnit;
2629

@@ -76,7 +79,19 @@ void onFailure(HCaptchaException e) {
7679

7780
@Test
7881
public void testLoaderJsAssetPresence() throws IOException {
79-
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
80-
assertNotNull(appContext.getAssets().open("hcaptcha/loader.mjs"));
82+
AssetManager assets = InstrumentationRegistry
83+
.getInstrumentation()
84+
.getTargetContext()
85+
.getAssets();
86+
assertNotNull(assets.open("hcaptcha/loader.js"));
87+
}
88+
89+
@Test
90+
public void testPolyfillsJsAssetPresence() throws IOException {
91+
AssetManager assets = InstrumentationRegistry
92+
.getInstrumentation()
93+
.getTargetContext()
94+
.getAssets();
95+
assertNotNull(assets.open("hcaptcha/polyfills.js"));
8196
}
8297
}

0 commit comments

Comments
 (0)