2022-08-15 23:24:19 +00:00
|
|
|
package com.lagradost.cloudstream3.ui
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2022-09-10 17:33:30 +00:00
|
|
|
import android.webkit.JavascriptInterface
|
2022-08-15 23:24:19 +00:00
|
|
|
import android.webkit.WebResourceRequest
|
|
|
|
import android.webkit.WebView
|
|
|
|
import android.webkit.WebViewClient
|
2022-08-21 18:14:26 +00:00
|
|
|
import androidx.fragment.app.Fragment
|
2022-09-10 17:33:30 +00:00
|
|
|
import androidx.fragment.app.FragmentActivity
|
2022-08-15 23:24:19 +00:00
|
|
|
import androidx.navigation.fragment.findNavController
|
|
|
|
import com.lagradost.cloudstream3.R
|
2022-08-21 18:14:26 +00:00
|
|
|
import com.lagradost.cloudstream3.network.WebViewResolver
|
2022-08-16 22:17:50 +00:00
|
|
|
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringRepo
|
2022-08-15 23:24:19 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.AppUtils.loadRepository
|
|
|
|
import kotlinx.android.synthetic.main.fragment_webview.*
|
2022-08-16 22:17:50 +00:00
|
|
|
import java.net.URI
|
2022-08-15 23:24:19 +00:00
|
|
|
|
|
|
|
class WebviewFragment : Fragment() {
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
val url = arguments?.getString(WEBVIEW_URL) ?: "".also {
|
|
|
|
findNavController().popBackStack()
|
|
|
|
}
|
|
|
|
|
|
|
|
web_view.webViewClient = object : WebViewClient() {
|
|
|
|
override fun shouldOverrideUrlLoading(
|
|
|
|
view: WebView?,
|
|
|
|
request: WebResourceRequest?
|
|
|
|
): Boolean {
|
|
|
|
val requestUrl = request?.url.toString()
|
2022-08-16 22:17:50 +00:00
|
|
|
val repoUrl = if (requestUrl.startsWith("https://cs.repo")) {
|
|
|
|
"https://" + requestUrl.substringAfter("?")
|
|
|
|
} else if (URI(requestUrl).scheme == appStringRepo) {
|
2022-08-20 01:06:35 +00:00
|
|
|
requestUrl.replaceFirst(appStringRepo, "https")
|
2022-08-16 22:17:50 +00:00
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repoUrl != null) {
|
|
|
|
activity?.loadRepository(repoUrl)
|
2022-08-15 23:24:19 +00:00
|
|
|
findNavController().popBackStack()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.shouldOverrideUrlLoading(view, request)
|
|
|
|
}
|
|
|
|
}
|
2022-09-10 17:33:30 +00:00
|
|
|
web_view.addJavascriptInterface(RepoApi(activity), "RepoApi")
|
2022-08-15 23:24:19 +00:00
|
|
|
web_view.settings.javaScriptEnabled = true
|
|
|
|
web_view.settings.domStorageEnabled = true
|
2022-08-21 18:14:26 +00:00
|
|
|
|
|
|
|
WebViewResolver.webViewUserAgent = web_view.settings.userAgentString
|
|
|
|
// web_view.settings.userAgentString = USER_AGENT
|
2022-08-15 23:24:19 +00:00
|
|
|
web_view.loadUrl(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
// Inflate the layout for this fragment
|
|
|
|
return inflater.inflate(R.layout.fragment_webview, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val WEBVIEW_URL = "webview_url"
|
|
|
|
fun newInstance(webViewUrl: String) =
|
|
|
|
Bundle().apply {
|
|
|
|
putString(WEBVIEW_URL, webViewUrl)
|
|
|
|
}
|
|
|
|
}
|
2022-09-10 17:33:30 +00:00
|
|
|
|
|
|
|
private class RepoApi(val activity: FragmentActivity?) {
|
|
|
|
@JavascriptInterface
|
|
|
|
fun installRepo(repoUrl: String) {
|
|
|
|
activity?.loadRepository(repoUrl)
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 23:24:19 +00:00
|
|
|
}
|