-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
56 lines (49 loc) · 1.5 KB
/
background.js
File metadata and controls
56 lines (49 loc) · 1.5 KB
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
/* Keep track of the active tab in each window */
var activeTabs = {};
chrome.tabs.onActivated.addListener(function(details) {
activeTabs[details.windowId] = details.tabId;
});
/* Clear the corresponding entry, whenever a window is closed */
chrome.windows.onRemoved.addListener(function(winId) {
delete activeTabs[winId];
});
/* Listen for web-requests and filter them */
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (details.tabId == -1) {
console.log("Skipping request from non-tabbed context...");
return;
}
var notInteresting = Object.keys(activeTabs).every(function(key) {
if (activeTabs[key] == details.tabId) {
/* We are interested in this request */
console.log("Check this out:", details);
return false;
} else {
return true;
}
});
if (notInteresting) {
/* We are not interested in this request */
console.log("Just ignore this one:", details);
}
},
{ urls: ["<all_urls>"] }
);
/* Get the active tabs in all currently open windows */
chrome.tabs.query({ active: true }, function(tabs) {
tabs.forEach(function(tab) {
activeTabs[tab.windowId] = tab.id;
});
console.log("activeTabs = ", activeTabs);
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (!request.state.type && !request.state.devtoolsEnabled) {
chrome.runtime.sendMessage({
msg: "state_changed",
data: {
state: request.state
}
});
}
});