Skip to content

Commit

Permalink
Improvements: opening links in external browsers and userscript support
Browse files Browse the repository at this point in the history
  • Loading branch information
r57zone committed Aug 5, 2024
1 parent 4f7117e commit cfde1c3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
28 changes: 28 additions & 0 deletions Source/Config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[Main]
Title=Test App
# Application folder address - "%FULLPATH%/", clear the value of "File" if you want to load the website.
File=%FULLPATH%/index.html
UserAgent=
URL=https://google.ru
# Opening links with "_blank" attribute in external browsers
OpenExternalLinks=1
# Example of the path - "%FULLPATH%/UserScript.js"
UserScript=

[Window]
Width=640
Height=360
SaveSize=0

# None = 0, Sizeable = 1, Single = 2, Dialog = 3, SizeToolWin = 4, ToolWindow = 5
BorderStyle=1
HideMaximize=0

# Normal = 0, Maximized = 1, FullScreen = 2
WindowState=0
StayOnTop=0

# Position
Top=
Left=
SavePos=0
12 changes: 8 additions & 4 deletions Source/Unit1.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ object Main: TMain
Left = 0
Top = 0
Caption = 'Main'
ClientHeight = 468
ClientWidth = 796
ClientHeight = 480
ClientWidth = 640
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Expand All @@ -17,11 +17,15 @@ object Main: TMain
object EdgeBrowser: TEdgeBrowser
Left = 0
Top = 0
Width = 796
Height = 468
Width = 640
Height = 480
Align = alClient
TabOrder = 0
UserDataFolder = '%LOCALAPPDATA%\bds.exe.WebView2'
OnCreateWebViewCompleted = EdgeBrowserCreateWebViewCompleted
OnNavigationCompleted = EdgeBrowserNavigationCompleted
OnNewWindowRequested = EdgeBrowserNewWindowRequested
ExplicitWidth = 792
ExplicitHeight = 467
end
end
56 changes: 50 additions & 6 deletions Source/Unit1.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, WebView2, Winapi.ActiveX, Vcl.Edge, IniFiles;
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, WebView2, Winapi.ActiveX, Vcl.Edge, IniFiles, ShellAPI;

const // https://stackoverflow.com/questions/66692031/how-to-set-useragent-in-new-delphi-tedgebrowser
IID_ICoreWebView2Settings2: TGUID = '{EE9A0F68-F46C-4E32-AC23-EF8CAC224D2A}';
Expand All @@ -23,6 +23,10 @@ TMain = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure EdgeBrowserCreateWebViewCompleted(Sender: TCustomEdgeBrowser;
AResult: HRESULT);
procedure EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
Args: TNewWindowRequestedEventArgs);
procedure EdgeBrowserNavigationCompleted(Sender: TCustomEdgeBrowser;
IsSuccess: Boolean; WebErrorStatus: TOleEnum);
private
{ Private declarations }
public
Expand All @@ -34,7 +38,8 @@ TMain = class(TForm)
WinOldWidth, WinOldHeight, WinOldTop, WinOldLeft: integer;
WinSaveSize, WinSavePos: boolean;
EdgeUserAgent: string;

OpenExternalLinks, LoadUserScript: boolean;
UserScriptFile: TStringList;

implementation

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

procedure TMain.EdgeBrowserNavigationCompleted(Sender: TCustomEdgeBrowser;
IsSuccess: Boolean; WebErrorStatus: TOleEnum);
begin
if UserScriptFile.Text <> '' then
EdgeBrowser.ExecuteScript(UserScriptFile.Text);
end;

procedure TMain.EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
Args: TNewWindowRequestedEventArgs);
var
WebViewArgs: ICoreWebView2NewWindowRequestedEventArgs;
PUri: PWideChar;
begin
if OpenExternalLinks = false then Exit;

// Get arguments WebView2
WebViewArgs:=Args as ICoreWebView2NewWindowRequestedEventArgs;

// Get URL
if Succeeded(WebViewArgs.Get_uri(PUri)) and (PUri <> nil) then begin
try
ShellExecute(0, 'open', PUri, nil, nil, SW_SHOWNORMAL);
finally
CoTaskMemFree(PUri);
end;

// Blocking the opening of a new window
Args.ArgsInterface.Set_Handled(1);
end;
end;

procedure TMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
Ini: TIniFile;
Expand All @@ -76,20 +112,28 @@ procedure TMain.FormClose(Sender: TObject; var Action: TCloseAction);
Ini.WriteInteger('Window', 'Left', Left);
Ini.Free;
end;
UserScriptFile.Free;
end;

procedure TMain.FormCreate(Sender: TObject);
var
Ini: TIniFile; URL, LocalFile: string;
Ini: TIniFile; URL, LocalFile, FulllPath, UserScriptPath: string;
begin
EdgeBrowser.UserDataFolder := ExtractFilePath(ParamStr(0)) + 'Data';
EdgeBrowser.UserDataFolder:=ExtractFilePath(ParamStr(0)) + 'Data';

Ini:=TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Config.ini');
FulllPath:=ExtractFilePath(ParamStr(0));
Ini:=TIniFile.Create(FulllPath + 'Config.ini');
Main.Caption:=UTF8ToAnsi(Ini.ReadString('Main', 'Title', ''));
LocalFile:=Ini.ReadString('Main', 'File', '');
LocalFile:=StringReplace(LocalFile, '%FULLPATH%/', ExtractFilePath(ParamStr(0)), []);

LocalFile:=StringReplace(LocalFile, '%FULLPATH%/', FulllPath, []);
LocalFile:=StringReplace(LocalFile, '\', '/', [rfReplaceAll]);
EdgeUserAgent:=Ini.ReadString('Main', 'UserAgent', '');
OpenExternalLinks:=Ini.ReadBool('Main', 'OpenExternalLinks', false);
UserScriptPath:=StringReplace(Ini.ReadString('Main', 'UserScript', ''), '%FULLPATH%/', FulllPath, []);
UserScriptFile:=TStringList.Create;
if FileExists(UserScriptPath) then
UserScriptFile.LoadFromFile(UserScriptPath, TEncoding.UTF8);

if LocalFile <> '' then
URL:=LocalFile
Expand Down

0 comments on commit cfde1c3

Please sign in to comment.