From c49b9a9b79bec073c954c60a3333faa98d425f88 Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Sun, 10 Apr 2022 17:26:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9F=A5=E6=89=BEchrome?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index b28cd2b..9d9b237 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "os" "os/exec" "os/signal" + "runtime" "github.com/zggsong/FileSync/config" "github.com/zggsong/FileSync/server" @@ -26,17 +27,16 @@ func main() { } 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.Start() go func() { <-chBackendDie cmd.Process.Kill() }() - go func() { - cmd.Wait() - chChomrDie <- struct{}{} - }() + cmd.Wait() + chChomrDie <- struct{}{} } //监听中断信号 @@ -45,3 +45,53 @@ func listen2Interrupt() <-chan os.Signal { signal.Notify(chSignal, os.Interrupt) 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 "" +}