forked from niutech/chrome-devtools-sidebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
31 lines (30 loc) · 1.05 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
var ports = [];
chrome.runtime.onConnect.addListener(function(port) {
if (port.name !== "bookmarks" && port.name !== "history")
return;
ports.push(port);
var extensionListener = function(message, sender, sendResponse) {
if (message.name === "getBookmarks") {
chrome.bookmarks.getTree(function(tree) {
ports.forEach(function(port) {
if(port.name === "bookmarks")
port.postMessage(tree);
});
});
} else if (message.name === "getHistory") {
chrome.history.search({text: '', /*startTime: (new Date()).getTime() - 604800000, maxResults: 1000*/}, function(tree) {
ports.forEach(function(port) {
if(port.name === "history")
port.postMessage(tree);
});
});
}
};
port.onMessage.addListener(extensionListener);
port.onDisconnect.addListener(function(port) {
var i = ports.indexOf(port);
if(i !== -1)
ports.splice(i, 1);
port.onMessage.removeListener(extensionListener);
});
});