-
Notifications
You must be signed in to change notification settings - Fork 20
/
models.go
101 lines (87 loc) · 3.6 KB
/
models.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
// Copyright (c) 2020 Dean Jackson <[email protected]>
// MIT Licence applies http://opensource.org/licenses/MIT
package main
import (
"fmt"
"net/url"
"strings"
)
/*
type Window struct {
ID int `json:"id"`
Title string `json:"title"`
Active bool `json:"active"`
Tabs []Tab `json:"tabs"`
}
func (w Window) String() string {
return fmt.Sprintf("Window(id=%d, title=%q, active=%v)", w.ID, w.Title, w.Active)
}
*/
// Tab represents a Firefox tab. It contains a subset of the properties
// of the tab.Tab object from Firefox's extensions API.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/Tab
type Tab struct {
ID int `json:"id"` // unique ID of tab
WindowID int `json:"windowId"` // unique ID of window tab belongs to
Index int `json:"index"` // position of tab in window
Title string `json:"title"` // tab's title
URL string `json:"url"` // tab's URL
Active bool `json:"active"` // whether tab is the active tab in its window
}
func (t Tab) String() string {
return fmt.Sprintf("Tab(id=%d, title=%q, url=%q, active=%v)", t.ID, t.Title, t.URL, t.Active)
}
// Bookmark represents a Firefox bookmark. It contains a subset of the properties
// of the bookmarks.BookmarkTreeNode object from the extensions API.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/bookmarks/BookmarkTreeNode
type Bookmark struct {
ID string `json:"id"` // unique ID
Title string `json:"title"` // bookmark title
Type string `json:"type"` // "bookmark" or "folder"
URL string `json:"url"` // only present for type "bookmark"
ParentID string `json:"parentId"` // ID of folder bookmark belongs to
Index int `json:"index"` // position in containing folder
}
func (bm Bookmark) String() string {
return fmt.Sprintf("Bookmark(id=%q, title=%q, url=%q)", bm.ID, bm.Title, bm.URL)
}
// IsBookmarklet returns true of bookmark URL starts with "javascript:"
func (bm Bookmark) IsBookmarklet() bool {
return strings.HasPrefix(bm.URL, "javascript:")
}
// JavaScript extracts JS code from a bookmarklet's URL. Returns an empty string
// if Bookmark is not a bookmarklet.
func (bm Bookmark) JavaScript() string {
if !bm.IsBookmarklet() {
return ""
}
s := strings.TrimPrefix(bm.URL, "javascript:")
s, _ = url.PathUnescape(s)
return s
}
// History is an entry from the browser history. It contains a subset of the properties
// of a native history.HistoryItem object.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/history/HistoryItem
type History struct {
ID string `json:"id"` // unique ID
Title string `json:"title"` // page title
URL string `json:"url"` // page URL
}
func (h History) String() string {
return fmt.Sprintf("History(id=%q, title=%q, url=%q)", h.ID, h.Title, h.URL)
}
// Download is a file downloaded by Firefox. Contains a subset of the properties
// of a Firefox downloads.DownloadItem object.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/downloads/DownloadItem
type Download struct {
ID int `json:"id"` // unique ID
Path string `json:"path"` // absolute filepath to downloaded file
Size int64 `json:"size"` // size of file in bytes
URL string `json:"url"` // URL file was downloaded from
MimeType string `json:"mime"` // mime type of file
Exists bool `json:"exists"` // whether Path still exists on disk
Err string `json:"error"` // error message
}
func (d Download) String() string {
return fmt.Sprintf("Download(id=%q, path=%q, url=%q)", d.ID, d.Path, d.URL)
}