popup.js revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Show a list of all tabs in the same process as this one.
6function init() {
7  chrome.windows.getCurrent(function(currentWindow) {
8    chrome.tabs.getSelected(currentWindow.id, function(selectedTab) {
9      chrome.experimental.processes.getProcessIdForTab(selectedTab.id,
10        function(pid) {
11          var outputDiv = document.getElementById("tab-list");
12          var titleDiv = document.getElementById("title");
13          titleDiv.innerHTML = "<b>Tabs in Process " + pid + ":</b>";
14          displayTabInfo(currentWindow.id, selectedTab, outputDiv);
15          displaySameProcessTabs(selectedTab, pid, outputDiv);
16        }
17      );
18
19    });
20  });
21}
22
23function displaySameProcessTabs(selectedTab, processId, outputDiv) {
24  // Loop over all windows and their tabs
25  var tabs = [];
26  chrome.windows.getAll({ populate: true }, function(windowList) {
27    for (var i = 0; i < windowList.length; i++) {
28      for (var j = 0; j < windowList[i].tabs.length; j++) {
29        var tab = windowList[i].tabs[j];
30        if (tab.id != selectedTab.id) {
31          tabs.push(tab);
32        }
33      }
34    }
35
36    // Display tab in list if it is in the same process
37    tabs.forEach(function(tab) {
38      chrome.experimental.processes.getProcessIdForTab(tab.id,
39        function(pid) {
40          if (pid == processId) {
41            displayTabInfo(tab.windowId, tab, outputDiv);
42          }
43        }
44      );
45    });
46  });
47}
48
49// Print a link to a given tab
50function displayTabInfo(windowId, tab, outputDiv) {
51  if (tab.favIconUrl != undefined) {
52    outputDiv.innerHTML += "<img src='chrome://favicon/" + tab.url + "'>\n";
53  }
54  outputDiv.innerHTML +=
55    "<b><a href='#' onclick='showTab(window, " + windowId + ", " + tab.id +
56    ")'>" + tab.title + "</a></b><br>\n" +
57    "<i>" + tab.url + "</i><br>\n";
58}
59
60// Bring the selected tab to the front
61function showTab(origWindow, windowId, tabId) {
62  // TODO: Bring the window to the front.  (See http://crbug.com/31434)
63  //chrome.windows.update(windowId, {focused: true});
64  chrome.tabs.update(tabId, { selected: true });
65  origWindow.close();
66}
67
68// Kick things off.
69document.addEventListener('DOMContentLoaded', init);
70