forked from i-a-n/tabnarok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
291 lines (238 loc) · 6.57 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* openTabs
* @description the original tab stats, forked from i-a-n's tabnarok
* @author https://github.com/i-a-n/tabnarok
*/
var rightNow = new Date().getTime() / 1000;
localStorage.openTabs = JSON.stringify({ 'loadedOn': rightNow });
chrome.tabs.onCreated.addListener(function(tab) {
var retrievedObject = JSON.parse( localStorage.getItem('openTabs') );
retrievedObject[tab.id] = { "createdOn" : new Date().getTime() / 1000 };
localStorage.setItem('openTabs', JSON.stringify(retrievedObject));
});
/**
* tab extracting
* @description extract all tabs from a particular url into their own window
*/
var shuffle = false;
var moveDelay = 10; // milliseconds
// trimPrefix trims the given prefix from the given string.
function trimPrefix(s, prefix) {
if (s.startsWith(prefix)) {
return s.slice(prefix.length);
}
return s;
}
// lexHost returns a string for sorting hostnames lexicographically.
// The TLD is stripped and the host pieces are reversed.
function lexHost(url) {
var u = new URL(url);
var parts = u.host.split('.');
parts.reverse();
if (parts.length > 1) {
parts = parts.slice(1);
}
return parts.join('.');
}
// lexScheme returns a string for sorting schemes lexicographically.
function lexScheme(url) {
var u = new URL(url);
switch (u.protocol) {
// Sort HTTP together
case 'http:':
case 'https:':
return 'http:';
// Sort local things late
case 'chrome:':
case 'file:':
return '~' + u.protocol;
}
return u.protocol;
}
// lexTab returns a string for sorting tabs lexicographically.
function lexTab(tab) {
var pieces = [];
if (shuffle) {
pieces.push(Math.random());
}
// Pinning
if (tab.pinned) {
pieces.push('pin:0(yes):' + tab.index);
} else {
pieces.push('pin:1(no)');
}
// Scheme
pieces.push(lexScheme(tab.url));
// Host
pieces.push(lexHost(tab.url));
// Title
pieces.push(tab.title.toLowerCase());
return pieces.join(' ! ');
}
// extractDomain extracts all tabs with the active tab's domain into
// a new window and then sorts them.
function extractDomain() {
chrome.tabs.getSelected(function(tab) {
var target = lexHost(tab.url);
// Create a window with the tab in it.
var cq = {
'tabId': tab.id,
'focused': true,
};
chrome.windows.create(cq, function(win) {
// Move all other windows with the same target into the window.
var tq = {
'windowType': 'normal',
};
chrome.tabs.query(tq, function(tabs) {
for (const tab of tabs) {
var host = lexHost(tab.url);
if (host != target) {
continue;
}
chrome.tabs.move(tab.id, {
'windowId': win.id,
'index': -1,
});
}
// Sort the tabs in the new window.
var wq = {
'windowTypes': ['normal'],
'populate': true,
};
window.setTimeout(
chrome.windows.get, moveDelay, win.id, wq, sortWindow);
});
});
});
}
function logWindow(windowId) {
var q = {
'windowId': windowId,
};
chrome.tabs.query(q, function(tabs) {
console.log('Tabs (after reposition):');
tabs.forEach(function(tab, i) {
console.log(i, lexTab(tab));
});
});
}
function moveNextTab(win, tabs, i, inserted) {
if (i >= tabs.length) {
console.log('Finished sorting window', win.id, '; tabs are now:');
logWindow(win.id);
return;
}
var tab = tabs[i];
if (tab.pinned) {
console.log('Pinned', tab.id, 'at', tab.index, 'to', i, lexTab(tab));
window.setTimeout(moveNextTab, 0, win, tabs, i + 1, inserted);
return;
}
if (i == tab.index + inserted) {
// Nothing to do
console.log(
'No action for', tab.id, 'at', tab.index, '+', inserted, lexTab(tab));
window.setTimeout(moveNextTab, 0, win, tabs, i + 1, inserted);
return;
}
console.log('Moving', tab.id, 'from', tab.index, 'to', i, lexTab(tab));
chrome.tabs.move(tab.id, {'index': i}, function() {
window.setTimeout(moveNextTab, moveDelay, win, tabs, i + 1, inserted + 1);
});
}
function extractMode(mode) {
extractDomain();
}
/**
* tab merging
* @description merge all tabs from all windows into a single window
*/
var targetWindow = null;
var tabCount = 0;
function start(tab) {
chrome.windows.getCurrent(getWindows);
}
function getWindows(win) {
targetWindow = win;
chrome.tabs.getAllInWindow(targetWindow.id, getTabs);
}
function getTabs(tabs) {
tabCount = tabs.length;
// We require all the tab information to be populated.
chrome.windows.getAll({"populate" : true}, moveTabs);
}
function moveTabs(windows) {
var [targetWindow] = windows.filter(window => window.focused === true);
var numWindows = windows.length;
var tabPosition = tabCount;
for (var i = 0; i < numWindows; i++) {
var win = windows[i];
if (targetWindow.id != win.id) {
var numTabs = win.tabs.length;
for (var j = 0; j < numTabs; j++) {
var tab = win.tabs[j];
// Move the tab into the window that triggered the browser action.
chrome.tabs.move(tab.id,
{"windowId": targetWindow.id, "index": tabPosition});
tabPosition++;
// fix pinned tabs
if(tab.pinned==true){chrome.tabs.update(tab.id, {"pinned":true});}
}
}
}
}
// Set up a click handler so that we can merge all the windows.
chrome.browserAction.onClicked.addListener(start);
function mergeMode(mode) {
start();
}
/**
* tab sorting
* @description group tabs by url, and sort alphabetically
*/
function sortWindow(win) {
// Sort tabs within the window
var tabs = win.tabs;
tabs.forEach(function(tab, i) {
console.log(i, lexTab(tab));
});
tabs.sort(function(a, b) {
return lexTab(a).localeCompare(lexTab(b));
});
window.setTimeout(moveNextTab, 0, win, tabs, 0, 0);
}
function sortMode(mode) {
var q = {
'windowTypes': ['normal'],
'populate': true,
};
chrome.windows.getAll(q, function(windows) {
windows.forEach(sortWindow);
});
}
/**
* handleMessage
* @description handles an incoming message from the app, and performs the appropriate action
*/
function handleMessage(message, sender, respond) {
var action = message.action;
var args = message.args;
console.log(action, '(', args, ')');
switch (action) {
case 'extract':
extractMode.apply(document, args);
break;
case 'merge':
mergeMode.apply(document, args);
break;
case 'sort':
sortMode.apply(document, args);
break;
default:
console.log('Unhandled message:', message);
break;
}
respond({});
}
chrome.runtime.onMessage.addListener(handleMessage);