Page 1 of 1

Bug? And is it possible to get around it?

Posted: Fri Dec 06, 2019 10:48 am
by igor666
Using old cef4delphi, something strange happens when replacing the User-Agent. I will explain with the example of simplebrower2 and the site youtube.com.
User-Agent spoofing code:

Code: Select all

procedure TForm1.Chromium1BeforeResourceLoad(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame;
  const request: ICefRequest; const callback: ICefRequestCallback;
  out Result: TCefReturnValue);
var
  header, header_new: ICefStringMultimap;
  i: NativeUInt;
  uaset: boolean;
begin
  header := TCefStringMultimapOwn.Create;
  header_new := TCefStringMultimapOwn.Create;
  request.GetHeaderMap(header);

  for i := 0 to header.Size - 1 do
  begin
    if AnsiLowerCase(header.Key[i]) = 'user-agent' then
    begin
      header_new.Append(header.Key[i],'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36');
      uaset := true;
    end
    else
      header_new.Append(header.Key[i],header.Value[i]);
  end;

  if not uaset then
    header_new.Append('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36');

  request.SetHeaderMap(header_new);
end;
When you download youtube, I get what is lower in the ATTACHMENT. And when you try to install any Chrome Windows User-Agent, youtube does not load. But if you substitute the User-Agent of any other browser or Chrome, but mobile, for example, then there are no problems. And if you substitute the native User-Agent - "Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 49.0.2623.110 Safari / 537.36", then there are no problems either, everything loads fine. The problem is pronounced precisely on the youtube website, most of the other sites load normally during this substitution.
Could you help to understand what this may be connected with and how to fix it? Thanks in advance.

Re: Bug? And is it possible to get around it?

Posted: Fri Dec 06, 2019 11:02 am
by salvadordf
YouTube and many other websites send different contents depending on the browser used to render their pages, operating system, preferred language, user apparent location, available browser features, etc.

That's how those websites are designed and in this case you can only use the user-agent string that works best.

Re: Bug? And is it possible to get around it?

Posted: Fri Dec 06, 2019 12:33 pm
by igor666
Ok, thanks for the answer.