最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C# CefSharpSelenium Allowing pop-ups but hiding them - Stack Overflow

programmeradmin1浏览0评论

I'm creating a Library where you can use Puter.js's AI API but on C#, and I'm running into an issue.

Puter has this weird pop-up authentication thingy where you have to manually click "Continue" in order to use the AI, and I want to automate the process of the authentication.

Now, this may sound unethical but I want to remove their pop-up and make it completely automatic where users who use this Library do not have to go through the annoying repeated pop-up in order to use the AI services they include.

I'm trying this out:

    <script>
    window.addEventListener('load', function() {
      window.open('/?embedded_in_popup=true&request_auth=true', 
                  'PuterAuth', 
                  'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=600, height=400');
});
</script>

using CefSharp on a C# .NET Framework Library.

1st Approach I tried

I tried hiding the pop-up coming out of the browser using CefSharp since I want to make it do it's thing but in the background instead of appearing on the user's screen but I've gotten nowhere with that. It seems you can't really do much with CefSharp when it comes to pop-ups except decide whether to close them or keep them open.

2nd Approach I tried (unrelated to CefSharp)

I tried completely switching to Selenium since it's more flexible with it's capabilities so I thought I could find a way to hide the pop-up on it but that didn't work either, ran into many roadblocks including that the HTML code was too long (since I'm not using /, instead I just modified the same code located inside / in order to at least be able to run the authentication automatically instead of waiting for the "Continue" button to be clicked), so that didn't work either.

Current code:

public static class AI
{

    private static ChromiumWebBrowser browser;

    private static void DeleteDirectory(string target_dir)
    {
        string[] files = Directory.GetFiles(target_dir);
        string[] dirs = Directory.GetDirectories(target_dir);

        foreach (string file in files)
        {
            System.IO.File.SetAttributes(file, FileAttributes.Normal);
            System.IO.File.Delete(file);
        }

        foreach (string dir in dirs)
        {
            DeleteDirectory(dir);
        }

        Directory.Delete(target_dir, false);
    }

    public static void AIChat(string chatQuery = "Hey, how are you doing?", string model = "claude", Control targetControl = null)
    {

        var cacheDirBase = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData), "CEF");

        DeleteDirectory(cacheDirBase);
        CefSettings settings = new CefSettings();
        settings.CefCommandLineArgs.Add("disable-web-security", "1");
        settings.CefCommandLineArgs.Add("allow-universal-access-from-files", "1");
        settings.CefCommandLineArgs.Add("disable-popup-blocking", "1");
        settings.RemoteDebuggingPort = 8080;
        Cef.Initialize(settings);

        browser = new ChromiumWebBrowser();
        browser.Dock = DockStyle.Fill;
        browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
        string fullResponse = string.Empty;

        browser.ConsoleMessage += (sender, args) =>
        {
            string message = args.Message;
            if (message.Contains("Bridge not ready!"))
            {
                return;
            }
            if (message.StartsWith("Received part: "))
            {
                message = message.Substring("Received part: ".Length);
            }

            fullResponse += message;

            Console.WriteLine(fullResponse);
        };
        targetControl.Controls.Add(browser);

        string html = "Way too long to be sent here";

        browser.LoadHtml(html, "http://localhost:8081");

        }
    }
}

And at this point (after working over 24 hours on this getting nowhere with trying to hide the Puter authentication pop-up through both js code in the html string and C# code using CefSharp or Windows APIs) I cant help but ask for some spoonfeeding. I'm not knowledgeable enough on this topic and overall what I'm doing but I still want to make it work.

发布评论

评论列表(0)

  1. 暂无评论