Lazarus

Programming => General => Topic started by: Ericktux on December 07, 2022, 05:59:44 am

Title: how to use "user agent" in webbrowser (IE) mobile mode
Post by: Ericktux on December 07, 2022, 05:59:44 am
hello brothers I found this example of webbrowser (IE) called "myBrowser.zip" in the following link
https://www.lazarusforum.de/viewtopic.php?f=55&t=7407

At the moment it works and compiles perfectly, it's very basic, just what I needed.

but I would like to modify the "user agent" to be able to emulate the mobile mode of the web pages. The problem is that I don't know what to do.

Code: Pascal  [Select][+][-]
  1. 'User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53'

I need your help please.  :(

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, activexcontainer, Forms, Controls, Graphics,
  9.   Dialogs, StdCtrls, ExtCtrls, SHDocVw_1_1_TLB;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ActiveXContainer1: TActiveXContainer;
  17.     btnTest: TButton;
  18.     btnNavigate: TButton;
  19.     btnPrint: TButton;
  20.     btnBack: TButton;
  21.     btnForward: TButton;
  22.     brnRefresh: TButton;
  23.     btnStop: TButton;
  24.     edtUrl: TEdit;
  25.     lblStatus: TLabel;
  26.     pnlFunctions: TPanel;
  27.     pnlStatus: TPanel;
  28.     procedure brnRefreshClick(Sender: TObject);
  29.     procedure btnBackClick(Sender: TObject);
  30.     procedure btnNavigateClick(Sender: TObject);
  31.     procedure btnForwardClick(Sender: TObject);
  32.     procedure btnPrintClick(Sender: TObject);
  33.     procedure btnStopClick(Sender: TObject);
  34.     procedure btnTestClick(Sender: TObject);
  35.     procedure FormCreate(Sender: TObject);
  36.     procedure FormDestroy(Sender: TObject);
  37.   private
  38.     procedure OnStatusTextChange(Sender: TObject; Text_: WideString);
  39.   public
  40.     { public declarations }
  41.   end;
  42.  
  43. var
  44.   Form1: TForm1;
  45.   Browser: TEvsWebBrowser;
  46.  
  47. implementation
  48.  
  49. {$R *.lfm}
  50.  
  51. { TForm1 }
  52.  
  53. procedure TForm1.btnTestClick(Sender: TObject);
  54. var ovMIMEType, ovDocument: Olevariant;
  55. begin
  56.   ovDocument := Browser.ComServer.Document;
  57.   ovMIMEType := 'text/html';
  58.   ovDocument.Open(ovMIMEType);
  59.   ovDocument.Write('<html><body><p>Hello World!</p></body></html>');
  60.   ovDocument.Close();
  61. end;
  62.  
  63. procedure TForm1.btnNavigateClick(Sender: TObject);
  64. var ovNull, ovUrl: OleVariant;
  65. begin
  66.   ovUrl := Utf8decode(edtUrl.Text);
  67.   ovNull := NULL;
  68.   Browser.ComServer.Navigate2(ovUrl, ovNull, ovNull, ovNull, ovNull);
  69. end;
  70.  
  71. procedure TForm1.btnForwardClick(Sender: TObject);
  72. begin
  73.   Browser.ComServer.GoForward;
  74. end;
  75.  
  76. procedure TForm1.btnBackClick(Sender: TObject);
  77. begin
  78.   Browser.ComServer.GoBack;
  79. end;
  80.  
  81. procedure TForm1.brnRefreshClick(Sender: TObject);
  82. begin
  83.   Browser.ComServer.Refresh;
  84. end;
  85.  
  86. procedure TForm1.btnPrintClick(Sender: TObject);
  87. var ovIn, ovOut: OleVariant;
  88. begin
  89.   if (Browser.ComServer.QueryStatusWB(OLECMDID_PRINT) and OLECMDF_ENABLED) = 2 then
  90.     Browser.ComServer.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, ovIn, ovOut);
  91. end;
  92.  
  93. procedure TForm1.btnStopClick(Sender: TObject);
  94. begin
  95.   Browser.ComServer.Stop;
  96. end;
  97.  
  98. procedure TForm1.FormCreate(Sender: TObject);
  99. var ovUrl, ovNull: Olevariant;
  100. begin
  101.   Browser := TEvsWebBrowser.Create(Self);
  102.   ActiveXContainer1.ComServer := Browser.ComServer;
  103.   ActiveXContainer1.Active := true;
  104.   Browser.OnStatusTextChange := @OnStatusTextChange;
  105.   ovUrl := Utf8decode('about:blank');
  106.   ovNull := NULL;
  107.   Browser.ComServer.Navigate2(ovUrl, ovNull, ovNull, ovNull, ovNull);
  108.   Browser.ComServer.Silent := true;
  109. end;
  110.  
  111. procedure TForm1.FormDestroy(Sender: TObject);
  112. begin
  113.   Browser.Free;
  114. end;
  115.  
  116. procedure TForm1.OnStatusTextChange(Sender: TObject; Text_: WideString);
  117. begin
  118.   lblStatus.Caption:='Browser: '+UTF8Encode(Text_);
  119. end;
  120.  
  121. end.

I attach the project and main code
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: KodeZwerg on December 07, 2022, 10:35:53 am
Doing this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnNavigateClick(Sender: TObject);
  2. var ovNull, ovUrl, ovHeader: OleVariant;
  3.     s: AnsiString;
  4. begin
  5.   ovUrl := Utf8decode(edtUrl.Text);
  6.   ovNull := NULL;
  7.   s := 'User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53'#0#0;
  8.   ovHeader := PAnsiChar(s);
  9.   Browser.ComServer.Navigate2(ovUrl, ovNull, ovNull, ovNull, ovHeader);
  10. end;
End up like i show in attachment.
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: KodeZwerg on December 07, 2022, 11:16:27 am
but I would like to modify the "user agent" to be able to emulate the mobile mode of the web pages.
Changing the user agent does not affect how it is rendered.
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: Ericktux on December 07, 2022, 03:53:55 pm
hello friend, thank you very much for your help, it works fine, but the web does not detect the change  :(, for example, I am trying to make an app to download the windows 10 iso from its official website here
https://www.microsoft.com/es-es/software-download/windows10ISO

If I open "Internet explorer" and change its "user agent" it is possible to enter the web in mobile mode and download the iso. but I can't do that with the project.

please help  :( :(

attached image example
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: KodeZwerg on December 07, 2022, 10:45:22 pm
hello friend, thank you very much for your help, it works fine
You are welcome  O:-)
but the web does not detect the change
That I told, it does not change the way of how its rendered.
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: Ericktux on December 07, 2022, 11:02:54 pm
Thank you very much for answering friend, can it be solved or is it so  :-\
If it can't be solved, will you know another simple browser that allows me to do it, friend?
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: KodeZwerg on December 08, 2022, 12:09:35 am
The problem you encounter is, that your try uses the microsoft internet explorer activex control, so internal it will always stay like that.
( even if you see on my above screenshot something different  :-X )
I do know only "real" browsers capable of doing what you try to achieve. (For myself I do happily use Opera for many many years and never want to change.)
Anyway, if I would be you, I would try CEF4Delphi (https://github.com/salvadordf/CEF4Delphi) but for myself, I havent tried that one with your special wish.
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: Ericktux on December 08, 2022, 12:17:57 am
I understand friend, I am aware of the existence of the CEF4delphi project, I did some tests a while ago, it is very good and complete, only it is a bit heavy and has many files compared to the basic project that I uploaded at the beginning.
In any case, I appreciate your help and sincerity my friend, if I find another simple way I will share it here, regards  :)
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: KodeZwerg on December 08, 2022, 12:49:03 am
Ok my friend, here you have a working solution. Now I force the control to do what you want to do.  8-)
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: Ericktux on December 08, 2022, 01:35:44 am
wow, thanks very much my friend, now it works fine  :)
Title: Re: how to use "user agent" in webbrowser (IE) mobile mode
Post by: KodeZwerg on December 08, 2022, 03:43:00 am
You are welcome and I need to say, take care, my forced way also affect any other browser on your system for current login session to windows.
TinyPortal © 2005-2018