pkg / browser

Package browser provides helpers to open files, readers, and urls in a browser window.
BSD 2-Clause "Simplified" License
559 stars 99 forks source link

Stop using CMD with Windows #16

Closed ghost closed 3 years ago

ghost commented 5 years ago

The current Windows incarnation is brittle:

https://github.com/pkg/browser/blob/0a3d74bf9ce488f035cf5bc36f753a711bc74334/browser_windows.go#L9-L12

it survives ampersands, but not other characters:

package main
import "github.com/pkg/browser"
func main() {
   browser.OpenURL("http://example.com?aa bb&cc")
}

better would be to call RunDLL32, no replacer needed. Examples:

rundll32 url,OpenURL "http://example.com?aa bb&cc"
rundll32 url,FileProtocolHandler "http://example.com?aa bb&cc"

https://github.com/skratchdot/open-golang/blob/master/open/exec_windows.go

Or call browser directly via the BROWSER environment variable. This is what Python does:

import webbrowser
webbrowser.open('http://example.com?aa bb&cc')

https://github.com/python/cpython/blob/master/Lib/webbrowser.py

Or better still would be to call ShellExecuteW on Windows:

https://docs.microsoft.com/windows/win32/api/shellapi/nf-shellapi-shellexecutew

This is what D does:

import std.process;
void main() {
   browse("http://example.com?aa bb&cc");
}

https://github.com/dlang/phobos/blob/master/std/process.d

and Nim:

import browsers
openDefaultBrowser("http://example.com?aa bb&cc")

https://github.com/nim-lang/Nim/blob/devel/lib/pure/browsers.nim

luna-duclos commented 3 years ago

PRs definately welcome for this, might get to it myself eventually, but no guarantees :)