增加使用系统默认浏览器打开连接方法
This commit is contained in:
parent
5cfbbbb832
commit
0a75d06083
33
browser.go
Normal file
33
browser.go
Normal file
@ -0,0 +1,33 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-04-09 19:02
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type browser struct {
|
||||
commandTable map[string]string
|
||||
}
|
||||
|
||||
// Open 使用系统默认浏览器打开链接
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:05 2023/4/9
|
||||
func (b *browser) Open(link string) error {
|
||||
run, ok := b.commandTable[runtime.GOOS]
|
||||
if !ok {
|
||||
return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
|
||||
}
|
||||
|
||||
cmd := exec.Command(run, link)
|
||||
return cmd.Start()
|
||||
}
|
9
init.go
9
init.go
@ -40,6 +40,8 @@ var (
|
||||
UserAgent *userAgent
|
||||
// Console 控制台输出数据
|
||||
Console *console
|
||||
// Browser 浏览器操作实例
|
||||
Browser *browser
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -61,4 +63,11 @@ func init() {
|
||||
list: defaultUserAgentList,
|
||||
}
|
||||
Console = &console{}
|
||||
Browser = &browser{
|
||||
commandTable: map[string]string{
|
||||
"windows": "start",
|
||||
"darwin": "open",
|
||||
"linux": "xdg-open",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user