Skip to content

Commit

Permalink
Improve JS injection method
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosOlivo committed Jun 17, 2021
1 parent 7dbf085 commit 69163d0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
16 changes: 16 additions & 0 deletions app/src/main/assets/native/injectionScript.js
Original file line number Diff line number Diff line change
@@ -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();
}();
15 changes: 5 additions & 10 deletions app/src/main/java/org/jellyfin/mobile/fragment/WebViewFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
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,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")
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
23 changes: 0 additions & 23 deletions app/src/main/java/org/jellyfin/mobile/utils/WebAppUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())

0 comments on commit 69163d0

Please sign in to comment.