Is it possible to check if a WebView is playing a Video at the moment and if yes to get the http://... .mp4/ URL of this Video to download?
I already have tried this
You can detect whether video is playing using JavaScript.
Create a .js
file in the assets
directory and declare a JavaScript function and jQuery like this:
$("video").on("play", function() {
//You can call JavaScriptInterface here.
});
Insert this .js
file in web page that contains video. I inserted .js
file when onPageFinished
is called.
view?.context?.assets?.let {
val inputStream: InputStream = it.open(scriptFile)
val buffer = ByteArray(inputStream.available())
inputStream.read(buffer)
inputStream.close()
val encoded = Base64.encodeToString(buffer, Base64.NO_WRAP)
view.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.innerHTML = window.atob('$encoded');" +
"parent.appendChild(script)" +
"})()")
If you are not familiar with Kotlin, check this reference: Android Web-View : Inject local Javascript file to Remote Webpage
Done! If you use JavaScriptInterface
, you can do whatever you want, like this.
class WebScriptInterface(val button: View) {
@JavascriptInterface
fun hideFloatingButton() {
if (button.visibility == View.VISIBLE) {
button.visibility = View.GONE
}
}
}
Set webView
use JavaScriptInterface
.
webView.addJavascriptInterface
(WebScriptInterface(binding.floatBrowserCollect), "App")
And then call.
$("video").on("play", function() {
App.hideFloatingButton()
});
I hope it can help you.