-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
290 lines (269 loc) · 7.93 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
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
(function() {
var TWITTER_URL = 'https://twitter.com/';
var STOR_WIDTH_KEY = 'window_width';
var STOR_HEIGHT_KEY = 'window_height';
var STOR_TOP_KEY = 'window_top';
var STOR_LEFT_KEY = 'window_left';
var existingTwitterWindow;
var mostRecentBrowserWindow;
var saveWindowDimensions = function(w) {
localStorage[STOR_WIDTH_KEY] = w.width.toString(10);
localStorage[STOR_HEIGHT_KEY] = w.height.toString(10);
localStorage[STOR_TOP_KEY] = w.top.toString(10);
localStorage[STOR_LEFT_KEY] = w.left.toString(10);
};
var getWindowDimensions = function() {
if(localStorage[STOR_WIDTH_KEY] !== undefined) {
return {
width: localStorage[STOR_WIDTH_KEY],
height: localStorage[STOR_HEIGHT_KEY],
top: localStorage[STOR_TOP_KEY],
left: localStorage[STOR_LEFT_KEY]
};
} else {
return {
width: '410',
height: '700',
top: '0',
left: '0'
};
}
};
// note: "best" is currently defined as the most recently focused
var findBestWindow = function(callback) {
if(mostRecentBrowserWindow !== undefined) {
chrome.windows.get(mostRecentBrowserWindow.id, {populate: true}, function(window) {
callback(window);
});
} else {
chrome.windows.getAll({populate:true}, function(windows) {
var firstNormalWindow = _.find(windows, function(window) {
return window.type === "normal";
});
mostRecentBrowserWindow = firstNormalWindow;
callback(firstNormalWindow);
});
}
};
// open twitter.com in a (mostly) chrome-less window
var openResponsiveTwitterWindow = function(callback) {
getExistingTwitterWindow(function(existingWindow) {
if(existingWindow !== undefined) {
chrome.windows.update(existingWindow.id, { focused: true });
if(callback) {
callback();
}
} else {
var dimensions = getWindowDimensions();
chrome.windows.create({
url: TWITTER_URL,
focused: true,
width: parseInt(dimensions.width, 10),
height: parseInt(dimensions.height, 10),
top: parseInt(dimensions.top, 10),
left: parseInt(dimensions.left, 10),
type: 'popup'
}, function(newWindow) {
existingTwitterWindow = newWindow;
if(callback) {
callback();
}
});
}
});
};
// open a url in a new tab, highlight (or focus it), but keep primary focus
// on twitter window.
var openNewTabInBestWindow = function(url) {
findBestWindow(function(window) {
if(window === undefined) {
var hasRecentWindow = mostRecentBrowserWindow !== undefined;
return chrome.windows.create({
url: url,
focused: true,
width: hasRecentWindow ? mostRecentBrowserWindow.width : 1024,
height: hasRecentWindow ? mostRecentBrowserWindow.height : 600,
left: hasRecentWindow ? mostRecentBrowserWindow.left : 25,
top: hasRecentWindow ? mostRecentBrowserWindow.top : 25
});
}
chrome.tabs.create({
url: url,
windowId: window.id,
active: false
}, function(tab) {
chrome.tabs.highlight({
windowId: window.id,
tabs: window.tabs.length
}, function() {});
});
});
};
var getExistingTwitterWindow = function(callback) {
if(existingTwitterWindow === undefined) {
return callback();
}
chrome.windows.getAll({populate: false /* no tabs */}, function(windows) {
callback(_.find(windows, function(w) {
return w.id == existingTwitterWindow.id;
}));
});
};
chrome.windows.onFocusChanged.addListener(function(windowId) {
chrome.windows.get(windowId, {populate: true}, function(window) {
if(window !== undefined && window.type == "normal") {
mostRecentBrowserWindow = window;
}
});
});
var navigateTo = function(path) {
getExistingTwitterWindow(function(existingWindow) {
chrome.windows.get(existingWindow.id, { populate: true }, function(w) {
chrome.tabs.update(w.tabs[0].id, {
url: TWITTER_URL + path
});
});
});
};
var requestHandlers = {
openLink: function(request) {
openNewTabInBestWindow(request.url);
},
saveDimensions: function(request) {
// only save if we have the twitter window open
// i.e. don't save for a tab in main browser
getExistingTwitterWindow(function(w) {
if(w !== undefined) {
saveWindowDimensions(request.dimensions);
}
});
}
};
var omniboxKeyboardShortcuts = {
o: {
location: '',
description: 'Open Twitter'
},
h: {
location: '',
description: 'Go Home'
},
c: {
location: 'i/connect',
description: 'Go to Connect'
},
a: {
location: 'activity',
description: 'Go to Activity'
},
r: {
location: 'mentions',
description: 'Go to Mentions'
},
d: {
location: 'i/discover',
description: 'Go Discover'
},
//p: 'TODO', // Profile
f: {
location: 'favorites',
description: 'Go Favorites'
},
//l: 'TODO', // Lists
m: {
location: 'messages',
description: 'Go Messages'
}
};
var omniboxHandlers = {
keyboardShortcuts: {
canHandle: function(text) {
return text.length === 1 && omniboxKeyboardShortcuts.hasOwnProperty(this.l(text));
},
getDefaultSuggestion: function(text) {
return {
description: omniboxKeyboardShortcuts[this.l(text)].description
};
},
handle: function(text) {
// if 'o', just open twitter, don't navigate
if(this.l(text) == 'o') return;
navigateTo(omniboxKeyboardShortcuts[this.l(text)].location);
},
getSuggestions: function(text) {
return _.map(_.keys(omniboxKeyboardShortcuts), function(key) {
return {
content: key,
description: omniboxKeyboardShortcuts[key].description
};
});
},
l: function(text) { return text.toLowerCase(); }
},
userLookup: {
canHandle: function(text) {
return text[0] === '@';
},
getDefaultSuggestion: function(text) {
return {
description: 'Go to user ' + this.getUserName(text)
};
},
handle: function(text) {
navigateTo(this.getUserName(text));
},
getSuggestions: function(text) {
return [];
},
getUserName: function(text) {
return text.substr(1);
}
},
search: {
canHandle: function(text) {
return true;
},
getDefaultSuggestion: function(text) {
return {
description: "Search Twitter for '%s'"
};
},
handle: function(text) {
navigateTo('search/' + escape(text));
},
getSuggestions: function(text) {
return [];
}
}
};
chrome.extension.onRequest.addListener(function(request) {
requestHandlers[request.action](request);
});
chrome.omnibox.setDefaultSuggestion({
description: "Search Twitter for '%s'"
});
chrome.omnibox.onInputChanged.addListener(function(text, suggest) {
for(var h in omniboxHandlers) {
var handler = omniboxHandlers[h];
if(omniboxHandlers.hasOwnProperty(h) && handler.canHandle(text)) {
chrome.omnibox.setDefaultSuggestion(
handler.getDefaultSuggestion(text)
);
return suggest(handler.getSuggestions(text));
}
}
});
chrome.omnibox.onInputEntered.addListener(function(text) {
openResponsiveTwitterWindow(function() {
for(var h in omniboxHandlers) {
var handler = omniboxHandlers[h];
if(omniboxHandlers.hasOwnProperty(h) && handler.canHandle(text)) {
return handler.handle(text);
}
}
});
});
chrome.browserAction.onClicked.addListener(function(tab) {
openResponsiveTwitterWindow();
});
})();