Skip to content

Commit

Permalink
Improve JS injection method
Browse files Browse the repository at this point in the history
Co-authored-by: Niels van Velzen <[email protected]>
  • Loading branch information
CarlosOlivo and nielsvanvelzen committed Jun 18, 2021
1 parent 7dbf085 commit d334116
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 52 deletions.
15 changes: 15 additions & 0 deletions app/src/main/assets/native/injectionScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(() => {
const scripts = [
'/native/nativeshell.js',
'/native/EventEmitter.js',
document.currentScript.src.concat('?', Date.now())
];
for (const script of scripts) {
const scriptElement = document.createElement('script');
scriptElement.src = script;
scriptElement.charset = 'utf-8';
scriptElement.setAttribute('defer', '');
document.head.appendChild(scriptElement);
}
document.currentScript.remove();
})();
19 changes: 7 additions & 12 deletions app/src/main/java/org/jellyfin/mobile/fragment/WebViewFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.fragment.app.add
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.webkit.WebResourceErrorCompat
import androidx.webkit.WebViewAssetLoader.AssetsPathHandler
import androidx.webkit.WebViewClientCompat
import androidx.webkit.WebViewCompat
import androidx.webkit.WebViewFeature
Expand All @@ -43,14 +44,12 @@ 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
import org.jellyfin.mobile.utils.dip
import org.jellyfin.mobile.utils.initLocale
import org.jellyfin.mobile.utils.isOutdated
import org.jellyfin.mobile.utils.loadAsset
import org.jellyfin.mobile.utils.replaceFragment
import org.jellyfin.mobile.utils.requestNoBatteryOptimizations
import org.jellyfin.mobile.utils.runOnUiThread
Expand All @@ -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
Expand Down Expand Up @@ -157,18 +156,14 @@ 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()
AssetsPathHandler(context).handle("native/injectionScript.js")
}
path.contains("native") -> webView.context.loadAsset("native/${url.lastPathSegment}")
path.contains("/native/") -> AssetsPathHandler(context).handle("native/${url.lastPathSegment}")
path.endsWith(Constants.CAST_SDK_PATH) -> {
// Load the chrome.cast.js library instead
webView.context.loadAsset("native/chrome.cast.js")
AssetsPathHandler(context).handle("native/chrome.cast.js")
}
path.endsWith(Constants.SESSION_CAPABILITIES_PATH) -> {
lifecycleScope.launch {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/jellyfin/mobile/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
39 changes: 0 additions & 39 deletions app/src/main/java/org/jellyfin/mobile/utils/WebAppUtils.kt

This file was deleted.

0 comments on commit d334116

Please sign in to comment.