Page 1 of 1

PROXY

Posted: Thu Dec 09, 2021 4:39 pm
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;

Re: PROXY

Posted: Fri Dec 10, 2021 8:37 am
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.

Re: PROXY

Posted: Fri Dec 10, 2021 3:03 pm
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?

Re: PROXY

Posted: Sat Dec 11, 2021 9:09 am
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

Re: PROXY

Posted: Mon Dec 13, 2021 9:02 am
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;



Re: PROXY

Posted: Sat Dec 18, 2021 9:21 am
by salvadordf
TChromiumCore.UpdatePreferences is asynchronous. Use the TChromiumCore.OnPrefsUpdated event to know when the preferences have been updated.