From 82fac20bfaf8a9c14fd9a95f94af2b11d2be006c Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Mon, 18 Dec 2023 23:15:27 +0800 Subject: [PATCH] Support multiple urls in web search. close #1424 --- Wox/plugin/system/websearch.go | 45 ++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/Wox/plugin/system/websearch.go b/Wox/plugin/system/websearch.go index 998850ba6..3605391c3 100644 --- a/Wox/plugin/system/websearch.go +++ b/Wox/plugin/system/websearch.go @@ -6,7 +6,9 @@ import ( "fmt" "github.com/mat/besticon/besticon" "io" + "slices" "strings" + "time" "wox/plugin" "wox/setting/definition" "wox/util" @@ -19,14 +21,14 @@ var webSearchesTableColumnUrlSettingKey = "Url" var webSearchesTableColumnEnabledSettingKey = "Enabled" var webSearchesTableColumnIconSettingKey = "Icon" -var websearchIcon = plugin.NewWoxImageSvg(``) +var webSearchIcon = plugin.NewWoxImageSvg(``) func init() { plugin.AllSystemPlugin = append(plugin.AllSystemPlugin, &WebSearchPlugin{}) } type webSearch struct { - Url string + Urls []string Title string Keyword string Icon plugin.WoxImage @@ -48,7 +50,7 @@ func (r *WebSearchPlugin) GetMetadata() plugin.Metadata { MinWoxVersion: "2.0.0", Runtime: "Nodejs", Description: "Provide the web search ability", - Icon: websearchIcon.String(), + Icon: webSearchIcon.String(), Entry: "", TriggerKeywords: []string{ "*", @@ -119,29 +121,33 @@ func (r *WebSearchPlugin) indexIcons(ctx context.Context) { } func (r *WebSearchPlugin) indexWebSearchIcon(ctx context.Context, search webSearch) plugin.WoxImage { + //sort urls, so that we can get the same icon between different runs + slices.Sort(search.Urls) + iconUrl := search.Urls[0] + option := besticon.WithLogger(besticon.NewDefaultLogger(io.Discard)) iconFinder := besticon.New(option).NewIconFinder() - icons, err := iconFinder.FetchIcons(search.Url) + icons, err := iconFinder.FetchIcons(iconUrl) if err != nil { - r.api.Log(ctx, fmt.Sprintf("failed to fetch icons for %s: %s", search.Url, err.Error())) - return websearchIcon + r.api.Log(ctx, fmt.Sprintf("failed to fetch icons for %s: %s", search.Urls, err.Error())) + return webSearchIcon } if len(icons) == 0 { - r.api.Log(ctx, fmt.Sprintf("no icons found for %s", search.Url)) - return websearchIcon + r.api.Log(ctx, fmt.Sprintf("no icons found for %s", search.Urls)) + return webSearchIcon } image, imageEr := icons[0].Image() if imageEr != nil { - r.api.Log(ctx, fmt.Sprintf("failed to get image for %s: %s", search.Url, imageEr.Error())) - return websearchIcon + r.api.Log(ctx, fmt.Sprintf("failed to get image for %s: %s", search.Urls, imageEr.Error())) + return webSearchIcon } woxImage, woxImageErr := plugin.NewWoxImage(*image) if woxImageErr != nil { - r.api.Log(ctx, fmt.Sprintf("failed to convert image for %s: %s", search.Url, woxImageErr.Error())) - return websearchIcon + r.api.Log(ctx, fmt.Sprintf("failed to convert image for %s: %s", search.Urls, woxImageErr.Error())) + return webSearchIcon } return woxImage @@ -172,17 +178,20 @@ func (r *WebSearchPlugin) Query(ctx context.Context, query plugin.Query) (result otherQuery := strings.Join(queries[1:], " ") for _, search := range r.webSearches { - if strings.ToLower(search.Keyword) == strings.ToLower(triggerKeyword) { + searchDummy := search + if strings.ToLower(searchDummy.Keyword) == strings.ToLower(triggerKeyword) { results = append(results, plugin.QueryResult{ - Title: strings.ReplaceAll(search.Title, "{query}", otherQuery), - Score: 100, - Icon: search.Icon, - ContextData: search.Url, + Title: strings.ReplaceAll(searchDummy.Title, "{query}", otherQuery), + Score: 100, + Icon: searchDummy.Icon, Actions: []plugin.QueryResultAction{ { Name: "Search", Action: func(actionContext plugin.ActionContext) { - util.ShellOpen(strings.ReplaceAll(actionContext.ContextData, "{query}", otherQuery)) + for _, url := range searchDummy.Urls { + util.ShellOpen(strings.ReplaceAll(url, "{query}", otherQuery)) + time.Sleep(time.Millisecond * 100) + } }, }, },