自动查找chrome位置

master
ZGGSONG 3 years ago
parent eb0dc08128
commit c49b9a9b79

@ -4,6 +4,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
"runtime"
"github.com/zggsong/FileSync/config" "github.com/zggsong/FileSync/config"
"github.com/zggsong/FileSync/server" "github.com/zggsong/FileSync/server"
@ -26,17 +27,16 @@ func main() {
} }
func startBrowser(chChomrDie chan struct{}, chBackendDie chan struct{}) { func startBrowser(chChomrDie chan struct{}, chBackendDie chan struct{}) {
chromePath := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" // chromePath := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
chromePath := LocateChrome()
cmd := exec.Command(chromePath, "--app=http://127.0.0.1:"+config.GetPort()+"/static/index.html") cmd := exec.Command(chromePath, "--app=http://127.0.0.1:"+config.GetPort()+"/static/index.html")
cmd.Start() cmd.Start()
go func() { go func() {
<-chBackendDie <-chBackendDie
cmd.Process.Kill() cmd.Process.Kill()
}() }()
go func() {
cmd.Wait() cmd.Wait()
chChomrDie <- struct{}{} chChomrDie <- struct{}{}
}()
} }
//监听中断信号 //监听中断信号
@ -45,3 +45,53 @@ func listen2Interrupt() <-chan os.Signal {
signal.Notify(chSignal, os.Interrupt) signal.Notify(chSignal, os.Interrupt)
return chSignal return chSignal
} }
func LocateChrome() string {
// If env variable "LORCACHROME" specified and it exists
if path, ok := os.LookupEnv("LORCACHROME"); ok {
if _, err := os.Stat(path); err == nil {
return path
}
}
var paths []string
switch runtime.GOOS {
case "darwin":
paths = []string{
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
"/Applications/Chromium.app/Contents/MacOS/Chromium",
"/usr/bin/google-chrome-stable",
"/usr/bin/google-chrome",
"/usr/bin/chromium",
"/usr/bin/chromium-browser",
}
case "windows":
paths = []string{
os.Getenv("LocalAppData") + "/Google/Chrome/Application/chrome.exe",
os.Getenv("ProgramFiles") + "/Google/Chrome/Application/chrome.exe",
os.Getenv("ProgramFiles(x86)") + "/Google/Chrome/Application/chrome.exe",
os.Getenv("LocalAppData") + "/Chromium/Application/chrome.exe",
os.Getenv("ProgramFiles") + "/Chromium/Application/chrome.exe",
os.Getenv("ProgramFiles(x86)") + "/Chromium/Application/chrome.exe",
os.Getenv("ProgramFiles(x86)") + "/Microsoft/Edge/Application/msedge.exe",
}
default:
paths = []string{
"/usr/bin/google-chrome-stable",
"/usr/bin/google-chrome",
"/usr/bin/chromium",
"/usr/bin/chromium-browser",
"/snap/bin/chromium",
}
}
for _, path := range paths {
if _, err := os.Stat(path); os.IsNotExist(err) {
continue
}
return path
}
return ""
}

Loading…
Cancel
Save