How to detect video in Android WebView with HitTestResult?

TheOnlyAnil

Like we detect anchor text using hitTestResult.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE, How can I detect a video playing on Facebook?

I want to know the hitTestResult.GetExtra() for a video file and then search for its valid extension (.mp4) and then download the file.

Rainmaker

So as I understand based on what you described you load your FB page in a WebView and want to download .mp4 videos if there is one. This code will do it for you

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webView = (WebView)findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.addJavascriptInterface(this, "FBDownloader");
    webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url)
            {
                if (swipeLayout.isRefreshing())
                {
                    swipeLayout.setRefreshing(false);
                }

                webView.loadUrl("javascript:(function() { "
                                + "var el = document.querySelectorAll('div[data-sigil]');"
                                + "for(var i=0;i<el.length; i++)"
                                + "{"
                                + "var sigil = el[i].dataset.sigil;"
                                + "if(sigil.indexOf('inlineVideo') > -1){"
                                + "delete el[i].dataset.sigil;"
                                + "var jsonData = JSON.parse(el[i].dataset.store);"
                                + "el[i].setAttribute('onClick', 'FBDownloader.processVideo(\"'+jsonData['src']+'\");');"
                                + "}" + "}" + "})()");
            }

            @Override
            public void onLoadResource(WebView view, String url)
            {
                webView.loadUrl("javascript:(function prepareVideo() { "
                                + "var el = document.querySelectorAll('div[data-sigil]');"
                                + "for(var i=0;i<el.length; i++)"
                                + "{"
                                + "var sigil = el[i].dataset.sigil;"
                                + "if(sigil.indexOf('inlineVideo') > -1){"
                                + "delete el[i].dataset.sigil;"
                                + "console.log(i);"
                                + "var jsonData = JSON.parse(el[i].dataset.store);"
                                + "el[i].setAttribute('onClick', 'FBDownloader.processVideo(\"'+jsonData['src']+'\",\"'+jsonData['videoID']+'\");');"
                                + "}" + "}" + "})()");
                webView.loadUrl("javascript:( window.onload=prepareVideo;"
                                + ")()");
            }
        });

    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    CookieSyncManager.getInstance().startSync();

    webView.loadUrl(target_url);
}

@JavascriptInterface
public void processVideo(final String vidData, final String vidID)
{
    try
    {
        String mBaseFolderPath = android.os.Environment
            .getExternalStorageDirectory()
            + File.separator
            + "FacebookVideos" + File.separator;
        if (!new File(mBaseFolderPath).exists())
        {
            new File(mBaseFolderPath).mkdir();
        }
        String mFilePath = "file://" + mBaseFolderPath + "/" + vidID + ".mp4";

        Uri downloadUri = Uri.parse(vidData);
        DownloadManager.Request req = new DownloadManager.Request(downloadUri);
        req.setDestinationUri(Uri.parse(mFilePath));
        req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        DownloadManager dm = (DownloadManager) getSystemService(getApplicationContext().DOWNLOAD_SERVICE);
        dm.enqueue(req);
        Toast.makeText(this, "Download Started", Toast.LENGTH_LONG).show();
    }
    catch (Exception e)
    {
        Toast.makeText(this, "Download Failed: " + e.toString(), Toast.LENGTH_LONG).show();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to show video in webView on Android

How to detect button click in WebView Android?

Detect rotation on Android WebView

How to detect if using Android Browser or Android Native WebView Client

How to get android webview url to auto detect https http and www?

How to detect 412 precondition failed errors in android webview url

How to change activity when an url is detect in webview Android

Detect URL changes in Webview android

Detect a blank webpage in WebView android

Android How To Get Playing Video Url From WebView

How to play a video in fullscreen mode and exit fullscreen mode in Android WebView?

How to clear Webview content to change the video in Android Oreo

How to detect HLS video is for Live, VOD or Event in Exoplayer android

WebView video not playing in Android 2.3

React Native Android Webview Video

Android: playing youtube video on Webview

Play video in Android webview - Video controls not visible

How to place a Renderable to a HitTestResult getPoint() location

How to detect single Tap on WebView

How to detect webview scrolled to bottom?

How to detect faces from video frames in real time while recording video in a service in android?

How to stop video playing in WebView

Detect when a webview video becomes fullscreen on ios8

How is video playing on Chrome different from the Android WebView on Android 7+?

Cannot play Vimeo video inside Android webview

WebView NOT opening android default video player?

Making an embed video full screen in android webview

Android video WebView FullScreen not working - onShowCustomView() not called

Android webview don`t display video