-
Notifications
You must be signed in to change notification settings - Fork 0
/
bg.js
47 lines (44 loc) · 1.26 KB
/
bg.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
trackPageView('/bg.html');
function toCode(f) {
return "(" + f.toString() + ")();";
}
function installCloseHandler() {
var rightPressed = false;
document.addEventListener('keydown', function (event) {
// Only close tab if user didn't scroll to the right.
if (event.keyCode == 39) {
rightPressed = true;
} else if (!rightPressed || event.altKey) {
if (event.keyCode == 37
&& (typeof(keysOn) === 'undefined' || keysOn === true)) {
chrome.extension.sendRequest({type: "track",
data: ['_trackEvent', 'usage',
'item', 'close-tab']});
self.close();
}
}
});
}
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.type == "openLink") {
chrome.tabs.create({
"url":request.data.url,
"selected":request.data.focus,
"index":sender.tab.index
}, function (tab) {
if (request.data.close == undefined || request.data.close) {
chrome.tabs.executeScript(tab.id, {
code: toCode(installCloseHandler)
});
}
});
} else if (request.type == "track") {
_gaq.push(request.data);
} else if (request.type == "get") {
var url = chrome.extension.getURL(request.data.resource);
$.get(url, function(data) {
sendResponse(data);
});
}
});