Update browsers.nim, fix openDefaultBrowser()'s bug (#22052)

modified to fix blank page fault. see openDefaultBrowser()

The previous implement use "http:about:blank" which will be processed by "prepare" and be considered as a file path, turning to "file://...", which is no doubt beyond expectation.
To fix it,firstly the "about:blank" is used instead. Next, a new "openDefaultBrowserImplPrep" proc is added, take place of the previous version of "openDefaultBrowserImpl"
This commit is contained in:
litlighilit
2023-06-09 21:54:33 +08:00
committed by GitHub
parent 64b27edd3a
commit 1d6fd30afb

View File

@@ -40,15 +40,17 @@ proc prepare(s: string): string =
else:
result = "file://" & absolutePath(s)
proc openDefaultBrowserImpl(url: string) =
proc openDefaultBrowserImplPrep(url: string) =
## note the url argument should be alreadly prepared, i.e. the url is passed "AS IS"
when defined(windows):
var o = newWideCString(osOpenCmd)
var u = newWideCString(prepare url)
var u = newWideCString(url)
discard shellExecuteW(0'i32, o, u, nil, nil, SW_SHOWNORMAL)
elif defined(macosx):
discard execShellCmd(osOpenCmd & " " & quoteShell(prepare url))
discard execShellCmd(osOpenCmd & " " & quoteShell(url))
else:
var u = quoteShell(prepare url)
var u = quoteShell(url)
if execShellCmd(osOpenCmd & " " & u) == 0: return
for b in getEnv("BROWSER").split(PathSep):
try:
@@ -58,6 +60,9 @@ proc openDefaultBrowserImpl(url: string) =
except OSError:
discard
proc openDefaultBrowserImpl(url: string) =
openDefaultBrowserImplPrep(prepare url)
proc openDefaultBrowser*(url: string) =
## Opens `url` with the user's default browser. This does not block.
## The URL must not be empty string, to open on a blank page see `openDefaultBrowser()`.
@@ -93,4 +98,4 @@ proc openDefaultBrowser*() {.since: (1, 1).} =
## **See also:**
##
## * https://tools.ietf.org/html/rfc6694#section-3
openDefaultBrowserImpl("http:about:blank") # See IETF RFC-6694 Section 3.
openDefaultBrowserImplPrep("about:blank") # See IETF RFC-6694 Section 3.