forked from ProLoser/Github-Omnibox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.coffee
337 lines (300 loc) · 12.9 KB
/
background.coffee
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
class Omni
debug: false
urls:
github: 'https://github.com/'
api: 'https://api.github.com/'
travis: 'https://travis-ci.org/'
clone: 'github-mac://openRepo/https://github.com/'
search: 'search?q='
io: (repo) ->
repo = repo.split('/')
"http://#{repo[0]}.github.io/#{repo[1]}"
text: null
api: null
user: null
actions:
'user': ['followers', 'following', 'starred', 'repositories', 'activity']
'my': ['issues', 'dash', 'pulls', 'settings']
'new': ['issue', 'release']
'repo': ['io', 'pulls', 'network', 'pulse', 'settings', 'issues', 'contributors', 'compare', 'wiki', 'graphs', '#', 'tags', 'releases']
caches:
my:
repos: []
orgs: []
their:
repos: {}
user: null
constructor: ->
@setup()
redirect: (url, fullPath) ->
url = @urls.github + url unless fullPath
if @debug
alert url
else
chrome.tabs.getSelected null, (tab) ->
chrome.tabs.update tab.id,
url: url
filter: (search, collection, anywhere) ->
return collection unless search
results = []
_.each collection, (item) =>
match = item.content.toLowerCase().indexOf(search.toLowerCase())
results.push item if anywhere and ~match or !match
@setDefault item.description if search is item.content
results
setDefault: (description) ->
chrome.omnibox.setDefaultSuggestion description: description
suggest: (@text, @suggester) ->
split = @text.split(' ')
suggestions = []
@powerPush suggestions, "my ", @actions.my
@powerPush suggestions, "my ", @actions.user
@powerPush suggestions, "!", @actions.repo, 'this repo '
@powerPush suggestions, "!new ", @actions.new, 'this repo '
@powerPush suggestions, "new ", ['repo']
switch true
when !!@text.match /^@\w+/
### @user ###
@powerPush suggestions, "#{split[0]} ", @actions.user
when !!@text.match /^\w+\/[\w-\.]+ /
### 'user/repo ' ###
@powerPush suggestions, "#{split[0]} new ", @actions.new
@powerPush suggestions, "#{split[0]} ", @actions.repo
when !!@text.match /^\w+\//
### user/ ###
@getTheirRepos split[0].split('/')[0], (repos) =>
@suggester @filter @text, @suggestionsFromRepos repos
when !!@text.match /^\/[\w-\.]*/
### /repo ###
Array::unshift.apply suggestions, @suggestionsFromRepos @caches.my.repos
@suggester @filter @text, suggestions, true
return
@suggester @filter @text, suggestions
powerPush: (destination, prefix = '', source, descriptionPrefix = prefix) ->
Array::unshift.apply destination, _.map source, (item) =>
description: descriptionPrefix + item
content: prefix + item
getCurrentRepo: (callback) ->
chrome.tabs.getSelected null, (tab) =>
if match = tab.url.match /github\.com\/(([\w-]+)\/([\w-\.]+))/
user = match[2]
repo = match[3]
else if match = tab.url.match /([\w-]+)\.github\.io\/([\w-\.]+)/
user = match[1]
repo = match[2]
callback user, repo
decide: (@text) ->
split = @text.split(' ')
switch true
when split[0] is 'new'
### new whatever ###
switch true
when split[1] is 'repo'
### new repo ###
url = 'new'
when !!@text.match /^my/
### my ###
switch true
when split[1] is 'auth'
@authorize()
alert 'You can unauthorize at any time by doing "gh my unauth"'
url = false
when split[1] is 'unauth'
@unauthorize()
url = false
when split[1] is 'reset'
@reset()
url = false
alert 'Cache has been cleared'
when !!~['dash', 'dashboard', 'home', 'news', 'feed'].indexOf(split[1])
url = ''
when !!~['stars', 'starred'].indexOf(split[1])
url = 'stars'
if split[2]
url += "?q=#{split.slice(2).join(' ')}"
when !!~['following', 'followers'].indexOf(split[1])
url = "#{@user}/#{split[1]}"
when !!~['pulls', 'issues', 'settings'].indexOf(split[1])
url = "dashboard/#{split[1]}"
when !!~['repositories', 'activity'].indexOf(split[1])
url = "#{@user}?tab=#{split[1]}"
when !split[1]
url = @user
when !!@text.match /^@\w+/
### @user ###
url = "#{split[0].substr(1)}/"
if split[1]
### @user whatever ###
switch true
when split[1] is 'starred'
url += 'following#starred'
when !!~['repositories', 'activity'].indexOf(split[1])
url += "?tab=#{split[1]}"
when !!~['followers', 'following'].indexOf(split[1])
url += split[1]
when !!@text.match(/^[\w-]+\/[\w-\.]+/), !!@text.match(/^!\w+/), !!@text.match(/^\/[\w-\.]+/)
### user/repo ###
### /repo ###
if @text[0] is '/'
split[0] = @user + split[0]
### !x ###
if @text[0] is '!'
split.unshift('##/@@')
split[1] = split[1].substr(1)
### user/repo x ###
if split[1]
### user/repo whatever ###
switch true
when split[1] is 'new' and split[2] and !!split[2].match(/^(issue|pull|release)/)
### user/repo new ###
url = "#{split[0]}/#{split[2]}s/new"
when !!split[1].match /^(io|pages)$/
### user/repo io ###
url = @urls.io(split[0])
fullPath = true
when split[1] is 'clone'
### user/repo clone ###
url = @urls.clone + split[0]
fullPath = true
when !!split[1].match /^(travis|travis-ci|ci)$/
### user/repo travis ###
url = @urls.travis + split[0]
fullPath = true
when !!split[1].match /^(issues|code)$/ and !!split[2]
url = "#{split[0]}/#{@urls.search}#{split.slice(2).join('+')}&type=#{split[1]}"
when !!split[1].match /^#[0-9]+/
url = "#{split[0]}/issues/#{split[1].substr(1)}"
when !!~@actions.repo.indexOf(split[1])
### user/repo whatever ###
url = "#{split[0]}/#{split[1]}/"
when !!split[1].match(/^@/), !!split[1].match /^\/.*/
### user/repo @branch ###
### user/repo /path ###
branch = 'master'
path = ''
switch true
when split[1][0] is '@'
branch = split[1].substr(1)
path = split[2]
when split[2][0] is '@'
branch = split[2].substr(1)
path = split[1]
else
path = split[1]
switch true
when path is '/'
url = "#{split[0]}/find/#{branch}"
else
url = "#{split[0]}/blob/#{branch}#{path}"
if split[0] is '##/@@'
newUrl = url
url = false
@getCurrentRepo (user, repo) =>
if repo and newUrl
url = newUrl.replace('##', user).replace('@@', repo)
else if repo
url = "#{user}/#{repo}/#{@urls.search}#{@text.substr(1)}"
else
url = @urls.search + @text
@redirect url, fullPath
else
if !url
url = "#{split[0]}/"
url += @urls.search + split.slice(1).join(' ') if split[1]
if url isnt false
url = @urls.search + @text if !url
@redirect url, fullPath
authorize: ->
localStorage.setup = true
github = new OAuth2 'github',
client_id: '9b3a55174a275a8b56ce'
client_secret: 'aea80effa00cc2b98c1cc590ade40ba05cbeea1e'
api_scope: 'repo'
github.authorize =>
# Ready for action, can now make requests with token
@api = github
@query 'user', (err, data) =>
@user = data.login
@getMyRepos()
unauthorize: ->
if @api
@api.clearAccessToken()
localStorage.setup = false
alert 'You can authorize at any time by doing "gh my auth"'
query: (url, callback, method = 'GET') ->
xhr = new XMLHttpRequest();
xhr.onreadystatechange = (event) =>
if xhr.readyState is 4
if xhr.status is 200
data = JSON.parse xhr.responseText
else
err = xhr
callback err, data
url = "#{@urls.api}#{url}"
if @api
url = "#{url}?access_token=#{@api.getAccessToken()}&per_page=1000"
xhr.open method.toUpperCase(), url, true
xhr.setRequestHeader 'Content-Type', 'application/json'
xhr.send()
getMyRepos: ->
@query 'user/repos', (err, repos) =>
Array::push.apply @caches.my.repos, repos
@query 'user/orgs', (err, orgs) =>
@caches.my.orgs = orgs
_(orgs).each (org) =>
@query "orgs/#{org.login}/repos", (err, repos) =>
Array::push.apply @caches.my.repos, repos
getTheirRepos: (user, callback) ->
@caches.their.user = user
myback = =>
callback(@caches.their.repos[user])
if @caches.their.repos[user]
myback()
else
@query "users/#{user}/repos", (err, repos) =>
@caches.their.repos[user] = repos
myback()
suggestionsFromRepos: (repos) ->
suggestions = []
_(repos).each (repo) =>
suggestions.push
description: repo.full_name
content: repo.full_name
suggestions
reset: ->
@caches.my =
repos: []
orgs: []
@cancel()
if @api
@getMyRepos()
cancel: ->
@caches.their =
repos: {}
user: null
setup: ->
if localStorage.setup
@authorize()
else if localStorage.setup?
if confirm 'Would you like to Authorize Github-Omnibox for personalized suggestions?'
@authorize()
alert 'You can unauthorize at any time by doing "gh my unauth"'
else
@unauthorize()
omni = new Omni()
# omni.debug = true
# This event is triggered by options.html
chrome.extension.onRequest.addListener (message, sender, sendResponse) ->
switch message
when !!'authorize'
omni.authorize()
# This event is fired each time the user updates the text in the omnibox,
# as long as the extension's keyword mode is still active.
chrome.omnibox.onInputChanged.addListener omni.suggest.bind omni
# This event is fired when !!the user starts the omnibox
chrome.omnibox.onInputStarted.addListener ->
# This event is fired with the user accepts the input in the omnibox.
chrome.omnibox.onInputEntered.addListener omni.decide.bind omni
# chrome.omnibox.onInputEntered.addListener omni.clear.bind omni
# This event is fired when !!the user cancels the omnibox
# chrome.omnibox.onInputCancelled.addListener omni.clear.bind omni