Skip to content

Commit 5adb01a

Browse files
author
Dean Sofer
committed
Small code cleanup
1 parent 57defc6 commit 5adb01a

File tree

6 files changed

+502
-450
lines changed

6 files changed

+502
-450
lines changed

background.js

Lines changed: 28 additions & 243 deletions
Original file line numberDiff line numberDiff line change
@@ -1,257 +1,42 @@
1-
// Generated by CoffeeScript 1.6.3
1+
var omni = new Omni(localStorage.setup === 'true');
22

3-
var Omni, omni;
4-
5-
Omni = (function () {
6-
Omni.prototype.debug = false;
7-
8-
Omni.prototype.urls = {
9-
github: 'https://github.com/',
10-
gist: 'https://gist.github.com/',
11-
api: 'https://api.github.com/',
12-
travis: 'https://travis-ci.org/',
13-
clone: 'github-mac://openRepo/https://github.com/',
14-
search: 'search?q=',
15-
io: function (repo) {
16-
repo = repo.split('/');
17-
return "http://" + repo[0] + ".github.io/" + repo[1];
18-
}
19-
};
20-
21-
Omni.prototype.api = null;
22-
23-
Omni.prototype.user = null;
24-
25-
Omni.prototype.caches = null;
26-
27-
function Omni() {
28-
this.api = new OAuth2('github', {
29-
client_id: '9b3a55174a275a8b56ce',
30-
client_secret: 'aea80effa00cc2b98c1cc590ade40ba05cbeea1e',
31-
api_scope: 'repo'
32-
});
33-
if (localStorage.setup === 'true') {
34-
this.authorize();
35-
}
36-
this.clearCache();
37-
}
38-
39-
Omni.prototype.redirect = function (url, fullPath) {
40-
if (!fullPath && url.indexOf("://") == -1) {
41-
url = this.urls.github + url;
42-
}
43-
if (this.debug) {
44-
alert(url);
45-
} else {
46-
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
47-
chrome.tabs.update(tabs[0].id, { url: url });
48-
});
49-
}
50-
};
51-
52-
Omni.prototype.suggest = function (text, suggester) {
53-
var suggestions, defaultSuggestionIndex = Infinity;
54-
if (this.caches.suggestions[text]) {
55-
suggestions = this.caches.suggestions[text];
56-
} else {
57-
suggestions = StepsManager.suggest(text);
58-
this.caches.suggestions[text] = suggestions;
59-
}
60-
Defer.allDone(suggestions, function (values, startingIndex) {
61-
if (startingIndex < defaultSuggestionIndex && values[0]) {
62-
defaultSuggestionIndex = startingIndex;
63-
chrome.omnibox.setDefaultSuggestion({ description: values[0].description });
64-
suggester(values.slice(1));
65-
} else {
66-
suggester(values);
67-
}
68-
});
69-
70-
if (defaultSuggestionIndex === Infinity) {
71-
chrome.omnibox.setDefaultSuggestion({description: "<dim>search for %s</dim>"});
72-
}
73-
};
74-
75-
Omni.prototype.decide = function (text, dontTrySuggestions) {
76-
var _this = this, decision = StepsManager.decide(text);
77-
Defer.eachDone(decision, function (url) {
78-
if (url === false) return;
79-
80-
if (url === null && text) {
81-
if (!dontTrySuggestions && _this.caches.suggestions[text] && _this.caches.suggestions[text][0]) {
82-
Defer.eachDone(_this.caches.suggestions[text][0], function (suggestion) {
83-
omni.decide(suggestion.content, true);
84-
});
85-
return; // stop
86-
}
87-
url = omni.urls.search + text;
88-
}
89-
_this.redirect(url);
90-
});
91-
};
92-
93-
Omni.prototype.authorize = function () {
94-
var _this = this;
95-
localStorage.setup = 'false';
96-
this.api.authorize(function () {
97-
localStorage.setup = 'true';
98-
_this.reset();
99-
});
100-
};
101-
102-
Omni.prototype.unauthorize = function () {
103-
if (this.api) {
104-
this.api.clearAccessToken();
105-
}
106-
localStorage.setup = 'false';
107-
};
108-
109-
Omni.prototype.query = function (options, callback) {
110-
var xhr;
111-
if (_.isString(options)) {
112-
options = {
113-
url: options
114-
};
115-
}
116-
_.defaults(options, {
117-
params: {},
118-
method: 'GET'
119-
});
120-
_.defaults(options.params, {
121-
per_page: 1000
122-
});
123-
if (this.api && this.api.getAccessToken()) {
124-
options.params.access_token = this.api.getAccessToken();
125-
}
126-
options.params = _.map(options.params, function (value, key) {
127-
return key + '=' + value;
128-
}).join('&');
129-
options.url = this.urls.api + options.url + "?" + options.params;
130-
131-
xhr = new XMLHttpRequest();
132-
xhr.onreadystatechange = function (event) {
133-
var data, err;
134-
if (xhr.readyState === 4) {
135-
if (xhr.status === 200) {
136-
data = JSON.parse(xhr.responseText);
137-
} else {
138-
err = xhr;
139-
}
140-
callback(err, data);
141-
}
142-
};
143-
xhr.open(options.method.toUpperCase(), options.url, true);
144-
xhr.setRequestHeader('Content-Type', 'application/json');
145-
xhr.send();
146-
};
147-
148-
Omni.prototype.getMyRepos = function () {
149-
var _this = this;
150-
this.query('user/repos', function (err, repos) {
151-
if (!err) Array.prototype.push.apply(_this.caches.my.repos, repos);
152-
});
153-
this.query('user/orgs', function (err, orgs) {
154-
if (!err) {
155-
_this.caches.my.orgs = orgs;
156-
_(orgs).each(function (org) {
157-
_this.query("orgs/" + org.login + "/repos", function (err, repos) {
158-
if (!err) Array.prototype.push.apply(_this.caches.my.repos, repos);
159-
});
160-
});
161-
}
162-
});
163-
};
164-
165-
Omni.prototype.getTheirRepos = function (user) {
166-
var _this = this,
167-
defer = Defer();
168-
//this.caches.their.user = user; TODO why?
169-
if (this.caches.their.repos[user]) {
170-
defer.resolve(this.caches.their.repos[user]);
171-
} else {
172-
this.query("users/" + user + "/repos", function (err, repos) {
173-
if (!err) {
174-
_this.caches.their.repos[user] = repos;
175-
defer.resolve(repos);
176-
} else {
177-
defer.resolve([]);
178-
}
179-
});
180-
}
181-
return defer;
182-
};
183-
184-
Omni.prototype.clearCache = function () {
185-
this.caches = {
186-
suggestions: {},
187-
my: {
188-
repos: [],
189-
orgs: [],
190-
following: [],
191-
gists: [],
192-
starred: []
193-
},
194-
their: {
195-
repos: {},
196-
user: null
197-
}
198-
};
199-
};
200-
201-
Omni.prototype.reset = function () {
202-
var _this = this;
203-
this.clearCache();
204-
205-
if (!this.api) return;
206-
207-
this.getMyRepos();
208-
this.query('gists', function (err, gists) {
209-
if (!err ) Array.prototype.push.apply(_this.caches.my.gists, gists);
210-
});
211-
this.query('gists/starred', function (err, gists) {
212-
if (!err) Array.prototype.push.apply(_this.caches.my.gists, gists);
213-
});
214-
this.query('user/following', function (err, users) {
215-
if (!err) _this.caches.my.following = users;
216-
});
217-
this.query({ url:'user/starred', params: { sort: 'updated' } }, function (err, repos) {
218-
if (!err) _this.caches.starred = repos;
219-
});
220-
this.query('user', function (err, data) {
221-
if (!err) _this.user = data.login;
222-
});
223-
};
224-
225-
Omni.prototype.setup = function () {
226-
if (_.isUndefined(localStorage.setup)) {
227-
if (confirm('Would you like to Authorize Github-Omnibox for personalized suggestions?')) {
228-
this.authorize();
229-
alert('You can unauthorize at any time by doing "gh my unauth"');
230-
} else {
231-
this.unauthorize();
232-
}
233-
}
234-
};
235-
236-
return Omni;
237-
238-
})();
239-
240-
omni = new Omni();
241-
242-
chrome.extension.onRequest.addListener(function (message/*, sender, sendResponse*/) {
3+
chrome.runtime.onMessage.addListener(function (message) {
2434
switch (message) {
2445
case 'authorize':
2456
omni.authorize();
7+
break;
2468
case 'decorate':
2479
chrome.extension.sendMessage(localStorage);
10+
break;
11+
case 'reset':
12+
omni.reset();
13+
alert('Cache has been cleared');
14+
break;
15+
case 'login':
16+
omni.authorize(function(){
17+
alert('Logged in');
18+
});
19+
break;
20+
case 'logout':
21+
omni.unauthorize();
22+
alert('Logged out');
23+
break;
24824
}
24925
});
25026

25127
chrome.omnibox.onInputChanged.addListener(omni.suggest.bind(omni));
25228

253-
chrome.omnibox.onInputStarted.addListener(omni.setup.bind(omni));
29+
chrome.omnibox.onInputStarted.addListener(function () {
30+
if (_.isUndefined(localStorage.setup)) {
31+
if (confirm('Would you like to Authorize Github-Omnibox for personalized suggestions?')) {
32+
omni.authorize();
33+
alert('You can unauthorize at any time by doing "gh my unauth"');
34+
} else {
35+
omni.unauthorize();
36+
}
37+
}
38+
});
25439

25540
chrome.omnibox.onInputEntered.addListener(function (text) {
25641
if (text) omni.decide(text);
257-
});
42+
});

0 commit comments

Comments
 (0)