Try to extract cookies just before closing recaptcha activity

Even if the page didn't auto-close
This commit is contained in:
Stypox 2020-02-01 17:59:16 +01:00
parent 0cc890a1d1
commit 9b09028440
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23

View file

@ -47,6 +47,7 @@ public class ReCaptchaActivity extends AppCompatActivity {
public static final String TAG = ReCaptchaActivity.class.toString(); public static final String TAG = ReCaptchaActivity.class.toString();
public static final String YT_URL = "https://www.youtube.com"; public static final String YT_URL = "https://www.youtube.com";
private WebView webView;
private String foundCookies = ""; private String foundCookies = "";
@Override @Override
@ -66,23 +67,23 @@ public class ReCaptchaActivity extends AppCompatActivity {
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
WebView myWebView = findViewById(R.id.reCaptchaWebView); webView = findViewById(R.id.reCaptchaWebView);
// Enable Javascript // Enable Javascript
WebSettings webSettings = myWebView.getSettings(); WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() { webView.setWebViewClient(new WebViewClient() {
@Override @Override
public void onPageFinished(WebView view, String url) { public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url); super.onPageFinished(view, url);
handleCookies(CookieManager.getInstance().getCookie(url)); handleCookies(url);
} }
}); });
// Cleaning cache, history and cookies from webView // Cleaning cache, history and cookies from webView
myWebView.clearCache(true); webView.clearCache(true);
myWebView.clearHistory(); webView.clearHistory();
android.webkit.CookieManager cookieManager = CookieManager.getInstance(); android.webkit.CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(aBoolean -> {}); cookieManager.removeAllCookies(aBoolean -> {});
@ -90,7 +91,7 @@ public class ReCaptchaActivity extends AppCompatActivity {
cookieManager.removeAllCookie(); cookieManager.removeAllCookie();
} }
myWebView.loadUrl(url); webView.loadUrl(url);
} }
@Override @Override
@ -125,6 +126,7 @@ public class ReCaptchaActivity extends AppCompatActivity {
} }
private void saveCookiesAndFinish() { private void saveCookiesAndFinish() {
handleCookies(webView.getUrl()); // try to get cookies of unclosed page
if (!foundCookies.isEmpty()) { if (!foundCookies.isEmpty()) {
// Give cookies to Downloader class // Give cookies to Downloader class
DownloaderImpl.getInstance().setCookies(foundCookies); DownloaderImpl.getInstance().setCookies(foundCookies);
@ -138,8 +140,10 @@ public class ReCaptchaActivity extends AppCompatActivity {
private void handleCookies(@Nullable String cookies) { private void handleCookies(String url) {
if (MainActivity.DEBUG) Log.d(TAG, "handleCookies: cookies=" + (cookies == null ? "null" : cookies)); String cookies = CookieManager.getInstance().getCookie(url);
if (MainActivity.DEBUG) Log.d(TAG, "handleCookies: url=" + url + "; cookies=" + (cookies == null ? "null" : cookies));
Log.e(TAG, "handleCookies: url=" + url + "; cookies=" + (cookies == null ? "null" : cookies));
if (cookies == null) return; if (cookies == null) return;
addYoutubeCookies(cookies); addYoutubeCookies(cookies);