-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbar_button.js
67 lines (61 loc) · 2.63 KB
/
toolbar_button.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
// This file is part of RECAP for Chrome.
// Copyright 2013 Ka-Ping Yee <[email protected]>
//
// RECAP for Chrome is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version. RECAP for Chrome is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// RECAP for Chrome. If not, see: http://www.gnu.org/licenses/
// -------------------------------------------------------------------------
// Toolbar button for RECAP (or "browser action" in Chrome parlance).
// Public impure functions. (See utils.js for details on defining services.)
function ToolbarButton() {
var pacerLogin = false; // true if the user is logged in to PACER
var notifier = new Notifier();
// Updates the toolbar button for a tab to reflect the tab's login status.
var updateToolbarButton = function (tab) {
var court = PACER.getCourtFromUrl(tab.url);
var setTitleIcon = function (title, icon) {
chrome.browserAction.setTitle({title: 'RECAP: ' + title, tabId: tab.id});
chrome.browserAction.setIcon({path: icon, tabId: tab.id});
};
if (!court) {
setTitleIcon('Not at a PACER site', 'grey-32.png');
} else if (PACER.isAppellateCourt(court)) {
setTitleIcon('Appellate courts are not supported', 'warning-32.png');
} else if (pacerLogin) {
setTitleIcon('Logged in to PACER', 'icon-32.png');
} else {
setTitleIcon('Not logged in to PACER', 'grey-32.png');
}
};
// Watches all the tabs so we can update their toolbar buttons on navigation.
if (chrome.tabs) {
chrome.tabs.onUpdated.addListener(function (tabId, details, tab) {
updateToolbarButton(tab);
});
}
return {
// Updates our knowledge of the login status based on the current cookies.
updateCookieStatus: function (court, cookies, cb) {
if (court) {
var newPacerLogin = !!PACER.hasPacerCookie(cookies);
if (newPacerLogin != pacerLogin) {
notifier.showStatus(newPacerLogin,
newPacerLogin ? 'Logged in to PACER' : 'Logged out of PACER', null);
pacerLogin = newPacerLogin;
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
updateToolbarButton(tabs[i]);
}
});
}
}
}
};
}