File tree Expand file tree Collapse file tree 4 files changed +38
-4
lines changed
main/kotlin/io/logto/sdk/android/auth/logto
test/kotlin/io/logto/sdk/android/auth/logto Expand file tree Collapse file tree 4 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,10 @@ class LogtoWebViewAuthActivity : AppCompatActivity() {
26
26
webView = WebView (this ).apply {
27
27
settings.javaScriptEnabled = true
28
28
settings.cacheMode = WebSettings .LOAD_NO_CACHE
29
+
30
+ val polyfill = LogtoWebViewPolyfill (hostActivity = this @LogtoWebViewAuthActivity)
31
+ addJavascriptInterface(polyfill, LogtoWebViewPolyfill .NAME )
32
+
29
33
val socialHandler = LogtoWebViewSocialHandler (
30
34
webView = this ,
31
35
hostActivity = this @LogtoWebViewAuthActivity,
@@ -34,9 +38,12 @@ class LogtoWebViewAuthActivity : AppCompatActivity() {
34
38
socialHandler,
35
39
LogtoWebViewSocialHandler .NAME ,
36
40
)
41
+
42
+ val injectScript = polyfill.getInjectScript() + socialHandler.getInjectScript()
43
+
37
44
webViewClient = LogtoWebViewAuthClient (
38
45
hostActivity = this @LogtoWebViewAuthActivity,
39
- injectScript = socialHandler.getInjectSocialScript() ,
46
+ injectScript = injectScript ,
40
47
)
41
48
}
42
49
webView.loadUrl(uri)
Original file line number Diff line number Diff line change
1
+ package io.logto.sdk.android.auth.logto
2
+
3
+ import android.app.Activity
4
+ import android.content.ClipData
5
+ import android.content.ClipboardManager
6
+ import android.content.Context
7
+ import android.webkit.JavascriptInterface
8
+
9
+ class LogtoWebViewPolyfill (private val hostActivity : Activity ) {
10
+ companion object {
11
+ const val NAME = " Polyfill"
12
+ }
13
+
14
+ fun getInjectScript () = """
15
+ window.addEventListener("load", () => {
16
+ window.navigator.clipboard = {
17
+ writeText: (text) => window.$NAME .writeText(text),
18
+ };
19
+ });
20
+ """ .trimIndent()
21
+
22
+ @JavascriptInterface
23
+ fun writeText (text : String ) {
24
+ val clipboard = hostActivity.getSystemService(Context .CLIPBOARD_SERVICE ) as ClipboardManager
25
+ clipboard.setPrimaryClip(ClipData .newPlainText(text, text))
26
+ }
27
+ }
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ class LogtoWebViewSocialHandler(
23
23
}
24
24
25
25
// Note: the `universal` will always be true for it is supported by the Android OS.
26
- fun getInjectSocialScript () = """
26
+ fun getInjectScript () = """
27
27
window.logtoNativeSdk = {
28
28
platform: 'android',
29
29
getPostMessage: () => (data) => window.$NAME .postMessage(JSON.stringify(data)),
Original file line number Diff line number Diff line change @@ -48,12 +48,12 @@ class LogtoWebViewSocialHandlerTest {
48
48
SocialSessionHelper .getSupportedNativeConnectorTargets()
49
49
} returns mutableListOf (" wechat" , " alipay" )
50
50
51
- val injectSocialScript = logtoWebViewSocialHandler.getInjectSocialScript ()
51
+ val injectScript = logtoWebViewSocialHandler.getInjectScript ()
52
52
verify {
53
53
SocialSessionHelper .getSupportedNativeConnectorTargets()
54
54
}
55
55
56
- assertThat(injectSocialScript )
56
+ assertThat(injectScript )
57
57
.isEqualTo(
58
58
"""
59
59
window.logtoNativeSdk = {
You can’t perform that action at this time.
0 commit comments