Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
2
Indexable
Never
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private SharedPreferences sharedPref;

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

        sharedPref = getPreferences(Context.MODE_PRIVATE);

        // Create a WebView and load https://chat.openai.com/
        webView = findViewById(R.id.webView);
        webView.loadUrl("https://chat.openai.com/");

        // Enable cookies for the WebView
        CookieManager.getInstance().setAcceptCookie(true);
        CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);

        // Enable JavaScript for the WebView
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Intercept the login form submission and save the login details to SharedPreferences
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                if (url.contains("https://chat.openai.com/login")) {
                    String email = "example@email.com";
                    String password = "password123";

                    // Save the login details to SharedPreferences
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putString("email", email);
                    editor.putString("password", password);
                    editor.apply();

                    // Submit the login form programmatically
                    webView.evaluateJavascript("javascript:(function(){" +
                            "document.getElementsByName('email')[0].value='" + email + "';" +
                            "document.getElementsByName('password')[0].value='" + password + "';" +
                            "document.getElementsByName('login')[0].click();" +
                            "})()", null);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Retrieve the saved login details from SharedPreferences and submit the login form programmatically
        String email = sharedPref.getString("email", "");
        String password = sharedPref.getString("password", "");

        if (!email.isEmpty() && !password.isEmpty()) {
            webView.evaluateJavascript("javascript:(function(){" +
                    "document.getElementsByName('email')[0].value='" + email + "';" +
                    "document.getElementsByName('password')[0].value='" + password + "';" +
                    "document.getElementsByName('login')[0].click();" +
                    "})()", null);
        }
    }
}