-
Notifications
You must be signed in to change notification settings - Fork 21
/
background.js
57 lines (48 loc) · 1.27 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
localStorage.migrate ();
var ports = {};
function refreshTabs () {
var keys = toArray (ports);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
var port = ports[key];
if (debug) console.log ("Loading " + port.passhashUrl + " for " + key);
port.postMessage ({ update: loadConfig (port.passhashUrl) });
}
}
function loadOptions () {
return localStorage.loadOptions ();
}
function saveOptions (options) {
localStorage.saveOptions (options);
refreshTabs ();
}
function loadConfig (url) {
return localStorage.loadConfig (url);
}
function loadTags () {
return localStorage.loadTags ();
}
function saveConfig (url, config) {
localStorage.saveConfig (url, config);
refreshTabs ();
}
chrome.extension.onConnect.addListener (function (port) {
console.assert (port.name == "passhash");
port.onMessage.addListener (function (msg) {
if (null != msg.init) {
var url = grepUrl (msg.url);
var config = localStorage.loadConfig (url);
port.passhashUrl = url;
ports[port.portId_] = port;
port.postMessage ({ init: true, update: config });
} else if (null != msg.save) {
var url = grepUrl (msg.url);
saveConfig (url, msg.save);
}
});
port.onDisconnect.addListener (function (port) {
if (null != port.portId_) {
delete ports[port.portId_];
}
});
});