-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
185 lines (153 loc) · 4.96 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
'use strict';
var enableDebug = false;
function log() {
console.log.apply(console, arguments);
}
function debug() {
if (enableDebug) {
console.debug.apply(console, arguments);
}
}
// Simple utility to start a promise chain
function chain(opts) {
return new Promise(function(resolve, reject) {
resolve(opts);
})
}
function urlParser(url) {
var el = document.createElement('a');
el.href = url;
return el;
}
function getTab() {
return new Promise(function(resolve, reject) {
chrome.tabs.getSelected(null, function(tab) {
resolve(tab);
});
});
}
function getRoomFromTab(tab) {
return getTab().then(function(tab) {
var parser = urlParser(tab.url);
return {
owner: parser.pathname.split('/')[1] || null,
name: parser.pathname.split('/')[2] || null,
tabId: tab.id
};
});
}
function getRoomUrl() {
var url = 'https://gitter.im';
return getRoomFromTab()
.then(function(room){
url += room.owner ? '/' + room.owner : '';
url += room.name ? '/' + room.name : '';
return url;
});
}
function roomCheckSuccess(request) {
// TODO: Improve status checks once API provides additional data
//
// request.exists - (Boolean) Does the room exist?
// request.public - (Boolean) Is the room publicly accessible?
// request.joinable - (Boolean) Can the current user join the room?
// false - user has perms || room is public
// false - user doesn't have perms && room is private
// request.makable - (Boolean) Can the current user create the room?
// true - room doesn't exist && user has perms
// false - room already exists || user doesn't have perms
// 200 = room is public and exists (request.exists == true)
// 302 = room is private (request.isPrivate == true)
// 401 = room does not exist (request.exists == false)
if(request.status === 200) {
chrome.pageAction.setIcon({
tabId: request.tabId,
path: 'icon.png'
});
chrome.pageAction.show(request.tabId);
// chrome.pageAction.setPopup({
// tabId: request.tabId,
// popup: 'page_action/popup.html'
// })
}
}
function roomCheckError(error) {
console.error(error);
}
// room (Object)
// room.name - name of the room (e.g. gitter)
// room.owner - User or organization that owns the room (e.g. gitterHQ)
function roomCheck(room) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.addEventListener('load', function(){
debug('API resp for ' + room.owner + room.name + ' (' + req.status + ')');
req.tabId = room.tabId;
resolve(req);
}, false);
req.addEventListener('error', reject, false);
room.name = room.name ? '/' + room.name : '';
var host = 'http://gitter-ext-server.herokuapp.com/room/';
var url = host + room.owner + room.name;
req.open('GET', url, true);
req.setRequestHeader('Content-Type', 'application/json');
debug('API req', room.owner + room.name);
req.send();
});
}
function updatePageAction(tabId, tab) {
var parser = urlParser(tab.url);
if(parser.hostname === 'github.com' || parser.hostname === 'www.github.com') {
// Found a GitHub page! That means there may be a room here ...
getRoomFromTab()
.then(roomCheck)
.then(
roomCheckSuccess,
roomCheckError
);
} else {
debug('FINSH: Dropping', parser.href);
// noop - we don't care about this URL
}
}
// -- Event handlers -----------------------------------------------------------
// The following event handlers normalize contexts in which the page action
// will need to get checked.
function onActivated(activeInfo) {
var tabId = activeInfo.tabId;
chrome.tabs.get(tabId, function(tab) {
debug('START: onActivated', tab.url);
updatePageAction(tabId, tab);
});
}
function onCreated(tab) {
debug('START: onCreated', tab.url);
updatePageAction(tab.id, tab);
}
function onUpdated(tabId, changeInfo, tab) {
if (changeInfo.status === "loading") {
debug('START: onUpdate', tab.url, changeInfo);
updatePageAction(tabId, tab);
} else {
debug('IGNOR: onUpdated', tab.url);
}
}
function actionClicked() {
// TODO: Rather than create a new tab every time, switch to the appropriate tab
getRoomUrl()
.then(function(url) {
chrome.tabs.create({
active: true,
url: url
}, function(){ /* ... */ });
});
}
// -- Register tab event handlers ----------------------------------------------
// Re-check URL whenever the user navigates
chrome.tabs.onUpdated.addListener(onUpdated);
// Whenever the user switches to another tab, check the new tab
chrome.tabs.onActivated.addListener(onActivated);
// Cover cases where a home page is set to a Github page(Overkill?)
chrome.tabs.onCreated.addListener(onCreated);
// -- Register action event handlers -------------------------------------------
chrome.pageAction.onClicked.addListener(actionClicked);