Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

PROXY

Post Reply
amiran6543
Posts: 15
Joined: Sun Dec 05, 2021 12:07 pm

PROXY

Post by amiran6543 »

Hello, there is a problem, there is a form on which there is a checkbox that disables or enables proxies, the problem is that proxies are enabled but after unchecking they are not disabled!.

Code: Select all

begin
if CheckBox1.Checked = false then
            begin
             TempCache := GlobalCEFApp.RootCache + '\root_cache\user1';
                 TempContext := TCefRequestContextRef.New(TempCache, '', '', False, False, False) ;
             Chromium1.LoadURL(edit1.text);
              Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext);
                Chromium1.UpdatePreferences;
            end;
          if CheckBox1.Checked = true then
            begin
                Chromium1.ProxyType := CEF_PROXYTYPE_FIXED_SERVERS;
             Chromium1.ProxyScheme := psSOCKS5;
              Chromium1.ProxyServer := form2.edit1.text;
             Chromium1.ProxyPort := StrToInt(form2.edit2.text);
              Chromium1.ProxyUsername := '';
              Chromium1.ProxyPassword := '';
             TempCache := GlobalCEFApp.RootCache + '\root_cache\user1';
                 TempContext := TCefRequestContextRef.New(TempCache, '', '', False, False, False) ;
             Chromium1.LoadURL(edit1.text);
              Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext);
                Chromium1.UpdatePreferences;
            end;

end;
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: PROXY

Post by salvadordf »

The MiniBrowser demo has all the code to set or remove the proxy settings here :
https://github.com/salvadordf/CEF4Delphi/blob/6393b6f480725b1b279e944e5c26540bcf8c9192/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L1509

Many TChromium methods and properties will only work when the browser is fully initialized and ready to take commands.

TChromium.CreateBrowser is an asynchronous function and a few milliseconds later the TChromium.OnAfterCreated event is triggered.

You can call LoadURL and UpdatePreferences only after the OnAfterCreated event was triggered because the browser is fully initialized after that moment.
amiran6543
Posts: 15
Joined: Sun Dec 05, 2021 12:07 pm

Re: PROXY

Post by amiran6543 »

I didn't quite understand what you meant, is there some property to disable the proxy or is it done in some other way?
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: PROXY

Post by salvadordf »

Follow these steps :
  • Create the form with the browser
  • Use the TForm.OnCreate to set Chromium1.DefaultURL to the first address you want to navigate when the browser is fully initialized.
  • Use the TForm.OnCreate to set the default proxy settings : ProxyType, ProxyScheme, ProxyServer, etc
  • Call Chromium1.CreateBrowser
  • Wait until Chromium1.OnAfterCreated is triggered
  • If the user wants to navigate to another website he can do it now, after Chromium1.OnAfterCreated is triggered, with a call to Chromium1.LoadURL
  • If you need to change the proxy settings again do it after the Chromium1.OnAfterCreated event is triggered and call Chromium1.UpdatePreferences afterwards.
If you want to use a direct connection without a proxy server then set Chromium1.ProxyType to CEF_PROXYTYPE_DIRECT
amiran6543
Posts: 15
Joined: Sun Dec 05, 2021 12:07 pm

Re: PROXY

Post by amiran6543 »

I got it thanks!, but I can't understand why he doesn't want to apply these settings to another chromium component?.

Code: Select all

     case Chromium1.ProxyScheme of
    psSOCKS4 : PreferencesFrm.ProxySchemeCb.ItemIndex := 1;
    psSOCKS5 : PreferencesFrm.ProxySchemeCb.ItemIndex := 2;
    else       PreferencesFrm.ProxySchemeCb.ItemIndex := 0;
  end;

    case Chromium3.ProxyScheme of
    psSOCKS4 : PreferencesFrm.ProxySchemeCb2.ItemIndex := 1;
    psSOCKS5 : PreferencesFrm.ProxySchemeCb2.ItemIndex := 2;
    else       PreferencesFrm.ProxySchemeCb2.ItemIndex := 0;
  end;

  PreferencesFrm.ProxyTypeCbx.ItemIndex           := Chromium1.ProxyType;
  PreferencesFrm.ProxyServerEdt.Text              := Chromium1.ProxyServer;
  PreferencesFrm.ProxyPortEdt.Text                := inttostr(Chromium1.ProxyPort);

  PreferencesFrm.ProxyTypeCbx2.ItemIndex           := Chromium3.ProxyType;
  PreferencesFrm.ProxyServerEdt2.Text              := Chromium3.ProxyServer;
  PreferencesFrm.ProxyPortEdt2.Text                := inttostr(Chromium3.ProxyPort);

  if (PreferencesFrm.ShowModal = mrOk) then
    begin
      Chromium1.ProxyType              := PreferencesFrm.ProxyTypeCbx.ItemIndex;
      Chromium1.ProxyServer            := PreferencesFrm.ProxyServerEdt.Text;
      Chromium1.ProxyPort              := strtoint(PreferencesFrm.ProxyPortEdt.Text);

      Chromium3.ProxyType              := PreferencesFrm.ProxyTypeCbx2.ItemIndex;
      Chromium3.ProxyServer            := PreferencesFrm.ProxyServerEdt2.Text;
      Chromium3.ProxyPort              := strtoint(PreferencesFrm.ProxyPortEdt2.Text);

      case PreferencesFrm.ProxySchemeCb.ItemIndex of
        1  : Chromium1.ProxyScheme := psSOCKS4;
        2  : Chromium1.ProxyScheme := psSOCKS5;
        else Chromium1.ProxyScheme := psHTTP;
      end;
       case PreferencesFrm.ProxySchemeCb2.ItemIndex of
        1  : Chromium3.ProxyScheme := psSOCKS4;
        2  : Chromium3.ProxyScheme := psSOCKS5;
        else Chromium3.ProxyScheme := psHTTP;
      end;

      Chromium1.UpdatePreferences;
      Chromium3.UpdatePreferences;


User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: PROXY

Post by salvadordf »

TChromiumCore.UpdatePreferences is asynchronous. Use the TChromiumCore.OnPrefsUpdated event to know when the preferences have been updated.
Post Reply