Skip to content

Commit cfde1c3

Browse files
committed
Improvements: opening links in external browsers and userscript support
1 parent 4f7117e commit cfde1c3

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

Source/Config.ini

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[Main]
2+
Title=Test App
3+
# Application folder address - "%FULLPATH%/", clear the value of "File" if you want to load the website.
4+
File=%FULLPATH%/index.html
5+
UserAgent=
6+
URL=https://google.ru
7+
# Opening links with "_blank" attribute in external browsers
8+
OpenExternalLinks=1
9+
# Example of the path - "%FULLPATH%/UserScript.js"
10+
UserScript=
11+
12+
[Window]
13+
Width=640
14+
Height=360
15+
SaveSize=0
16+
17+
# None = 0, Sizeable = 1, Single = 2, Dialog = 3, SizeToolWin = 4, ToolWindow = 5
18+
BorderStyle=1
19+
HideMaximize=0
20+
21+
# Normal = 0, Maximized = 1, FullScreen = 2
22+
WindowState=0
23+
StayOnTop=0
24+
25+
# Position
26+
Top=
27+
Left=
28+
SavePos=0

Source/Unit1.dfm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ object Main: TMain
22
Left = 0
33
Top = 0
44
Caption = 'Main'
5-
ClientHeight = 468
6-
ClientWidth = 796
5+
ClientHeight = 480
6+
ClientWidth = 640
77
Color = clBtnFace
88
Font.Charset = DEFAULT_CHARSET
99
Font.Color = clWindowText
@@ -17,11 +17,15 @@ object Main: TMain
1717
object EdgeBrowser: TEdgeBrowser
1818
Left = 0
1919
Top = 0
20-
Width = 796
21-
Height = 468
20+
Width = 640
21+
Height = 480
2222
Align = alClient
2323
TabOrder = 0
2424
UserDataFolder = '%LOCALAPPDATA%\bds.exe.WebView2'
2525
OnCreateWebViewCompleted = EdgeBrowserCreateWebViewCompleted
26+
OnNavigationCompleted = EdgeBrowserNavigationCompleted
27+
OnNewWindowRequested = EdgeBrowserNewWindowRequested
28+
ExplicitWidth = 792
29+
ExplicitHeight = 467
2630
end
2731
end

Source/Unit1.pas

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface
44

55
uses
66
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7-
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, WebView2, Winapi.ActiveX, Vcl.Edge, IniFiles;
7+
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, WebView2, Winapi.ActiveX, Vcl.Edge, IniFiles, ShellAPI;
88

99
const // https://stackoverflow.com/questions/66692031/how-to-set-useragent-in-new-delphi-tedgebrowser
1010
IID_ICoreWebView2Settings2: TGUID = '{EE9A0F68-F46C-4E32-AC23-EF8CAC224D2A}';
@@ -23,6 +23,10 @@ TMain = class(TForm)
2323
procedure FormClose(Sender: TObject; var Action: TCloseAction);
2424
procedure EdgeBrowserCreateWebViewCompleted(Sender: TCustomEdgeBrowser;
2525
AResult: HRESULT);
26+
procedure EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
27+
Args: TNewWindowRequestedEventArgs);
28+
procedure EdgeBrowserNavigationCompleted(Sender: TCustomEdgeBrowser;
29+
IsSuccess: Boolean; WebErrorStatus: TOleEnum);
2630
private
2731
{ Private declarations }
2832
public
@@ -34,7 +38,8 @@ TMain = class(TForm)
3438
WinOldWidth, WinOldHeight, WinOldTop, WinOldLeft: integer;
3539
WinSaveSize, WinSavePos: boolean;
3640
EdgeUserAgent: string;
37-
41+
OpenExternalLinks, LoadUserScript: boolean;
42+
UserScriptFile: TStringList;
3843

3944
implementation
4045

@@ -57,6 +62,37 @@ procedure TMain.EdgeBrowserCreateWebViewCompleted(Sender: TCustomEdgeBrowser;
5762
//raise Exception.Create('Get_UserAgent failed');
5863
end;
5964

65+
procedure TMain.EdgeBrowserNavigationCompleted(Sender: TCustomEdgeBrowser;
66+
IsSuccess: Boolean; WebErrorStatus: TOleEnum);
67+
begin
68+
if UserScriptFile.Text <> '' then
69+
EdgeBrowser.ExecuteScript(UserScriptFile.Text);
70+
end;
71+
72+
procedure TMain.EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
73+
Args: TNewWindowRequestedEventArgs);
74+
var
75+
WebViewArgs: ICoreWebView2NewWindowRequestedEventArgs;
76+
PUri: PWideChar;
77+
begin
78+
if OpenExternalLinks = false then Exit;
79+
80+
// Get arguments WebView2
81+
WebViewArgs:=Args as ICoreWebView2NewWindowRequestedEventArgs;
82+
83+
// Get URL
84+
if Succeeded(WebViewArgs.Get_uri(PUri)) and (PUri <> nil) then begin
85+
try
86+
ShellExecute(0, 'open', PUri, nil, nil, SW_SHOWNORMAL);
87+
finally
88+
CoTaskMemFree(PUri);
89+
end;
90+
91+
// Blocking the opening of a new window
92+
Args.ArgsInterface.Set_Handled(1);
93+
end;
94+
end;
95+
6096
procedure TMain.FormClose(Sender: TObject; var Action: TCloseAction);
6197
var
6298
Ini: TIniFile;
@@ -76,20 +112,28 @@ procedure TMain.FormClose(Sender: TObject; var Action: TCloseAction);
76112
Ini.WriteInteger('Window', 'Left', Left);
77113
Ini.Free;
78114
end;
115+
UserScriptFile.Free;
79116
end;
80117

81118
procedure TMain.FormCreate(Sender: TObject);
82119
var
83-
Ini: TIniFile; URL, LocalFile: string;
120+
Ini: TIniFile; URL, LocalFile, FulllPath, UserScriptPath: string;
84121
begin
85-
EdgeBrowser.UserDataFolder := ExtractFilePath(ParamStr(0)) + 'Data';
122+
EdgeBrowser.UserDataFolder:=ExtractFilePath(ParamStr(0)) + 'Data';
86123

87-
Ini:=TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Config.ini');
124+
FulllPath:=ExtractFilePath(ParamStr(0));
125+
Ini:=TIniFile.Create(FulllPath + 'Config.ini');
88126
Main.Caption:=UTF8ToAnsi(Ini.ReadString('Main', 'Title', ''));
89127
LocalFile:=Ini.ReadString('Main', 'File', '');
90-
LocalFile:=StringReplace(LocalFile, '%FULLPATH%/', ExtractFilePath(ParamStr(0)), []);
128+
129+
LocalFile:=StringReplace(LocalFile, '%FULLPATH%/', FulllPath, []);
91130
LocalFile:=StringReplace(LocalFile, '\', '/', [rfReplaceAll]);
92131
EdgeUserAgent:=Ini.ReadString('Main', 'UserAgent', '');
132+
OpenExternalLinks:=Ini.ReadBool('Main', 'OpenExternalLinks', false);
133+
UserScriptPath:=StringReplace(Ini.ReadString('Main', 'UserScript', ''), '%FULLPATH%/', FulllPath, []);
134+
UserScriptFile:=TStringList.Create;
135+
if FileExists(UserScriptPath) then
136+
UserScriptFile.LoadFromFile(UserScriptPath, TEncoding.UTF8);
93137

94138
if LocalFile <> '' then
95139
URL:=LocalFile

0 commit comments

Comments
 (0)