Load the url from the exception in the ReCaptchaActivity

Sometimes YouTube introduces recaptchas only on some pages. By loading the url inside the ReCaptchaException into ReCaptchaActivity's webview, the page that originally caused the problem is shown. The user can then solve the page-specific recaptcha.
This commit is contained in:
Stypox 2019-08-17 09:30:42 +02:00
parent e66f2ab36b
commit 43446d56c5
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
2 changed files with 16 additions and 8 deletions

View file

@ -37,15 +37,24 @@ import android.webkit.WebViewClient;
*/
public class ReCaptchaActivity extends AppCompatActivity {
public static final int RECAPTCHA_REQUEST = 10;
public static final String RECAPTCHA_URL_EXTRA = "recaptcha_url_extra";
public static final String TAG = ReCaptchaActivity.class.toString();
public static final String YT_URL = "https://www.youtube.com";
private String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recaptcha);
url = getIntent().getStringExtra(RECAPTCHA_URL_EXTRA);
if (url == null || url.isEmpty()) {
url = YT_URL;
}
// Set return to Cancel by default
setResult(RESULT_CANCELED);
@ -73,15 +82,12 @@ public class ReCaptchaActivity extends AppCompatActivity {
myWebView.clearHistory();
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
@Override
public void onReceiveValue(Boolean aBoolean) {}
});
cookieManager.removeAllCookies(aBoolean -> {});
} else {
cookieManager.removeAllCookie();
}
myWebView.loadUrl(YT_URL);
myWebView.loadUrl(url);
}
private class ReCaptchaWebViewClient extends WebViewClient {

View file

@ -180,7 +180,7 @@ public abstract class BaseStateFragment<I> extends BaseFragment implements ViewC
}
if (exception instanceof ReCaptchaException) {
onReCaptchaException();
onReCaptchaException((ReCaptchaException) exception);
return true;
} else if (exception instanceof IOException) {
showError(getString(R.string.network_error), true);
@ -190,11 +190,13 @@ public abstract class BaseStateFragment<I> extends BaseFragment implements ViewC
return false;
}
public void onReCaptchaException() {
public void onReCaptchaException(ReCaptchaException exception) {
if (DEBUG) Log.d(TAG, "onReCaptchaException() called");
Toast.makeText(activity, R.string.recaptcha_request_toast, Toast.LENGTH_LONG).show();
// Starting ReCaptcha Challenge Activity
startActivityForResult(new Intent(activity, ReCaptchaActivity.class), ReCaptchaActivity.RECAPTCHA_REQUEST);
Intent intent = new Intent(activity, ReCaptchaActivity.class);
intent.putExtra(ReCaptchaActivity.RECAPTCHA_URL_EXTRA, exception.getUrl());
startActivityForResult(intent, ReCaptchaActivity.RECAPTCHA_REQUEST);
showError(getString(R.string.recaptcha_request_toast), false);
}