forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
45 lines (36 loc) · 1.17 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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
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 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++;
}
}
}
}
// Set up a click handler so that we can merge all the windows.
chrome.browserAction.onClicked.addListener(start);