Squashed commit of the following: commit 146f106fbece251606f358141bda3d9c524c3e93 Author: Sun <95302870@qq.com> Date: Thu Dec 28 19:39:36 2023 +0800 修改默认宽度 commit 827197e295b2b0997902b8f965d14ab2598c5c1b Author: Sun <95302870@qq.com> Date: Thu Dec 28 17:54:25 2023 +0800 修复升级掉登录的问题 commit 2e54326f5fe2b8fac4eed488a14b04b8133c41bb Author: Sun <95302870@qq.com> Date: Thu Dec 28 17:21:49 2023 +0800 优化边距等提示 commit 469c6fd644a2a23d96405d15456980edfdfb17b0 Author: Sun <95302870@qq.com> Date: Thu Dec 28 17:21:34 2023 +0800 搜索栏选择搜索引擎后关闭选择器 commit 632da9635a4a434d361495dfcb83f8b650cb4a3c Author: Sun <95302870@qq.com> Date: Thu Dec 28 16:54:28 2023 +0800 增加 内容左右边距和最大宽度 commit f1cc3dce2a51ec60c1b04f21954b99b284b2df61 Author: Sun <95302870@qq.com> Date: Thu Dec 28 14:24:23 2023 +0800 分组标题增加阴影 commit 7615a29678037362c18632f38caad72e07a32d1a Author: Sun <95302870@qq.com> Date: Thu Dec 28 14:18:41 2023 +0800 更改版本号1.2.1 commit b605374e951d3fb2ca52d36bd0fa82aec4a5e89b Author: Sun <95302870@qq.com> Date: Thu Dec 28 14:11:06 2023 +0800 优化小部分网站图标获取失败的问题 commit c8141184a14fb02f941bbec03ac98f59f8b12d4b Author: Sun <95302870@qq.com> Date: Thu Dec 28 13:00:56 2023 +0800 优化上传管理界面
87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package siteFavicon
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
"net/url"
|
||
"regexp"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"github.com/PuerkitoBio/goquery"
|
||
)
|
||
|
||
func IsHTTPURL(url string) bool {
|
||
httpPattern := `^(http://|https://|//)`
|
||
match, err := regexp.MatchString(httpPattern, url)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
return match
|
||
}
|
||
|
||
func GetOneFaviconURL(urlStr string) (string, bool) {
|
||
iconURLs, err := getFaviconURL(urlStr)
|
||
if err != nil {
|
||
return "", false
|
||
}
|
||
|
||
for _, v := range iconURLs {
|
||
// 标准的路径地址
|
||
if IsHTTPURL(v) {
|
||
return v, true
|
||
} else {
|
||
urlInfo, _ := url.Parse(urlStr)
|
||
fullUrl := urlInfo.Scheme + "://" + urlInfo.Host + "/" + strings.TrimPrefix(v, "/")
|
||
return fullUrl, true
|
||
}
|
||
}
|
||
return "", false
|
||
}
|
||
|
||
func getFaviconURL(url string) ([]string, error) {
|
||
var icons []string
|
||
icons = make([]string, 0)
|
||
client := &http.Client{}
|
||
req, err := http.NewRequest("GET", url, nil)
|
||
if err != nil {
|
||
return icons, err
|
||
}
|
||
|
||
// 设置User-Agent头字段,模拟浏览器请求
|
||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
|
||
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
return icons, err
|
||
}
|
||
|
||
defer resp.Body.Close()
|
||
|
||
if resp.StatusCode != http.StatusOK {
|
||
return icons, errors.New("HTTP request failed with status code " + strconv.Itoa(resp.StatusCode))
|
||
}
|
||
|
||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||
if err != nil {
|
||
return icons, err
|
||
}
|
||
|
||
// 查找所有link标签,筛选包含rel属性为"icon"的标签
|
||
doc.Find("link").Each(func(i int, s *goquery.Selection) {
|
||
rel, _ := s.Attr("rel")
|
||
href, _ := s.Attr("href")
|
||
|
||
if strings.Contains(rel, "icon") && href != "" {
|
||
// fmt.Println(href)
|
||
icons = append(icons, href)
|
||
}
|
||
})
|
||
|
||
if len(icons) == 0 {
|
||
return icons, errors.New("favicon not found on the page")
|
||
}
|
||
|
||
return icons, nil
|
||
}
|