-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
100 lines (82 loc) · 2.12 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
function Tablist (tabId) {
this.tabId = tabId;
chrome.extension.onMessage.addListener(this.reciveMsg.bind(this));
}
Tablist.prototype = {
/**
* 发送消息
*/
sendMsg : function (type, msg) {
chrome.tabs.sendMessage(this.tabId, {type: type, data: msg });
},
/**
* 接受消息
*/
reciveMsg : function(request, sender, sendResponse) {
switch (request.type) {
case 'highlight':
case 'active':
case 'select':
this.highlightTab(request.data);
break;
case 'create':
case 'new':
case 'plus':
this.createTab(request.data);
break;
case 'remove':
case 'delete':
case 'del':
case 'close':
this.removeTab(request.data);
break;
default:
console.log('Recived unhandle message: '+ JSON.stringify(request) + '; Sender: '+ sender);
}
},
/**
* 显示Tab
*/
highlightTab : function (tab) {
chrome.tabs.highlight({tabs: tab.index});
},
/**
* 移除Tab
*/
removeTab : function (tab) {
chrome.tabs.remove(tab.id);
},
/**
* 创建Tab
*/
createTab: function (options) {
var opt = {};
if (options.url) { opt.url = options.url; }
if (options.index) { opt.index = options.index; }
if (options.active) { opt.active = options.active; }
if (options.openerTabId) { opt.openerTabId = options.openerTabId; }
if (options.selected) { opt.selected = options.selected; }
chrome.tabs.highlight(opt);
},
render: function() {
var _this = this;
chrome.tabs.getAllInWindow(function(tabs) {
_this.sendMsg('list', tabs);
});
}
};
chrome.tabs.onActivated.addListener(function (tabId, winId) {
new Tablist(tabId).render();
});
chrome.tabs.onHighlighted.addListener(function (highlightInfo) {
var ids = highlightInfo.tabIds;
if (ids) {
ids.forEach(function(tabId) {
new Tablist(tabId).render();
});
}
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status !== 'loading') return false;
new Tablist(tabId).render();
});