-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathurlutils.pas
More file actions
62 lines (53 loc) · 1.43 KB
/
Copy pathurlutils.pas
File metadata and controls
62 lines (53 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
unit urlutils;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LazHelpHTML, UTF8Process, LCLProc;
/// <summary>
/// Opens a URL in the system's default web browser
/// </summary>
/// <param name="URL">The URL to open (e.g., 'https://github.com/example')</param>
/// <returns>True if successful, False otherwise</returns>
function OpenURLInBrowser(const URL: string): Boolean;
implementation
function OpenURLInBrowser(const URL: string): Boolean;
var
v: THTMLBrowserHelpViewer;
BrowserPath, BrowserParams: string;
p: LongInt;
BrowserProcess: TProcessUTF8;
begin
Result := False;
v := THTMLBrowserHelpViewer.Create(nil);
try
try
v.FindDefaultBrowser(BrowserPath, BrowserParams);
debugln(['Browser Path=', BrowserPath, ' Params=', BrowserParams]);
// Replace %s placeholder with actual URL
p := System.Pos('%s', BrowserParams);
if p > 0 then
begin
System.Delete(BrowserParams, p, 2);
System.Insert(URL, BrowserParams, p);
end;
// Launch browser
BrowserProcess := TProcessUTF8.Create(nil);
try
BrowserProcess.CommandLine := BrowserPath + ' ' + BrowserParams;
BrowserProcess.Execute;
Result := True;
finally
BrowserProcess.Free;
end;
except
on E: Exception do
begin
debugln(['Error opening URL: ', E.Message]);
Result := False;
end;
end;
finally
v.Free;
end;
end;
end.