Skip to content

Commit e7fe84f

Browse files
StypoxAudricV
authored andcommitted
Make sure downloadAndRunBotguard() is called after <script> loaded
1 parent 2b183a0 commit e7fe84f

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/potoken/PoTokenWebView.kt

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PoTokenWebView private constructor(
3939
webviewSettings.blockNetworkLoads = true // the WebView does not need internet access
4040

4141
// so that we can run async functions and get back the result
42-
webView.addJavascriptInterface(this, "PoTokenWebView")
42+
webView.addJavascriptInterface(this, JS_INTERFACE)
4343
}
4444

4545
/**
@@ -48,6 +48,10 @@ class PoTokenWebView private constructor(
4848
* run it, and obtain an `integrityToken`.
4949
*/
5050
private fun loadHtmlAndObtainBotguard(context: Context) {
51+
if (BuildConfig.DEBUG) {
52+
Log.d(TAG, "loadHtmlAndObtainBotguard() called")
53+
}
54+
5155
disposables.add(
5256
Single.fromCallable {
5357
val html = context.assets.open("po_token.html").bufferedReader()
@@ -60,22 +64,31 @@ class PoTokenWebView private constructor(
6064
{ html ->
6165
webView.loadDataWithBaseURL(
6266
"https://www.youtube.com",
63-
html,
67+
html.replace(
68+
"</script>",
69+
// calls downloadAndRunBotguard() when the page has finished loading
70+
"\n$JS_INTERFACE.downloadAndRunBotguard()</script>"
71+
),
6472
"text/html",
6573
"utf-8",
6674
null,
6775
)
68-
downloadAndRunBotguard()
6976
},
7077
this::onInitializationErrorCloseAndCancel
7178
)
7279
)
7380
}
7481

7582
/**
76-
* Called during initialization after the WebView content has been loaded.
83+
* Called during initialization by the JavaScript snippet appended to the HTML page content in
84+
* [loadHtmlAndObtainBotguard] after the WebView content has been loaded.
7785
*/
78-
private fun downloadAndRunBotguard() {
86+
@JavascriptInterface
87+
fun downloadAndRunBotguard() {
88+
if (BuildConfig.DEBUG) {
89+
Log.d(TAG, "downloadAndRunBotguard() called")
90+
}
91+
7992
makeJnnPaGoogleapisRequest(
8093
"https://jnn-pa.googleapis.com/\$rpc/google.internal.waa.v1.Waa/Create",
8194
"[ \"$REQUEST_KEY\" ]",
@@ -86,9 +99,9 @@ class PoTokenWebView private constructor(
8699
data = JSON.parse(String.raw`$responseBody`)
87100
result = await runBotGuard(data)
88101
globalThis.webPoSignalOutput = result.webPoSignalOutput
89-
PoTokenWebView.onRunBotguardResult(result.botguardResponse)
102+
$JS_INTERFACE.onRunBotguardResult(result.botguardResponse)
90103
} catch (error) {
91-
PoTokenWebView.onJsInitializationError(error.toString())
104+
$JS_INTERFACE.onJsInitializationError(error.toString())
92105
}
93106
})();""",
94107
) {}
@@ -127,9 +140,9 @@ class PoTokenWebView private constructor(
127140
"""(async function() {
128141
try {
129142
globalThis.integrityToken = JSON.parse(String.raw`$responseBody`)
130-
PoTokenWebView.onInitializationFinished(integrityToken[1])
143+
$JS_INTERFACE.onInitializationFinished(integrityToken[1])
131144
} catch (error) {
132-
PoTokenWebView.onJsInitializationError(error.toString())
145+
$JS_INTERFACE.onJsInitializationError(error.toString())
133146
}
134147
})();""",
135148
) {}
@@ -145,6 +158,9 @@ class PoTokenWebView private constructor(
145158
*/
146159
@JavascriptInterface
147160
fun onInitializationFinished(expirationTimeInSeconds: Long) {
161+
if (BuildConfig.DEBUG) {
162+
Log.d(TAG, "onInitializationFinished() called, expiration=${expirationTimeInSeconds}s")
163+
}
148164
// leave 10 minutes of margin just to be sure
149165
expirationInstant = Instant.now().plusSeconds(expirationTimeInSeconds - 600)
150166
generatorEmitter.onSuccess(this)
@@ -178,6 +194,9 @@ class PoTokenWebView private constructor(
178194

179195
override fun generatePoToken(identifier: String): Single<String> =
180196
Single.create { emitter ->
197+
if (BuildConfig.DEBUG) {
198+
Log.d(TAG, "generatePoToken() called with identifier $identifier")
199+
}
181200
runOnMainThread(emitter) {
182201
addPoTokenEmitter(identifier, emitter)
183202
webView.evaluateJavascript(
@@ -186,9 +205,9 @@ class PoTokenWebView private constructor(
186205
try {
187206
poToken = await obtainPoToken(webPoSignalOutput, integrityToken,
188207
identifier)
189-
PoTokenWebView.onObtainPoTokenResult(identifier, poToken)
208+
$JS_INTERFACE.onObtainPoTokenResult(identifier, poToken)
190209
} catch (error) {
191-
PoTokenWebView.onObtainPoTokenError(identifier, error.toString())
210+
$JS_INTERFACE.onObtainPoTokenError(identifier, error.toString())
192211
}
193212
})();""",
194213
) {}
@@ -309,6 +328,7 @@ class PoTokenWebView private constructor(
309328
private const val REQUEST_KEY = "O43z0dpjhgX20SCx4KAo"
310329
private const val USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
311330
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.3"
331+
private const val JS_INTERFACE = "PoTokenWebView"
312332

313333
override fun newPoTokenGenerator(context: Context): Single<PoTokenGenerator> =
314334
Single.create { emitter ->

0 commit comments

Comments
 (0)