-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
66 lines (62 loc) · 1.88 KB
/
background.js
File metadata and controls
66 lines (62 loc) · 1.88 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
57
58
59
60
61
62
63
64
65
66
chrome.commands.onCommand.addListener(function(command) {
console.log('Command:', command);
init();
});
function init() {
// get active tab
chrome.tabs.query({active: true}, function(tabs) {
// inject css and js to page
chrome.tabs.insertCSS(tabs[0].id, {file: 'styles.css'})
chrome.tabs.executeScript(tabs[0].id, {file: 'window.js'});
chrome.tabs.executeScript(tabs[0].id, {file: 'fuzzy.js'});
});
};
// recent tabs
var recentTabIds = [];
function clear(val) {
var index = recentTabIds.indexOf(val);
if (index > -1) recentTabIds.splice(index, 1);
}
// tabs observers
chrome.tabs.onActivated.addListener(function (info) {
clear(info.tabId);
recentTabIds.push(info.tabId);
});
chrome.tabs.onRemoved.addListener(function (removedTabId) {
clear(removedTabId);
});
chrome.tabs.onReplaced.addListener(function (addedTabId, removedTabId) {
clear(removedTabId);
clear(addedTabId);
recentTabIds.push(addedTabId);
});
function getRecentTabs(callback) {
var n = recentTabIds.length
var j = 0, tabs = [];
if (n < 2) return callback([]);
for(var i = n - 2; i >= 0; i--) {
chrome.tabs.get(recentTabIds[i], function(tab) {
if (tab) tabs.push(tab);
if (j === (n - 2)) return callback(tabs);
j++;
});
};
}
// message comunication
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.action === 'activate_tab') {
chrome.tabs.update(msg.data.tab.id, {selected: true});
}
if (msg.action === 'get_all_tabs') {
chrome.tabs.query({}, function(tabs) {
msg.data = tabs;
chrome.tabs.sendMessage(sender.tab.id, msg);
});
}
if (msg.action === 'get_recent_tabs') {
getRecentTabs(function(tabs) {
msg.data = tabs;
chrome.tabs.sendMessage(sender.tab.id, msg);
});
}
});