-
Notifications
You must be signed in to change notification settings - Fork 32
/
tabs.go
221 lines (181 loc) · 8.45 KB
/
tabs.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
package chrome
import "github.com/gopherjs/gopherjs/js"
type Tabs struct {
o *js.Object
}
/*
* Types
*/
type Tab struct {
*js.Object
Id int `js:"id"`
Index int `js:"index"`
WindowId int `js:"windowId"`
OpenerTabId int `js:"openerTabId"`
Selected bool `js:"selected"`
Highlighted bool `js:"highlighted"`
Active bool `js:"active"`
Pinned bool `js:"pinned"`
Url string `js:"url"`
Title string `js:"title"`
FavIconUrl string `js:"favIconUrl"`
Status string `js:"status"`
Incognito bool `js:"incognito"`
Width int `js:"width"`
Height int `js:"height"`
SessionId string `js:"sessionId"`
}
type ZoomSettings map[string]string
/*
* Methods:
*/
// Get retrieves details about the specified tab.
func (t *Tabs) Get(tabId int, callback func(tab Tab)) {
t.o.Call("get", tabId, callback)
}
// GetCurrent gets the tab that this script call is being made from.
// May be undefined if called from a non-tab context (for example: a background page or popup view).
func (t *Tabs) GetCurrent(callback func(tab Tab)) {
t.o.Call("getCurrent", callback)
}
// Connect Connects to the content script(s) in the specified tab.
// The runtime.onConnect event is fired in each content script running in the specified tab
// for the current extension. For more details, see Content Script Messaging (https://developer.chrome.com/extensions/messaging)
func (t *Tabs) Connect(tabId int, connectInfo interface{}) {
t.o.Call("connect", tabId, connectInfo)
}
// SendMessage sends a single message to the content script(s) in the specified tab,
// with an optional callback to run when a response is sent back. The runtime.onMessage
// event is fired in each content script running in the specified tab for the current extension.
func (t *Tabs) SendMessage(tabId int, message interface{}, responseCallback func(response Object)) {
t.o.Call("sendMessage", tabId, message, responseCallback)
}
// GetSelected gets the tab that is selected in the specified window.
func (t *Tabs) GetSelected(windowId int, callback func(tab Tab)) {
t.o.Call("getSelected", windowId, callback)
}
// GetAllInWindow gets details about all tabs in the specified window.
func (t *Tabs) GetAllInWindow(windowId int, callback func(tabs []Tab)) {
t.o.Call("getAllInWindow", windowId, callback)
}
// Create creates a new tab.
func (t *Tabs) Create(createProperties interface{}, callback func(tab Tab)) {
t.o.Call("create", createProperties, callback)
}
// Duplicate duplicates a tab.
func (t *Tabs) Duplicate(tabId int, callback func(tab Tab)) {
t.o.Call("duplicate", tabId, callback)
}
// Query gets all tabs that have the specified properties, or all tabs if no properties are specified.
func (t *Tabs) Query(queryInfo interface{}, callback func(result []Tab)) {
t.o.Call("query", queryInfo, callback)
}
// Highlight highlights the given tabs
func (t *Tabs) Highlight(highlightInfo interface{}, callback func(js.Object)) {
t.o.Call("highlight", highlightInfo, callback)
}
// Update modifies the properties of a tab. Properties that are not specified in updateProperties are not modified.
func (t *Tabs) Update(tabId int, updateProperties interface{}, callback func(tab Tab)) {
t.o.Call("update", updateProperties, callback)
}
// Move moves one or more tabs to a new position within its window, or to a new window.
// Note that tabs can only be moved to and from normal windows.
func (t *Tabs) Move(tabIds []interface{}, moveProperties interface{}, callback func(tabs []Tab)) {
t.o.Call("move", tabIds, moveProperties, callback)
}
// Reload reloads a tab.
func (t *Tabs) Reload(tabId int, reloadProperties interface{}, callback func(js.Object)) {
t.o.Call("reload", tabId, reloadProperties, callback)
}
// Remove closes one or more tabs.
func (t *Tabs) Remove(tabIds []interface{}, callback func(js.Object)) {
t.o.Call("remove", tabIds, callback)
}
// DetectLanguage detects the primary language of the content in a tab.
func (t *Tabs) DetectLanguage(tabId int, callback func(language string)) {
t.o.Call("detectLanguage", tabId, callback)
}
// CaptureVisibleTab captures the visible area of the currently active tab in the specified window.
// You must have <all_urls> permission to use this method.
func (t *Tabs) CaptureVisibleTab(windowId int, options interface{}, callback func(dataUrl string)) {
t.o.Call("captureVisibleTab", windowId, options, callback)
}
// ExecuteScript injects JavaScript code into a page. For details, see the programmatic
// injection section of the content scripts doc: (https://developer.chrome.com/extensions/content_scripts#pi)
func (t *Tabs) ExecuteScript(tabId int, details interface{}, callback func(result []interface{})) {
t.o.Call("executeScript", tabId, details, callback)
}
// InsertCss injects CSS into a page. For details, see the programmatic injection
// section of the content scripts doc. (https://developer.chrome.com/extensions/content_scripts#pi)
func (t *Tabs) InsertCss(tabId int, details interface{}, callback func()) {
t.o.Call("insertCss", tabId, details, callback)
}
// SetZoom zooms a specified tab.
func (t *Tabs) SetZoom(tabId int, zoomFactor int64, callback func()) {
t.o.Call("setZoom", tabId, zoomFactor, callback)
}
// GetZoom gets the current zoom factor of a specified tab.
func (t *Tabs) GetZoom(tabId int, callback func(zoomFactor int64)) {
t.o.Call("getZoom", tabId, callback)
}
// SetZoomSettings sets the zoom settings for a specified tab, which define how zoom changes are handled.
// These settings are reset to defaults upon navigating the tab.
func (t *Tabs) SetZoomSettings(tabId int, zoomSettings ZoomSettings, callback func()) {
t.o.Call("setZoomSettings", tabId, zoomSettings, callback)
}
// GetZoomSettings gets the current zoom settings of a specified tab.
func (t *Tabs) GetZoomSettings(tabId int, callback func(zoomSettings ZoomSettings)) {
t.o.Call("getZoomSettings", tabId, callback)
}
/*
* Events
*/
// OnCreated is fired when a tab is created. Note that the tab's URL may not be set at the time
// this event fired, but you can listen to onUpdated events to be notified when a URL is set.
func (t *Tabs) OnCreated(callback func(tab Tab)) {
t.o.Get("onCreated").Call("addListener", callback)
}
// OnUpdated fired when a tab is updated.
func (t *Tabs) OnUpdated(callback func(tabId int, changeInfo Object, tab Tab)) {
t.o.Get("onUpdated").Call("addListener", callback)
}
// OnMoved fired when a tab is moved within a window. Only one move event is fired,
// representing the tab the user directly moved. Move events are not fired for the
// other tabs that must move in response. This event is not fired when a tab is moved between windows.
func (t *Tabs) OnMoved(callback func(tabId int, movedInfo Object)) {
t.o.Get("onMoved").Call("addListener", callback)
}
// OnActivated fires when the active tab in a window changes. Note that the tab's URL
// may not be set at the time this event fired, but you can listen to onUpdated events
// to be notified when a URL is set.
func (t *Tabs) OnActivated(callback func(activeInfo Object)) {
t.o.Get("onActivated").Call("addListener", callback)
}
// OnHighlightChanged fired when the highlighted or selected tabs in a window changes.
func (t *Tabs) OnHighlightChanged(callback func(selectInfo Object)) {
t.o.Get("onHighlightChanged").Call("addListener", callback)
}
// OnHighlighted fired when the highlighted or selected tabs in a window changes.
func (t *Tabs) OnHighlighted(callback func(highlightInfo Object)) {
t.o.Get("onHighlighted").Call("addListener", callback)
}
// OnDetached fired when a tab is detached from a window, for example because it is being moved between windows.
func (t *Tabs) OnDetached(callback func(tabId int, detachInfo Object)) {
t.o.Get("onDetached").Call("addListener", callback)
}
// OnAttached fired when a tab is attached to a window, for example because it was moved between windows.
func (t *Tabs) OnAttached(callback func(tabId int, attachInfo Object)) {
t.o.Get("onAttached").Call("addListener", callback)
}
// OnRemoved fired when a tab is closed.
func (t *Tabs) OnRemoved(callback func(tabId int, removeInfo Object)) {
t.o.Get("onRemoved").Call("addListener", callback)
}
// OnReplaced fired when a tab is replaced with another tab due to prerendering or instant.
func (t *Tabs) OnReplaced(callback func(addedTabId int, removedTabId int)) {
t.o.Get("onReplaced").Call("addListener", callback)
}
// OnZoomChange fired when a tab is zoomed.
func (t *Tabs) OnZoomChange(callback func(zoomChangeInfo Object)) {
t.o.Get("onZoomChange").Call("addListener", callback)
}