-
Notifications
You must be signed in to change notification settings - Fork 8
/
chrome_plus.go
187 lines (181 loc) · 6.18 KB
/
chrome_plus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
jsoniter "github.com/json-iterator/go"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"sync"
"time"
)
func chromePlusScreen(win fyne.Window, data *SettingsData) fyne.CanvasObject {
var githubReleaseMap map[string]GithubRelease
var versionList []string
chromePlusRadio := widget.NewRadioGroup([]string{
"Bush2021"}, func(value string) {
data.chromePlus.Set(value)
})
versionSelect := widget.NewSelect([]string{}, func(ver string) {
setPlusVer(data, ver, githubReleaseMap)
})
versionSelect.PlaceHolder = LoadString("VersionSelectPlaceHolder")
versionSelect.Disable()
chromePlusRadio.Selected = getString(data.chromePlus)
chromePlusRadio.Disable()
downBtn := widget.NewButtonWithIcon(LoadString("InstallBtnLabel"), theme.DownloadIcon(), func() {
ov, _ := data.oldPlusVer.Get()
cv, _ := data.curPlusVer.Get()
if cv == ov && fileExist(path.Join(getString(data.installPath), "version.dll")) {
alertInfo(LoadString("NoNeedUpdateMsg"), win)
} else {
chromeInUse := isProcessExist("chrome.exe")
if chromeInUse {
alertInfo(LoadString("ChromeRunningMsg"), win)
} else {
data.plusBtnStatus.Set(true)
installPlus(data, win)
}
}
})
checkBtn := widget.NewButtonWithIcon(LoadString("CheckBtnLabel"), theme.SearchIcon(), func() {
var err error
githubReleaseMap, versionList, err = getChromePlusInfo(data)
if err != nil {
alertInfo(LoadString("UpdateErrMsg"), win)
} else {
if githubReleaseMap != nil && len(versionList) > 0 {
versionSelect.SetOptions(versionList)
versionSelect.SetSelected(versionList[0])
setPlusVer(data, versionList[0], githubReleaseMap)
versionSelect.Enable()
downBtn.Enable()
} else {
alertInfo(LoadString("UpdateErrMsg"), win)
}
}
})
data.plusBtnStatus.AddListener(binding.NewDataListener(func() {
if getBool(data.plusBtnStatus) {
downBtn.Disable()
} else {
downBtn.Enable()
}
}))
curVerLabel := widget.NewLabelWithData(data.curPlusVer)
curVerLabel.TextStyle.Bold = true
oldPlusVer := GetVersion(data, "version.dll")
logger.Info("chrome++ version:", oldPlusVer)
_ = data.oldPlusVer.Set(oldPlusVer)
form := widget.NewForm(
&widget.FormItem{Text: LoadString("NowVerLabel"), Widget: widget.NewLabelWithData(data.oldPlusVer)},
&widget.FormItem{Text: LoadString("LatestVerLabel"), Widget: versionSelect},
&widget.FormItem{Text: LoadString("BranchLabel"), Widget: chromePlusRadio},
)
rich := widget.NewRichTextFromMarkdown(LoadString("MarkdownMsg"))
rich.Wrapping = fyne.TextWrapWord
infoCard := widget.NewCard("", "", rich)
plusDownloadProgress = widget.NewProgressBar()
plusDownloadProgress.TextFormatter = func() string {
if plusDownloadProgress.Max*0.9 == plusDownloadProgress.Value {
return fmt.Sprintf(LoadString("PlusDownloadedMsg"))
} else if plusDownloadProgress.Max == plusDownloadProgress.Value {
return "安装完成"
} else if plusDownloadProgress.Value == -1 {
return "下载失败,请稍后重试"
}
return fmt.Sprintf(LoadString("PlusDownloadingMsg"))
}
data.plusProcessStatus.AddListener(binding.NewDataListener(func() {
if getBool(data.plusProcessStatus) {
plusDownloadProgress.Show()
} else {
plusDownloadProgress.Hide()
}
}))
logger.Debug("Chrome++ tab load success.")
return container.New(&buttonLayout{}, container.NewVBox(form,
infoCard,
), container.NewVBox(plusDownloadProgress, container.NewGridWithColumns(2, checkBtn, downBtn)))
}
func setPlusVer(data *SettingsData, ver string, releaseMap map[string]GithubRelease) {
plusInfo := releaseMap[ver]
data.curPlusVer.Set(plusInfo.TagName)
data.plusDownloadUrl.Set(plusInfo.Assets[0].BrowserDownloadURL)
}
var (
plusDownloadProgress *widget.ProgressBar
)
func installPlus(data *SettingsData, win fyne.Window) {
url := getString(data.plusDownloadUrl)
plusDownloadProgress.SetValue(0)
data.plusProcessStatus.Set(true)
sysInfo := getInfo()
parentPath, _ := data.installPath.Get()
fileName := getFileName(url)
fileName = filepath.Join(parentPath, fileName)
fileSize, _ := getFileSize(url)
var wg = &sync.WaitGroup{}
GoroutineDownload(data, url, fileName, 4, 100*1024, 500, fileSize, plusDownloadProgress, wg)
downloadedBytes = 0
UnCompress7zFilter(fileName, parentPath, sysInfo.goarch)
os.Rename(filepath.Join(parentPath, sysInfo.goarch, "App", "version.dll"), path.Join(parentPath, "version.dll"))
if !fileExist(path.Join(parentPath, "chrome++.ini")) {
os.Rename(filepath.Join(parentPath, sysInfo.goarch, "App", "chrome++.ini"), path.Join(parentPath, "chrome++.ini"))
}
//clean tmp dir
os.Remove(fileName)
os.RemoveAll(filepath.Join(parentPath, sysInfo.goarch))
plusDownloadProgress.SetValue(1)
defer data.oldPlusVer.Set(getString(data.curPlusVer))
defer data.plusBtnStatus.Set(false)
alertInfo(LoadString("InstalledMsg"), win)
}
func setProxy(sd *SettingsData, reqUrl string) (*http.Client, string) {
ghProxy := getString(sd.ghProxy)
client := http.Client{Timeout: time.Second * time.Duration(30)}
if ghProxy != "" {
if getString(sd.proxyType) == "GH-PROXY" {
reqUrl = pathJoin(ghProxy, reqUrl)
} else {
urli := url.URL{}
urlproxy, _ := urli.Parse(ghProxy)
client.Transport = &http.Transport{
Proxy: http.ProxyURL(urlproxy),
}
}
}
return &client, reqUrl
}
func getChromePlusInfo(sd *SettingsData) (map[string]GithubRelease, []string, error) {
apiUrl := "https://raw.githubusercontent.com/libsgh/ghapi-json-generator/output/v2/repos/Bush2021/chrome_plus/releases%3Fper_page%3D10/data.json"
client, reqUrl := setProxy(sd, apiUrl)
response, err := client.Get(reqUrl)
if err != nil {
logger.Errorln(err)
return nil, nil, err
}
defer response.Body.Close()
data, err := io.ReadAll(response.Body)
var githubReleases []GithubRelease
jsoniter.UnmarshalFromString(string(data), &githubReleases)
if err != nil {
logger.Errorln(err)
return nil, nil, err
}
result := make(map[string]GithubRelease)
versionList := make([]string, 0)
for _, item := range githubReleases {
result[item.TagName] = item
versionList = append(versionList, item.TagName)
}
logger.Debugf("Chrome++ versions:%v", versionList)
return result, versionList, err
}