-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
217 lines (191 loc) · 6.29 KB
/
background.js
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
// Environment variables ───────────────────────────────────────────────────────
switch (true) {
case (typeof browser !== 'undefined'):
var PLATFORM = 'firefox'
var SHELL_EXTENSION_ID = 'shell@alexherbo2.github.com'
break
case (typeof chrome !== 'undefined'):
var PLATFORM = 'chrome'
var SHELL_EXTENSION_ID = 'ohgecdnlcckpfnhjepfdcdgcfgebkdgl'
break
}
// Extensions ──────────────────────────────────────────────────────────────────
// Shell
const shell = {}
shell.port = chrome.runtime.connect(SHELL_EXTENSION_ID)
// Settings ────────────────────────────────────────────────────────────────────
const settings = {}
// Pipe tabs through the given external filter program.
settings.dmenu = {
command: 'dmenu',
arguments: []
}
// Sync settings
chrome.storage.sync.get(null, (items) => {
Object.assign(settings, items)
})
chrome.storage.onChanged.addListener((changes, namespace) => {
for (const key in changes) {
const storageChange = changes[key]
settings[key] = storageChange.newValue
}
})
// dmenu ───────────────────────────────────────────────────────────────────────
// dmenu
const dmenu = (id, menu) => {
return new Promise((resolve, reject) => {
const input = Object.keys(menu).join('\n')
shell.port.postMessage({
id,
input,
command: settings.dmenu.command,
arguments: settings.dmenu.arguments
})
shell.port.onMessage.addListener((response) => {
if (response.id !== id) {
reject(response.id)
} else {
const keys = response.output.split('\n')
const results = keys.flatMap((key) => {
const item = menu[key]
return item
? [item]
: []
})
resolve(results)
}
})
})
}
const getTabMenu = () => {
return new Promise((resolve, reject) => {
chrome.tabs.query({}, (tabs) => {
const menu = {}
for (const tab of tabs) {
const key = `${tab.title} ${tab.url} ${tab.id}`
menu[key] = tab
}
resolve(menu)
})
})
}
const getBookmarkMenu = () => {
return new Promise((resolve, reject) => {
chrome.bookmarks.search({}, (bookmarks) => {
const menu = {}
for (const bookmark of bookmarks) {
if (bookmark.children) {
continue
}
const key = `${bookmark.title} ${bookmark.url}`
menu[key] = bookmark
}
resolve(menu)
})
})
}
const getHistoryMenu = () => {
return new Promise((resolve, reject) => {
chrome.history.search({ text: '' }, (history) => {
const menu = {}
for (const item of history) {
const key = `${item.title} ${item.url}`
menu[key] = item
}
resolve(menu)
})
})
}
// Commands ────────────────────────────────────────────────────────────────────
// Keyboard shortcuts
// https://developer.chrome.com/extensions/commands
//
// Entry point: Listen for incoming requests.
chrome.commands.onCommand.addListener((name) => {
const command = commands[name]
if (command) {
command()
}
})
const commands = {}
// Tab search
commands['tab-search'] = async () => {
const menu = await getTabMenu()
const [tab] = await dmenu('tab-search', menu)
if (! tab) {
return
}
// Does not affect whether the window is focused
chrome.tabs.update(tab.id, { active: true })
chrome.windows.update(tab.windowId, { focused: true })
}
// Bring tab
commands['bring-tab'] = async () => {
const menu = await getTabMenu()
const targetTabs = await dmenu('bring-tab', menu)
if (targetTabs.length === 0) {
return
}
const targetTabIds = targetTabs.map((tab) => tab.id)
chrome.tabs.query({ currentWindow: true, active: true }, ([currentTab]) => {
const pinned = currentTab.pinned
// Issue: Moving a tab to another window does not preserve the pinned state.
chrome.tabs.move(targetTabIds, { windowId: currentTab.windowId, index: -1 }, (tabs) => {
for (const tabId of targetTabIds) {
chrome.tabs.update(tabId, { pinned })
}
chrome.tabs.move(targetTabIds, { index: currentTab.index + 1 })
})
})
}
// Open bookmark
commands['open-bookmark'] = async () => {
const menu = await getBookmarkMenu()
const [bookmark] = await dmenu('open-bookmark', menu)
if (! bookmark) {
return
}
chrome.tabs.update(undefined, { url: bookmark.url })
}
// Search history
commands['search-history'] = async () => {
const menu = await getHistoryMenu()
const [historyItem] = await dmenu('search-history', menu)
if (! historyItem) {
return
}
chrome.tabs.update(undefined, { url: historyItem.url })
}
// External ────────────────────────────────────────────────────────────────────
// Cross-extension messaging
// https://developer.chrome.com/extensions/messaging#external
//
// Entry point: Listen for incoming requests.
// Each request has the following format:
// {
// command: String,
// arguments: Array
// }
chrome.runtime.onConnectExternal.addListener((port) => {
port.onMessage.addListener((request) => {
const command = external.requests[request.command]
const arguments = request.arguments || []
const self = {
port
}
if (command) {
command.apply(self, arguments)
}
})
})
external = {}
// Requests
external.requests = {}
external.requests['set'] = (items) => {
chrome.storage.sync.set(items)
}
// Forward to commands
external.requests['tab-search'] = commands['tab-search']
external.requests['bring-tab'] = commands['bring-tab']
external.requests['open-bookmark'] = commands['open-bookmark']
external.requests['search-history'] = commands['search-history']