From 69163d077d17aa9fb12d2a165408998502c53113 Mon Sep 17 00:00:00 2001 From: Carlos Olivo Date: Thu, 17 Jun 2021 15:15:08 -0500 Subject: [PATCH] Improve JS injection method --- app/src/main/assets/native/injectionScript.js | 16 +++++++++++++ .../mobile/fragment/WebViewFragment.kt | 15 ++++-------- .../org/jellyfin/mobile/utils/Constants.kt | 2 +- .../org/jellyfin/mobile/utils/WebAppUtils.kt | 23 ------------------- 4 files changed, 22 insertions(+), 34 deletions(-) create mode 100644 app/src/main/assets/native/injectionScript.js diff --git a/app/src/main/assets/native/injectionScript.js b/app/src/main/assets/native/injectionScript.js new file mode 100644 index 0000000000..905d484a7f --- /dev/null +++ b/app/src/main/assets/native/injectionScript.js @@ -0,0 +1,16 @@ +!function() { + var scripts = [ + '/native/nativeshell.js', + '/native/EventEmitter.js', + document.currentScript.src.concat('?', Date.now()) + ]; + scripts.forEach(function(src) { + var scriptElement = document.createElement('script'); + scriptElement.type = 'text/javascript'; + scriptElement.src = src; + scriptElement.charset = 'utf-8'; + scriptElement.setAttribute('defer', ''); + document.body.appendChild(scriptElement); + }); + document.currentScript.remove(); +}(); diff --git a/app/src/main/java/org/jellyfin/mobile/fragment/WebViewFragment.kt b/app/src/main/java/org/jellyfin/mobile/fragment/WebViewFragment.kt index 6963af8361..65a192b784 100644 --- a/app/src/main/java/org/jellyfin/mobile/fragment/WebViewFragment.kt +++ b/app/src/main/java/org/jellyfin/mobile/fragment/WebViewFragment.kt @@ -43,7 +43,6 @@ import org.jellyfin.mobile.model.sql.entity.ServerEntity import org.jellyfin.mobile.player.PlayerFragment import org.jellyfin.mobile.utils.Constants import org.jellyfin.mobile.utils.Constants.FRAGMENT_WEB_VIEW_EXTRA_SERVER -import org.jellyfin.mobile.utils.JS_INJECTION_CODE import org.jellyfin.mobile.utils.addFragment import org.jellyfin.mobile.utils.applyDefault import org.jellyfin.mobile.utils.applyWindowInsetsAsMargins @@ -61,7 +60,7 @@ import org.json.JSONObject import org.koin.android.ext.android.inject import timber.log.Timber import java.io.Reader -import java.util.* +import java.util.Locale import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlin.coroutines.suspendCoroutine @@ -157,15 +156,11 @@ class WebViewFragment : Fragment(), NativePlayerHost { val url = request.url val path = url.path?.lowercase(Locale.ROOT) ?: return null return when { - path.endsWith(Constants.WEB_CONFIG_PATH) -> { - runOnUiThread { - webView.evaluateJavascript(JS_INJECTION_CODE) { - onConnectedToWebapp() - } - } - null // continue loading normally + !connected && path.endsWith(Constants.ANY_BUNDLE_PATH) -> { + onConnectedToWebapp() + webView.context.loadAsset("native/injectionScript.js") } - path.contains("native") -> webView.context.loadAsset("native/${url.lastPathSegment}") + path.contains("/native/") -> webView.context.loadAsset("native/${url.lastPathSegment}") path.endsWith(Constants.CAST_SDK_PATH) -> { // Load the chrome.cast.js library instead webView.context.loadAsset("native/chrome.cast.js") diff --git a/app/src/main/java/org/jellyfin/mobile/utils/Constants.kt b/app/src/main/java/org/jellyfin/mobile/utils/Constants.kt index c6f9565d0c..3f59ba7abe 100644 --- a/app/src/main/java/org/jellyfin/mobile/utils/Constants.kt +++ b/app/src/main/java/org/jellyfin/mobile/utils/Constants.kt @@ -12,7 +12,7 @@ object Constants { // Webapp constants const val MINIMUM_WEB_VIEW_VERSION = 80 - const val WEB_CONFIG_PATH = "config.json" + const val ANY_BUNDLE_PATH = ".bundle.js" const val CAST_SDK_PATH = "cast_sender.js" const val SESSION_CAPABILITIES_PATH = "sessions/capabilities/full" diff --git a/app/src/main/java/org/jellyfin/mobile/utils/WebAppUtils.kt b/app/src/main/java/org/jellyfin/mobile/utils/WebAppUtils.kt index aefd973002..7b6cf96226 100644 --- a/app/src/main/java/org/jellyfin/mobile/utils/WebAppUtils.kt +++ b/app/src/main/java/org/jellyfin/mobile/utils/WebAppUtils.kt @@ -5,27 +5,6 @@ import android.webkit.WebResourceResponse import timber.log.Timber import java.io.IOException -const val JS_INJECTION_CODE = """ -!function() { - if (window.injectedAppJS) { - return; - } - var scripts = [ - '/native/nativeshell.js', - '/native/EventEmitter.js', - ]; - scripts.forEach(function(src) { - var scriptElement = document.createElement('script'); - scriptElement.type = 'text/javascript'; - scriptElement.src = src; - scriptElement.charset = 'utf-8'; - scriptElement.setAttribute('defer', ''); - document.body.appendChild(scriptElement); - }); - window.injectedAppJS = true; -}(); -""" - fun Context.loadAsset(url: String, mimeType: String = "application/javascript"): WebResourceResponse { val data = try { assets.open(url) @@ -35,5 +14,3 @@ fun Context.loadAsset(url: String, mimeType: String = "application/javascript"): } return WebResourceResponse(mimeType, Charsets.UTF_8.name(), data) } - -val emptyResponse = WebResourceResponse("text/html", Charsets.UTF_8.toString(), "".byteInputStream())