在 WebView 中从 Facebook 获取视频网址 [英] Get video url from Facebook in WebView
本文介绍了在 WebView 中从 Facebook 获取视频网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从 Facebook 下载视频.为了下载视频,我需要来自 Webview
的视频 url,我正在加载 Facebook 页面并让用户登录我尝试了以下方法来实现这一目标,但没有任何效果
I need to download video from Facebook. In order to download video i need video url from Webview
where i am loading the Facebook Page and user get login
I have tried following approaches to achieve this but none of its work
1.链接 1
2.链接 2
我试图从 webview 中的 html 获取视频标签,但它没有用,因为当我们点击 webview 上的视频时,我们无法获取任何 html 字符串
I tried to get video tag from html in webview but it didnt work because when we click on video on webview we are not able to get any html string
以下是我目前在 WebViewActivity
public class WebViewActivity extends BaseActivity {
private static final String TAG = WebViewActivity.class.getSimpleName();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.setWebChromeClient(new WebChromeClient());
WebViewClient client = new ChildBrowserClient();
webview.setWebViewClient(client);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setUseWideViewPort(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setBuiltInZoomControls(true);
settings.setPluginState(WebSettings.PluginState.ON);
settings.setDomStorageEnabled(true);
webview.loadUrl("http://m.facebook.com");
webview.setId(5);
webview.setInitialScale(0);
webview.requestFocus();
webview.requestFocusFromTouch();
setContentView(webview);
}
public class ChildBrowserClient extends WebViewClient {
@SuppressLint("InlinedApi")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean value = true;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
String mimeType = mime.getMimeTypeFromExtension(extension);
if (mimeType != null) {
if (mimeType.toLowerCase().contains("video")
|| extension.toLowerCase().contains("mov")
|| extension.toLowerCase().contains("mp4")) {
DownloadManager mdDownloadManager = (DownloadManager) WebViewActivity.this
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
File destinationFile = new File(
Environment.getExternalStorageDirectory(),
getFileName(url));
request.setDescription("Downloading via Your app name..");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(destinationFile));
mdDownloadManager.enqueue(request);
value = false;
}
}
if (value) {
view.loadUrl(url);
}
}
return value;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
/**
* Notify the host application that a page has started loading.
*
* @param view The webview initiating the callback.
* @param url The url of the page.
*/
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
}
/**
* File name from URL
*
* @param url
* @return
*/
public String getFileName(String url) {
String filenameWithoutExtension = "";
filenameWithoutExtension = String.valueOf(System.currentTimeMillis()
+ ".mp4");
return filenameWithoutExtension;
}
}
任何帮助将不胜感激!
推荐答案
@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();
}
}
这篇关于在 WebView 中从 Facebook 获取视频网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文