-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclient.go
656 lines (581 loc) · 15.6 KB
/
client.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// Copyright (c) 2020 Dean Jackson <[email protected]>
// MIT Licence applies http://opensource.org/licenses/MIT
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/util"
"github.com/peterbourgon/ff/ffcli"
)
var (
// search history
historyCmd = &ffcli.Command{
Name: "history",
Usage: "alfred-firefox -query <query> history",
ShortHelp: "search browsing history",
LongHelp: wrap(`Search browser history.`),
Exec: runHistory,
}
// search downloads
downloadsCmd = &ffcli.Command{
Name: "downloads",
Usage: "alfred-firefox -query <query> downloads",
ShortHelp: "search downloads",
LongHelp: wrap(`Search browser downloads.`),
Exec: runDownloads,
}
// search bookmarks
bookmarksCmd = &ffcli.Command{
Name: "bookmarks",
Usage: "alfred-firefox -query <query> bookmarks",
ShortHelp: "search bookmarks",
LongHelp: wrap(`Search browser bookmarks.`),
Exec: runBookmarks,
}
// search bookmarklets
bookmarkletsCmd = &ffcli.Command{
Name: "bookmarklets",
Usage: "alfred-firefox -query <query> bookmarklets",
ShortHelp: "search bookmarklets",
LongHelp: wrap(`Search bookmarklets and execute in frontmost tab.`),
Exec: runBookmarklets,
}
/*
// open URL
// TODO: is this used? can it be removed?
openURLCmd = &ffcli.Command{
Name: "open-url",
Usage: "alfred-firefox -url <url> open-url",
ShortHelp: "open URL",
LongHelp: wrap(`Open specified URL`),
Exec: runOpenURL,
}
*/
// execute a bookmarklet in the specified tab
runBookmarkletCmd = &ffcli.Command{
Name: "run-bookmarklet",
Usage: "alfred-firefox [-tab <id>] -bookmark <id> run-bookmarklet",
ShortHelp: "execute bookmarklet in the specified tab",
LongHelp: wrap(`
Execute a bookmarklet in a tab. Bookmark ID is required.
If no tab ID is specified, bookmarklet is run in the active tab.
`),
Exec: runBookmarklet,
}
// filter open tabs
tabsCmd = &ffcli.Command{
Name: "tabs",
Usage: "alfred-firefox [-query <query>] tabs",
ShortHelp: "filter browser tabs",
LongHelp: wrap(`Filter browser tabs and perform actions on them.`),
Exec: runTabs,
}
// filter tab & URL actions for current tab
currentTabCmd = &ffcli.Command{
Name: "current-tab",
Usage: "alfred-firefox [-query <query>] current-tab",
ShortHelp: "actions for current tab",
LongHelp: wrap(`Filter and run actions for current tab`),
Exec: runCurrentTab,
}
infoFlags = flag.NewFlagSet("tab-info", flag.ExitOnError)
shellVars bool // export tab info as shell variables
// export info for current tab
currentTabInfoCmd = &ffcli.Command{
Name: "tab-info",
Usage: "alfred-firefox tab-info [-shell]",
ShortHelp: "export current tab info",
LongHelp: wrap(`Export current tab info as variables`),
FlagSet: infoFlags,
Exec: runCurrentTabInfo,
}
// run a tab/URL action for the specified tab
tabCmd = &ffcli.Command{
Name: "tab",
Usage: "alfred-firefox [-tab <id>] -action <name> tab",
ShortHelp: "execute tab action",
LongHelp: wrap(`
Execute specified action on tab. Both URL and tab actions
are available on tabs.
`),
Exec: runTabAction,
}
// inject JS into the specified tab
injectCmd = &ffcli.Command{
Name: "inject",
Usage: "alfred-firefox [-tab <id>] inject <script>",
ShortHelp: "inject JavaScript into tab",
LongHelp: wrap(`
Execute JavaScript in specifed tab and return result as JSON.
`),
Exec: runInject,
}
// run action for URL
urlCmd = &ffcli.Command{
Name: "url",
Usage: "alfred-firefox [-url <url>] -action <name> url",
ShortHelp: "execute URL action",
LongHelp: wrap(`Execute specified action on URL`),
Exec: runURLAction,
}
// filter URL (and tab) actions
actionsCmd = &ffcli.Command{
Name: "actions",
Usage: "alfred-firefox [-tab <id>] [-url <url>] [-query <query>] actions",
ShortHelp: "filter tab/URL actions",
LongHelp: wrap(`View/filter and execute tab/URL actions.`),
Exec: runActions,
}
// check for update
updateCmd = &ffcli.Command{
Name: "update",
Usage: "alfred-firefox update",
ShortHelp: "check for workflow update",
LongHelp: wrap(`Check if newer version of workflow is available.`),
Exec: runUpdate,
}
// show workflow status
statusCmd = &ffcli.Command{
Name: "options",
Usage: "alfred-firefox [-query <query>] options",
ShortHelp: "show workflow status & options",
LongHelp: wrap(`Show workflow status, info and options.`),
Exec: runStatus,
}
// open file in default application
openCmd = &ffcli.Command{
Name: "open",
Usage: "alfred-firefox open <path>",
ShortHelp: "open file in default application",
LongHelp: wrap(`Open file in default application.`),
Exec: runOpen,
}
// reveal file in Finder
revealCmd = &ffcli.Command{
Name: "reveal",
Usage: "alfred-firefox reveal <path>",
ShortHelp: "reveal file in Finder",
LongHelp: wrap(`Reveal file in Finder.`),
Exec: runReveal,
}
)
func init() {
infoFlags.BoolVar(&shellVars, "shell", false, "export shell variables")
}
// func runOpenURL(_ []string) error {
// wf.Configure(aw.TextErrors(true))
// log.Printf("opening URL %q ...", URL)
// _, err := util.RunCmd(exec.Command("open", URL))
// return err
// }
// search Firefox history
func runHistory(_ []string) error {
checkForUpdate()
if len(query) < 3 {
wf.Warn("Query Too Short", "Please enter at least 3 characters")
return nil
}
log.Printf("searching bookmarks for %q ...", query)
history, err := mustClient().History(query)
if err != nil {
return err
}
custom := loadCustomActions()
for _, h := range history {
it := wf.NewItem(h.Title).
Subtitle(h.URL).
Arg(h.URL).
UID(h.ID).
Valid(true).
Icon(iconHistory).
Var("CMD", "url").
Var("ACTION", urlDefault).
Var("URL", h.URL).
Var("TITLE", h.Title)
it.NewModifier(aw.ModCmd).
Subtitle("Other Actions…").
Arg("").
Icon(iconMore).
Var("CMD", "actions")
custom.Add(it, false)
}
wf.WarnEmpty("No Results", "Try a different query?")
wf.SendFeedback()
return nil
}
// search Firefox bookmarks
func runBookmarks(_ []string) error {
checkForUpdate()
if len(query) < 3 {
wf.Warn("Query Too Short", "Please enter at least 3 characters")
return nil
}
log.Printf("searching bookmarks for %q ...", query)
bookmarks, err := mustClient().Bookmarks(query)
if err != nil {
return err
}
custom := loadCustomActions()
for _, bm := range bookmarks {
if bm.IsBookmarklet() {
continue
}
it := wf.NewItem(bm.Title).
Subtitle(bm.URL).
Arg(bm.URL).
UID(bm.ID).
Valid(true).
Icon(iconBookmark).
Var("CMD", "url").
Var("ACTION", urlDefault).
Var("URL", bm.URL).
Var("TITLE", bm.Title)
it.NewModifier(aw.ModCmd).
Subtitle("Other Actions…").
Arg("").
Icon(iconMore).
Var("CMD", "actions")
custom.Add(it, false)
}
wf.WarnEmpty("No Results", "Try a different query?")
wf.SendFeedback()
return nil
}
// search Firefox bookmarklets
func runBookmarklets(_ []string) error {
checkForUpdate()
if len(query) < 3 {
wf.Warn("Query Too Short", "Please enter at least 3 characters")
return nil
}
log.Printf("searching bookmarklets for %q ...", query)
bookmarks, err := mustClient().Bookmarks(query)
if err != nil {
return err
}
for _, bm := range bookmarks {
if !bm.IsBookmarklet() {
continue
}
wf.NewItem(bm.Title).
Subtitle("↩ to execute in current tab").
UID(bm.ID).
Copytext("bml:"+bm.ID+","+bm.Title).
Arg(bm.URL).
Icon(iconBookmarklet).
Valid(true).
Var("CMD", "run-bookmarklet").
Var("BOOKMARK", bm.ID)
}
wf.WarnEmpty("No Results", "Try a different query?")
wf.SendFeedback()
return nil
}
// execute a bookmarklet in a tab
func runBookmarklet(_ []string) error {
wf.Configure(aw.TextErrors(true))
log.Printf("running bookmarklet %q in tab #%d ...", bookmarkID, tabID)
return mustClient().
RunBookmarklet(RunBookmarkletArg{BookmarkID: bookmarkID, TabID: tabID})
}
// filter open Firefox tabs
func runTabs(_ []string) error {
log.Printf("fetching tabs for query %q ...", query)
checkForUpdate()
var (
tabs []Tab
err error
)
if tabs, err = mustClient().Tabs(); err != nil {
return err
}
custom := loadCustomActions()
for _, t := range tabs {
id := fmt.Sprintf("%d", t.ID)
it := wf.NewItem(t.Title).
Subtitle(t.URL).
Arg(t.URL).
UID(t.Title).
Valid(true).
Icon(iconTab).
Var("CMD", "tab").
Var("ACTION", "Activate Tab").
Var("TAB", id).
Var("URL", t.URL).
Var("TITLE", t.Title)
it.NewModifier(aw.ModCmd).
Subtitle("Other Actions…").
Arg("").
Icon(iconMore).
Var("CMD", "actions")
custom.Add(it, true)
}
if query != "" {
_ = wf.Filter(query)
}
wf.WarnEmpty("No Matching Tabs", "Try a different query?")
wf.SendFeedback()
return nil
}
// execute a tab or URL action on the given tab
func runTabAction(_ []string) error {
wf.Configure(aw.TextErrors(true))
// load tab info so we can also run URL actions
tab, err := mustClient().Tab(tabID)
if err != nil {
return err
}
log.Printf("running action %q on tab #%d ...", action, tab.ID)
if a, ok := tabActions[action]; ok {
return a.Run(tabID)
}
if a, ok := urlActions[action]; ok {
return a.Run(tab.URL)
}
return fmt.Errorf("unknown action %q", action)
}
// run an action on a URL
func runURLAction(_ []string) error {
_ = wf.Configure(aw.TextErrors(true))
if URL == "" {
tab, err := mustClient().Tab(0)
if err != nil {
return err
}
URL = tab.URL
}
log.Printf("running action %q on URL %q ...", action, URL)
a, ok := urlActions[action]
if !ok {
return fmt.Errorf("unknown action %q", action)
}
return a.Run(URL)
}
// export variables containing info for currently-active tab
func runCurrentTabInfo(_ []string) error {
_ = wf.Configure(aw.TextErrors(true))
tab, err := mustClient().Tab(0)
if err != nil {
return err
}
if shellVars {
fmt.Printf("export FF_TAB=%d\n", tab.ID)
fmt.Printf("export FF_WINDOW=%d\n", tab.WindowID)
fmt.Printf("export FF_INDEX=%d\n", tab.Index)
fmt.Printf("export FF_TITLE=\"%s\"\n", tab.Title)
fmt.Printf("export FF_URL=\"%s\"\n", tab.URL)
return nil
}
av := aw.NewArgVars().
Var("FF_TAB", fmt.Sprintf("%d", tab.ID)).
Var("FF_WINDOW", fmt.Sprintf("%d", tab.WindowID)).
Var("FF_INDEX", fmt.Sprintf("%d", tab.Index)).
Var("FF_TITLE", tab.Title).
Var("FF_URL", tab.URL)
return av.Send()
}
// show actions for currently-active tab
func runCurrentTab(_ []string) error {
tab, err := mustClient().Tab(0)
if err != nil {
return err
}
tabID = tab.ID
URL = tab.URL
return runActions([]string{})
}
// inject JavaScript into specified tab. If tabID is 0, JS in injected
// into the active tab.
func runInject(args []string) error {
_ = wf.Configure(aw.TextErrors(true))
if len(args) != 1 {
return fmt.Errorf("inject command takes 1 argument, not %d", len(args))
}
js, err := mustClient().RunJS(RunJSArg{TabID: tabID, JS: args[0]})
if err != nil {
return err
}
fmt.Print(js)
return nil
}
// filter actions for tab or URL
func runActions(_ []string) error {
if tabID != 0 {
for _, a := range tabActions {
wf.NewItem(a.Name()).
UID(a.Name()).
Copytext(a.Name()).
Icon(a.Icon()).
Valid(true).
Var("CMD", "tab").
Var("ACTION", a.Name()).
Var("TAB", fmt.Sprintf("%d", tabID))
}
// add custom bookmarklet commands
for _, a := range loadCustomActions() {
if a.kind != "bookmarklet" {
continue
}
wf.NewItem(a.name).
UID(a.id).
Copytext("bml:"+a.id+","+a.name).
Icon(actionIcon(a.name, iconBookmarklet)).
Valid(true).
Var("CMD", "run-bookmarklet").
Var("BOOKMARK", a.id).
Var("TAB", fmt.Sprintf("%d", tabID))
}
}
if URL != "" {
for _, a := range urlActions {
if a.Name() == urlDefault {
continue
}
wf.NewItem(a.Name()).
UID(a.Name()).
Copytext(a.Name()).
Icon(a.Icon()).
Valid(true).
Var("CMD", "url").
Var("ACTION", a.Name()).
Var("URL", URL)
}
}
if query != "" {
_ = wf.Filter(query)
}
wf.WarnEmpty("No Matching Actions", "Try a different query?")
wf.SendFeedback()
return nil
}
// check if a newer version of workflow is available
func runUpdate(_ []string) error {
wf.Configure(aw.TextErrors(true))
log.Print("checking for update ...")
if err := wf.CheckForUpdate(); err != nil {
return err
}
if wf.UpdateAvailable() {
log.Println("a newer version of the workflow is available")
}
return nil
}
// show workflow status and options
func runStatus(_ []string) error {
if c, err := newClient(); err != nil {
wf.NewItem("No Connection to Browser").
Subtitle(err.Error()).
Icon(iconError)
} else {
if err := c.Ping(); err != nil {
wf.NewItem("No Connection to Browser").
Subtitle(err.Error()).
Icon(iconError)
} else {
wf.NewItem("Connected to Browser").
Subtitle("Extension is installed and running")
}
}
wf.NewItem("Register Workflow with Browser").
Subtitle("Use if you've updated or moved the workflow and it isn't working").
Autocomplete("workflow:register").
Icon(iconInstall).
Valid(false)
wf.NewItem("Install Browser Extension").
Subtitle("Get the browser extension to integrate this workflow with Firefox").
Arg(addonURL).
Valid(true).
Icon(iconAddon).
Var("CMD", "url").
Var("ACTION", urlDefault).
Var("URL", addonURL)
if wf.UpdateAvailable() {
wf.NewItem("Update Available").
Subtitle("↩ or ⇥ to install new version").
Autocomplete("workflow:update").
Icon(iconUpdateAvailable).
Valid(false)
} else {
wf.NewItem("Workflow is Up to Date").
Icon(iconUpdateOK).
Valid(false)
}
dir := filepath.Join(wf.DataDir(), "scripts")
wf.NewItem("Open Scripts Directory").
Subtitle("Open custom scripts directory in Finder").
Arg(dir).
Valid(true).
Icon(iconScript).
Var("CMD", "url").
Var("ACTION", "Open in Default Application").
Var("URL", dir)
wf.NewItem("Documentation").
Subtitle("Open documentation in your browser").
Arg(helpURL).
Valid(true).
Icon(iconDocs).
Var("CMD", "url").
Var("ACTION", urlDefault).
Var("URL", docsURL)
wf.NewItem("Report Issue").
Subtitle("Open issue tracker in your browser").
Arg(helpURL).
Valid(true).
Icon(iconIssue).
Var("CMD", "url").
Var("ACTION", urlDefault).
Var("URL", helpURL)
if query != "" {
wf.Filter(query)
}
wf.WarnEmpty("No Matching Items", "Try a different query?")
wf.SendFeedback()
return nil
}
func runDownloads(_ []string) error {
log.Printf("searching downloads for %q ...", query)
downloads, err := mustClient().Downloads(query)
if err != nil {
return err
}
for _, dl := range downloads {
wf.NewItem(filepath.Base(dl.Path)).
Subtitle(util.PrettyPath(dl.Path)).
Arg(dl.Path).
UID(dl.Path).
IsFile(true).
Icon(&aw.Icon{Value: dl.Path, Type: aw.IconTypeFileIcon}).
Valid(true).
Var("CMD", "open").
NewModifier(aw.ModCmd).
Subtitle("Reveal in Finder").
Var("CMD", "reveal")
}
wf.WarnEmpty("Nothing Found", "Try a different query?")
wf.SendFeedback()
return nil
}
// open file in default application
func runOpen(args []string) error {
path := args[0]
log.Printf("opening file %q ...", util.PrettyPath(path))
return exec.Command("/usr/bin/open", path).Run()
}
// reveal file in Finder
func runReveal(args []string) error {
path := args[0]
log.Printf("revealing file %q in Finder ...", util.PrettyPath(path))
return exec.Command("/usr/bin/open", "-R", path).Run()
}
// run update check in background
func checkForUpdate() {
if wf.UpdateCheckDue() && !wf.IsRunning("update") {
wf.RunInBackground("update", exec.Command(os.Args[0], "update"))
}
// TODO: show "update available" message
}