1 // Copyright (c) 2012 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.
8 focusedWindowId = undefined;
9 currentWindowId = undefined;
11 function bootStrap() {
12 chrome.windows.getCurrent(function(currentWindow) {
13 currentWindowId = currentWindow.id;
14 chrome.windows.getLastFocused(function(focusedWindow) {
15 focusedWindowId = focusedWindow.id;
22 return (typeof i == "number") && !(i % 1) && !isNaN(i);
25 function loadWindowList() {
26 chrome.windows.getAll({ populate: true }, function(windowList) {
29 for (var i = 0; i < windowList.length; i++) {
30 windowList[i].current = (windowList[i].id == currentWindowId);
31 windowList[i].focused = (windowList[i].id == focusedWindowId);
33 for (var j = 0; j < windowList[i].tabs.length; j++) {
34 tabIds[tabIds.length] = windowList[i].tabs[j].id;
35 tabs[windowList[i].tabs[j].id] = windowList[i].tabs[j];
39 var input = new JsExprContext(windowList);
40 var output = document.getElementById('windowList');
41 jstProcess(input, output);
45 function updateTabData(id) {
47 url: document.getElementById('url_' + id).value,
48 selected: document.getElementById('selected_' + id).value ? true : false
54 function updateTab(id){
56 chrome.tabs.update(id, updateTabData(id));
62 function moveTabData(id) {
64 'index': parseInt(document.getElementById('index_' + id).value),
65 'windowId': parseInt(document.getElementById('windowId_' + id).value)
68 function moveTab(id) {
70 chrome.tabs.move(id, moveTabData(id));
76 function createTabData(id) {
78 'index': parseInt(document.getElementById('index_' + id).value),
79 'windowId': parseInt(document.getElementById('windowId_' + id).value),
80 'index': parseInt(document.getElementById('index_' + id).value),
81 'url': document.getElementById('url_' + id).value,
82 'selected': document.getElementById('selected_' + id).value ? true : false
86 function createTab() {
87 var args = createTabData('new')
89 if (!isInt(args.windowId))
91 if (!isInt(args.index))
95 chrome.tabs.create(args);
101 function updateAll() {
103 for (var i = 0; i < tabIds.length; i++) {
104 chrome.tabs.update(tabIds[i], updateTabData(tabIds[i]));
112 appendToLog('moving all');
114 for (var i = 0; i < tabIds.length; i++) {
115 chrome.tabs.move(tabIds[i], moveTabData(tabIds[i]));
122 function removeTab(tabId) {
124 chrome.tabs.remove(tabId, function() {
125 appendToLog('tab: ' + tabId + ' removed.');
132 function appendToLog(logLine) {
133 document.getElementById('log')
134 .appendChild(document.createElement('div'))
135 .innerText = "> " + logLine;
138 function clearLog() {
139 document.getElementById('log').innerText = '';
142 chrome.windows.onCreated.addListener(function(createInfo) {
143 appendToLog('windows.onCreated -- window: ' + createInfo.id);
147 chrome.windows.onFocusChanged.addListener(function(windowId) {
148 focusedWindowId = windowId;
149 appendToLog('windows.onFocusChanged -- window: ' + windowId);
153 chrome.windows.onRemoved.addListener(function(windowId) {
154 appendToLog('windows.onRemoved -- window: ' + windowId);
158 chrome.tabs.onCreated.addListener(function(tab) {
160 'tabs.onCreated -- window: ' + tab.windowId + ' tab: ' + tab.id +
161 ' title: ' + tab.title + ' index ' + tab.index + ' url ' + tab.url);
165 chrome.tabs.onAttached.addListener(function(tabId, props) {
167 'tabs.onAttached -- window: ' + props.newWindowId + ' tab: ' + tabId +
168 ' index ' + props.newPosition);
172 chrome.tabs.onMoved.addListener(function(tabId, props) {
174 'tabs.onMoved -- window: ' + props.windowId + ' tab: ' + tabId +
175 ' from ' + props.fromIndex + ' to ' + props.toIndex);
179 function refreshTab(tabId) {
180 chrome.tabs.get(tabId, function(tab) {
181 var input = new JsExprContext(tab);
182 var output = document.getElementById('tab_' + tab.id);
183 jstProcess(input, output);
184 appendToLog('tab refreshed -- tabId: ' + tab.id + ' url: ' + tab.url);
188 chrome.tabs.onUpdated.addListener(function(tabId, props) {
190 'tabs.onUpdated -- tab: ' + tabId + ' status ' + props.status +
191 ' url ' + props.url);
195 chrome.tabs.onDetached.addListener(function(tabId, props) {
197 'tabs.onDetached -- window: ' + props.oldWindowId + ' tab: ' + tabId +
198 ' index ' + props.oldPosition);
202 chrome.tabs.onSelectionChanged.addListener(function(tabId, props) {
204 'tabs.onSelectionChanged -- window: ' + props.windowId + ' tab: ' +
209 chrome.tabs.onRemoved.addListener(function(tabId) {
210 appendToLog('tabs.onRemoved -- tab: ' + tabId);
214 function createWindow() {
216 'left': parseInt(document.getElementById('new_window_left').value),
217 'top': parseInt(document.getElementById('new_window_top').value),
218 'width': parseInt(document.getElementById('new_window_width').value),
219 'height': parseInt(document.getElementById('new_window_height').value),
220 'url': document.getElementById('new_window_url').value
223 if (!isInt(args.left))
225 if (!isInt(args.top))
227 if (!isInt(args.width))
229 if (!isInt(args.height))
235 chrome.windows.create(args);
241 function refreshWindow(windowId) {
242 chrome.windows.get(windowId, function(window) {
243 chrome.tabs.getAllInWindow(window.id, function(tabList) {
244 window.tabs = tabList;
245 var input = new JsExprContext(window);
246 var output = document.getElementById('window_' + window.id);
247 jstProcess(input, output);
249 'window refreshed -- windowId: ' + window.id + ' tab count:' +
255 function updateWindowData(id) {
257 left: parseInt(document.getElementById('left_' + id).value),
258 top: parseInt(document.getElementById('top_' + id).value),
259 width: parseInt(document.getElementById('width_' + id).value),
260 height: parseInt(document.getElementById('height_' + id).value)
262 if (!isInt(retval.left))
264 if (!isInt(retval.top))
266 if (!isInt(retval.width))
268 if (!isInt(retval.height))
269 delete retval.height;
274 function updateWindow(id){
276 chrome.windows.update(id, updateWindowData(id));
282 function removeWindow(windowId) {
284 chrome.windows.remove(windowId, function() {
285 appendToLog('window: ' + windowId + ' removed.');
292 function refreshSelectedTab(windowId) {
293 chrome.tabs.query({active: true, currentWindow: true} function(tabs) {
294 var input = new JsExprContext(tabs[0]);
295 var output = document.getElementById('tab_' + tabs[0].id);
296 jstProcess(input, output);
298 'selected tab refreshed -- tabId: ' + tabs[0].id +
299 ' url:' + tabs[0].url);
303 document.addEventListener('DOMContentLoaded', function() {