-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
145 lines (129 loc) · 4.16 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
config.defaults({
altIsMeta: true,
debugEnabled: false,
bindingFiles: ['bindings/base.js', 'bindings/emacs.js'],
urlEnabled: false,
bindingUrl: 'http://localhost:2562/bindings.js',
bindingText: '',
blacklist: [],
username: '',
password: ''
});
function xhr(req, callback) {
var xhr = new XMLHttpRequest();
xhr.open(req.method, req.url, true);
for (var h in req.headers)
xhr.setRequestHeader(h, req.headers[h]);
if (req.authenticate) {
var creds = config.get('username') + ':' + config.get('password');
var auth = 'Basic ' + Base64.encode(creds);
xhr.setRequestHeader('Authorization', auth);
}
xhr.onreadystatechange = function() {
if (this.readyState === 4)
callback.apply(this);
};
if (typeof req.data === 'string') {
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.send(req.data);
} else if (typeof req.data === 'object') {
var pairs = [];
for (var k in req.data) {
pairs.push(k + '=' + encodeURIComponent(req.data[k]));
}
xhr.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
xhr.send(pairs.join('&'));
} else {
xhr.send(null);
}
}
// Binding-script cache. XHR is async, of course, so we can't depend
// on the list being filled in any particular order. Therefore we give
// each file a numeric priority corresponding to where they are on the
// options page (textarea is last, so it always overrides others).
//
// FIXME: There is no way for a content script to tell that the list
// is in fact populated. This is going to fail on a cold start with a
// bunch of tabs. Maybe.
var bindingFiles;
function fetchBindingFiles() {
bindingFiles = [{
name: 'options.html',
text: config.get('bindingText'),
priority: Number.MAX_VALUE
}];
var fileNames = config.get('bindingFiles');
if (config.get('urlEnabled'))
fileNames.push(config.get('bindingUrl'));
fileNames.forEach(function(f, i) {
xhr({method: 'GET', url: f}, function() {
bindingFiles.push({
name: f,
text: this.responseText,
priority: i
});
});
});
}
fetchBindingFiles();
// Every request that needs to implicitly hold on to a callback will
// use the "simple" interface. The request should always have a 'type'
// property set so we can decide what to do with it.
chrome.extension.onRequest.addListener(function(msg, src, send) {
switch (msg.type) {
case 'xhr':
xhr(msg.req, function() { send(this); });
break;
case 'tabs.getAllInWindow':
chrome.tabs.getAllInWindow(msg.id, send);
break;
case 'tabs.getSelected':
chrome.tabs.getSelected(msg.id, send);
break;
case 'tabs.create':
chrome.tabs.create(msg.info, send);
break;
case 'tabs.update':
chrome.tabs.update(msg.id, msg.info, send);
break;
case 'tabs.move':
chrome.tabs.move(msg.id, msg.info, send);
break;
case 'tabs.remove':
chrome.tabs.remove(msg.id, msg.info, send);
break;
}
});
// Things that are purely message-based will use ports.
chrome.extension.onConnect.addListener(function(port) {
switch (port.name) {
case 'getConfig':
port.onMessage.addListener(function(req) {
port.postMessage({
debugEnabled: config.get('debugEnabled'),
altIsMeta: config.get('altIsMeta'),
bindingEnabled: !isBlacklisted(req)
});
});
break;
case 'getBindings':
port.onMessage.addListener(function(req) {
port.postMessage({
bindingFiles: bindingFiles
});
});
break;
case 'reloadBindings':
// TODO: Should send updated bindings to all tabs on change.
port.onMessage.addListener(function(req) {
fetchBindingFiles();
});
break;
}
});
function isBlacklisted(url) {
return config.get('blacklist').some(function(pat) {
return RegExp(pat).test(url);
});
}