-
Notifications
You must be signed in to change notification settings - Fork 6
/
vim-background.js
152 lines (130 loc) · 3.62 KB
/
vim-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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch (request.type) {
case "switch_tab_left":
switchTab(-1);
break;
case "switch_tab_right":
switchTab(1);
break;
case "reload":
chrome.tabs.reload({ bypassCache: request.bypassCache });
break;
case "open_new_tab":
chrome.tabs.create({});
break;
case "close_tab":
closeTab(request.focusLeft);
break;
case "close_system_tabs":
closeSystemTabs();
break;
case "zoom_in":
chrome.tabs.getZoom(function(curr_zoom){
var target_zoom = Math.min(curr_zoom * 1.08, 3.0);
chrome.tabs.setZoom(target_zoom);
});
break;
case "zoom_out":
chrome.tabs.getZoom(function(curr_zoom){
var target_zoom = Math.max(curr_zoom * 0.92, 0.3);
chrome.tabs.setZoom(target_zoom);
});
break;
case "undo_close_tab":
chrome.sessions.getRecentlyClosed({ maxResults: 1 }, function(recentSessions){
if (!recentSessions.length) {
return;
}
let prevSession = recentSessions[0];
if (prevSession.tab) {
chrome.sessions.restore(prevSession.tab.sessionId);
}
});
break;
}
}
);
function queryTabAll(options) {
return new Promise(function(resolve) {
chrome.tabs.query(options, resolve);
});
}
function queryTab(options) {
return queryTabAll(options).then(function (tabs) {
return tabs[0];
});
}
function getActiveTab() {
return queryTab({ active: true, currentWindow: true });
}
function getTabByIndex(index) {
return queryTab({ index });
}
function activateTab(tab) {
return new Promise(function(resolve) {
chrome.tabs.update(tab.id, {active: true}, resolve);
});
}
function runPromiseGenerator(generator){
var gen = generator();
function go(result){
if(result.done) return;
result.value.then(function(r){
go(gen.next(r));
});
}
go(gen.next());
}
function switchTab(offset) {
runPromiseGenerator(function *() {
// first, get currently active tab
let activeTab = yield getActiveTab(),
currentIndex = activeTab.index;
// next, get number of tabs in the window, in order to allow cyclic next
let tabs = yield queryTabAll({ currentWindow: true }),
numTabs = tabs.length;
// finally, get the index of the tab to activate and activate it
let tabToActivate = yield getTabByIndex((currentIndex+offset+numTabs) % numTabs);
yield activateTab(tabToActivate);
});
}
function closeTab(focusLeft) {
runPromiseGenerator(function *() {
let tab = yield getActiveTab();
if (focusLeft && tab.index > 0) {
let tabLeft = yield getTabByIndex(tab.index - 1);
yield activateTab(tabLeft);
}
chrome.tabs.remove(tab.id);
});
}
function closeSystemTabs() {
// Pages that doesn't allow script inject, thus will block QuantumVim
var urls = {
'about:blank': null,
'about:newtab': null,
'about:debugging': null,
'about:addons': null,
'about:preferences': null,
'about:preferences#general': null,
'about:preferences#search': null,
'about:preferences#privacy': null,
'about:preferences#sync': null,
}
//chrome.tabs.query({"url": urls}, function(tabs){
chrome.tabs.query({}, function(tabs){
for (var tab of tabs){
if (tab.url in urls) {
chrome.tabs.remove(tab.id)
}
}
});
}
chrome.commands.onCommand.addListener(function(command) {
switch (command) {
case "close_system_tabs":
closeSystemTabs();
break;
}
});