如何使用 HitTestResult 检测 Android WebView 中的视频?

问题描述 投票:0回答:2

就像我们使用

hitTestResult.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE
检测锚文本一样,如何检测 Facebook 上播放的视频?

我想知道视频文件的

hitTestResult.GetExtra()
,然后搜索其有效扩展名 (.mp4),然后下载该文件。

android facebook webview android-webview
2个回答
10
投票

据我了解,根据您的描述,您在

WebView
中加载您的 FB 页面,并且想要下载 .mp4 视频(如果有)。这段代码将为您完成

@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();
    }
}

0
投票

没有登录就无法下载 Facebook 视频

© www.soinside.com 2019 - 2024. All rights reserved.