Skip to content

Commit

Permalink
Introduce {wox:active_browser_url} query variable and fix a focus iss…
Browse files Browse the repository at this point in the history
…ue for setting view back to main query window.
  • Loading branch information
qianlifeng committed May 23, 2024
1 parent 499ec2e commit ff760f6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ class WoxSettingGeneralView extends GetView<WoxSettingController> {
{
"Key": "Query",
"Label": "Query",
"Tooltip": "The query when the hotkey is triggered.\nYou can use {wox:selected_text} to insert selected text.",
"Tooltip": "The query when the hotkey is triggered. Following variables are supported:\n\n"
"{wox:selected_text} represent the selected text.\n"
"{wox:active_browser_url} represent the url of active browser tab.",
"Type": "text",
"TextMaxLines": 1,
"Validators": [
Expand Down
5 changes: 5 additions & 0 deletions Wox/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,11 @@ func (m *Manager) ReplaceQueryVariable(ctx context.Context, query string) string
}
}

if strings.Contains(query, QueryVariableActiveBrowserUrl) {
activeBrowserUrl := m.activeBrowserUrl
query = strings.ReplaceAll(query, QueryVariableActiveBrowserUrl, activeBrowserUrl)
}

return query
}

Expand Down
3 changes: 2 additions & 1 deletion Wox/plugin/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const (
)

const (
QueryVariableSelectedText QueryVariable = "{wox:selected_text}"
QueryVariableSelectedText QueryVariable = "{wox:selected_text}"
QueryVariableActiveBrowserUrl QueryVariable = "{wox:active_browser_url}"
)

// Query from Wox. See "Doc/Query.md" for details.
Expand Down
88 changes: 32 additions & 56 deletions Wox/plugin/system/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"wox/plugin"
"wox/setting/definition"
"wox/setting/validator"
"wox/share"
"wox/util"
)

Expand Down Expand Up @@ -67,12 +66,6 @@ func (c *BrowserPlugin) GetMetadata() plugin.Metadata {
TriggerKeywords: []string{
"*", "browser",
},
Commands: []plugin.MetadataCommand{
{
Command: "summary",
Description: "Summary current active browser url content",
},
},
SupportedOS: []string{
"Windows",
"Macos",
Expand Down Expand Up @@ -132,64 +125,47 @@ func (c *BrowserPlugin) Init(ctx context.Context, initParams plugin.InitParams)
}

func (c *BrowserPlugin) Query(ctx context.Context, query plugin.Query) (results []plugin.QueryResult) {
// only show results when the active window is a browser in global query
isInBrowser := strings.ToLower(query.Env.ActiveWindowTitle) == "google chrome"
if query.IsGlobalQuery() && !isInBrowser {
return results
}

if isInBrowser {
if query.IsGlobalQuery() {
for _, tab := range c.openedTabs {
isTitleMatched, titleScore := IsStringMatchScore(ctx, tab.Title, query.Search)
isUrlMatched, urlScore := strings.Contains(tab.Url, query.Search), int64(1)
if !isTitleMatched && !isUrlMatched {
continue
}
for _, tab := range c.openedTabs {
isTitleMatched, titleScore := IsStringMatchScore(ctx, tab.Title, query.Search)
isUrlMatched, urlScore := strings.Contains(tab.Url, query.Search), int64(1)
if !isTitleMatched && !isUrlMatched {
continue
}

icon := chromeIcon
if tabIconImg, err := getWebsiteIconWithCache(ctx, tab.Url); err == nil {
if backgroundImg, backgroundImgErr := chromeIcon.ToImage(); backgroundImgErr == nil {
if tabImage, tabImageErr := tabIconImg.ToImage(); tabImageErr == nil {
resizedImg := imaging.Resize(tabImage, 16, 16, imaging.Lanczos)
overlayImg := imaging.Overlay(backgroundImg, resizedImg, image.Pt(30, 30), 1)
overlayWoxImg, overlayWoxImgErr := plugin.NewWoxImage(overlayImg)
if overlayWoxImgErr == nil {
icon = overlayWoxImg
}
}
icon := chromeIcon
if tabIconImg, err := getWebsiteIconWithCache(ctx, tab.Url); err == nil {
if backgroundImg, backgroundImgErr := chromeIcon.ToImage(); backgroundImgErr == nil {
if tabImage, tabImageErr := tabIconImg.ToImage(); tabImageErr == nil {
resizedImg := imaging.Resize(tabImage, 16, 16, imaging.Lanczos)
overlayImg := imaging.Overlay(backgroundImg, resizedImg, image.Pt(30, 30), 1)
overlayWoxImg, overlayWoxImgErr := plugin.NewWoxImage(overlayImg)
if overlayWoxImgErr == nil {
icon = overlayWoxImg
}
}

results = append(results, plugin.QueryResult{
Title: tab.Title,
SubTitle: tab.Url,
Score: util.MaxInt64(titleScore, urlScore),
Icon: icon,
Actions: []plugin.QueryResultAction{
{
Name: "Open",
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
c.m.Broadcast([]byte(fmt.Sprintf(`{"method":"highlightTab","data":"{\"tabId\":%d,\"windowId\":%d,\"tabIndex\": %d}"}`, tab.TabId, tab.WindowId, tab.TabIndex)))
},
},
},
})
}
}

if query.Command == "summary" {
if query.Env.ActiveBrowserUrl == "" {
return []plugin.QueryResult{
{
Title: "No active browser url",
SubTitle: "Please open a browser tab",
Icon: browserIcon,
results = append(results, plugin.QueryResult{
Title: tab.Title,
SubTitle: tab.Url,
Score: util.MaxInt64(titleScore, urlScore),
Icon: icon,
Actions: []plugin.QueryResultAction{
{
Name: "Open",
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
c.m.Broadcast([]byte(fmt.Sprintf(`{"method":"highlightTab","data":"{\"tabId\":%d,\"windowId\":%d,\"tabIndex\": %d}"}`, tab.TabId, tab.WindowId, tab.TabIndex)))
},
}
}

c.api.ChangeQuery(ctx, share.PlainQuery{
QueryType: plugin.QueryTypeInput,
QueryText: "llm tldr " + query.Env.ActiveBrowserUrl,
})
}
},
},
})
}

return results
Expand Down
13 changes: 13 additions & 0 deletions Wox/ui/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,19 @@ func (m *Manager) PostSettingUpdate(ctx context.Context, key, value string) {
if key == "SelectionHotkey" {
m.RegisterSelectionHotkey(ctx, value)
}
if key == "QueryHotkeys" {
// unregister previous hotkeys
logger.Info(ctx, "post update query hotkeys, unregister previous query hotkeys")
for _, hk := range m.queryHotkeys {
hk.Unregister(ctx)
}
m.queryHotkeys = nil

queryHotkeys := setting.GetSettingManager().GetWoxSetting(ctx).QueryHotkeys.Get()
for _, queryHotkey := range queryHotkeys {
m.RegisterQueryHotkey(ctx, queryHotkey)
}
}
}

func (m *Manager) ExitApp(ctx context.Context) {
Expand Down

0 comments on commit ff760f6

Please sign in to comment.