forked from clvrobj/Emacsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
73 lines (66 loc) · 2.74 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
var selectSiblingTab = function (tab, step) {
if (tab.index == 0 && step < 0) {
chrome.tabs.query({windowId:tab.windowId},
function (tabs) {
if (tabs.length) {
chrome.tabs.update(
tabs.pop().id, {active:true});
}
});
return;
}
chrome.tabs.query({index:tab.index + step, windowId:tab.windowId},
function (tabs) {
if (tabs.length) {
chrome.tabs.update(tabs[0].id, {active:true});
} else {
chrome.tabs.query({index:0, windowId:tab.windowId},
function (tabs) {
if (tabs.length) {
chrome.tabs.update(tabs[0].id, {active:true});
}
});
}
});
},
getAllTabs = function (request, sender, sendResponse) {
chrome.tabs.query({"windowId":sender.tab.windowId},
function (tabs) {
if (tabs.length) {
sendResponse && sendResponse({tabs:tabs, currentTab:sender.tab});
}
});
},
activeTab = function (request, sender, sendResponse) {
chrome.tabs.query({windowId:sender.tab.windowId, index:request.index},
function (tabs) {
if (tabs.length) {
chrome.tabs.update(tabs[0].id, {active:true});
}
});
},
reloadTab = function (request, sender, sendResponse) {
chrome.tabs.reload();
},
closeCurrentTab = function (request, sender, sendResponse) {
chrome.tabs.remove([sender.tab.id], function () {});
},
getLocalstorage = function (request, sender, sendResponse) {
sendResponse({data:localStorage[request.key]});
}
actions = {
'next-tab': function (request, sender) { selectSiblingTab(sender.tab, 1);},
'previous-tab': function (request, sender) { selectSiblingTab(sender.tab, -1);},
'get-all-tabs': getAllTabs,
'active-tab': activeTab,
'reload-tab':reloadTab,
'close-current-tab':closeCurrentTab,
'get-localstorage':getLocalstorage
};
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
if (actions.hasOwnProperty(request.method)) {
actions[request.method](request, sender, sendResponse);
}
return true;
});