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. 4 5 tabs = {}; 6 tabIds = []; 7 8 focusedWindowId = undefined; 9 currentWindowId = undefined; 10 11 function bootStrap() { 12 chrome.windows.getCurrent(function(currentWindow) { 13 currentWindowId = currentWindow.id; 14 chrome.windows.getLastFocused(function(focusedWindow) { 15 focusedWindowId = focusedWindow.id; 16 loadWindowList(); 17 }); 18 }); 19 } 20 21 function isInt(i) { 22 return (typeof i == "number") && !(i % 1) && !isNaN(i); 23 } 24 25 function loadWindowList() { 26 chrome.windows.getAll({ populate: true }, function(windowList) { 27 tabs = {}; 28 tabIds = []; 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); 32 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]; 36 } 37 } 38 39 var input = new JsExprContext(windowList); 40 var output = document.getElementById('windowList'); 41 jstProcess(input, output); 42 }); 43 } 44 45 function updateTabData(id) { 46 var retval = { 47 url: document.getElementById('url_' + id).value, 48 selected: document.getElementById('selected_' + id).value ? true : false 49 } 50 51 return retval; 52 } 53 54 function updateTab(id){ 55 try { 56 chrome.tabs.update(id, updateTabData(id)); 57 } catch (e) { 58 alert(e); 59 } 60 } 61 62 function moveTabData(id) { 63 return { 64 'index': parseInt(document.getElementById('index_' + id).value), 65 'windowId': parseInt(document.getElementById('windowId_' + id).value) 66 } 67 } 68 function moveTab(id) { 69 try { 70 chrome.tabs.move(id, moveTabData(id)); 71 } catch (e) { 72 alert(e); 73 } 74 } 75 76 function createTabData(id) { 77 return { 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 83 } 84 } 85 86 function createTab() { 87 var args = createTabData('new') 88 89 if (!isInt(args.windowId)) 90 delete args.windowId; 91 if (!isInt(args.index)) 92 delete args.index; 93 94 try { 95 chrome.tabs.create(args); 96 } catch (e) { 97 alert(e); 98 } 99 } 100 101 function updateAll() { 102 try { 103 for (var i = 0; i < tabIds.length; i++) { 104 chrome.tabs.update(tabIds[i], updateTabData(tabIds[i])); 105 } 106 } catch(e) { 107 alert(e); 108 } 109 } 110 111 function moveAll() { 112 appendToLog('moving all'); 113 try { 114 for (var i = 0; i < tabIds.length; i++) { 115 chrome.tabs.move(tabIds[i], moveTabData(tabIds[i])); 116 } 117 } catch(e) { 118 alert(e); 119 } 120 } 121 122 function removeTab(tabId) { 123 try { 124 chrome.tabs.remove(tabId, function() { 125 appendToLog('tab: ' + tabId + ' removed.'); 126 }); 127 } catch (e) { 128 alert(e); 129 } 130 } 131 132 function appendToLog(logLine) { 133 document.getElementById('log') 134 .appendChild(document.createElement('div')) 135 .innerText = "> " + logLine; 136 } 137 138 function clearLog() { 139 document.getElementById('log').innerText = ''; 140 } 141 142 chrome.windows.onCreated.addListener(function(createInfo) { 143 appendToLog('windows.onCreated -- window: ' + createInfo.id); 144 loadWindowList(); 145 }); 146 147 chrome.windows.onFocusChanged.addListener(function(windowId) { 148 focusedWindowId = windowId; 149 appendToLog('windows.onFocusChanged -- window: ' + windowId); 150 loadWindowList(); 151 }); 152 153 chrome.windows.onRemoved.addListener(function(windowId) { 154 appendToLog('windows.onRemoved -- window: ' + windowId); 155 loadWindowList(); 156 }); 157 158 chrome.tabs.onCreated.addListener(function(tab) { 159 appendToLog( 160 'tabs.onCreated -- window: ' + tab.windowId + ' tab: ' + tab.id + 161 ' title: ' + tab.title + ' index ' + tab.index + ' url ' + tab.url); 162 loadWindowList(); 163 }); 164 165 chrome.tabs.onAttached.addListener(function(tabId, props) { 166 appendToLog( 167 'tabs.onAttached -- window: ' + props.newWindowId + ' tab: ' + tabId + 168 ' index ' + props.newPosition); 169 loadWindowList(); 170 }); 171 172 chrome.tabs.onMoved.addListener(function(tabId, props) { 173 appendToLog( 174 'tabs.onMoved -- window: ' + props.windowId + ' tab: ' + tabId + 175 ' from ' + props.fromIndex + ' to ' + props.toIndex); 176 loadWindowList(); 177 }); 178 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); 185 }); 186 } 187 188 chrome.tabs.onUpdated.addListener(function(tabId, props) { 189 appendToLog( 190 'tabs.onUpdated -- tab: ' + tabId + ' status ' + props.status + 191 ' url ' + props.url); 192 refreshTab(tabId); 193 }); 194 195 chrome.tabs.onDetached.addListener(function(tabId, props) { 196 appendToLog( 197 'tabs.onDetached -- window: ' + props.oldWindowId + ' tab: ' + tabId + 198 ' index ' + props.oldPosition); 199 loadWindowList(); 200 }); 201 202 chrome.tabs.onSelectionChanged.addListener(function(tabId, props) { 203 appendToLog( 204 'tabs.onSelectionChanged -- window: ' + props.windowId + ' tab: ' + 205 tabId); 206 loadWindowList(); 207 }); 208 209 chrome.tabs.onRemoved.addListener(function(tabId) { 210 appendToLog('tabs.onRemoved -- tab: ' + tabId); 211 loadWindowList(); 212 }); 213 214 function createWindow() { 215 var args = { 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 221 } 222 223 if (!isInt(args.left)) 224 delete args.left; 225 if (!isInt(args.top)) 226 delete args.top; 227 if (!isInt(args.width)) 228 delete args.width; 229 if (!isInt(args.height)) 230 delete args.height; 231 if (!args.url) 232 delete args.url; 233 234 try { 235 chrome.windows.create(args); 236 } catch(e) { 237 alert(e); 238 } 239 } 240 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); 248 appendToLog( 249 'window refreshed -- windowId: ' + window.id + ' tab count:' + 250 window.tabs.length); 251 }); 252 }); 253 } 254 255 function updateWindowData(id) { 256 var retval = { 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) 261 } 262 if (!isInt(retval.left)) 263 delete retval.left; 264 if (!isInt(retval.top)) 265 delete retval.top; 266 if (!isInt(retval.width)) 267 delete retval.width; 268 if (!isInt(retval.height)) 269 delete retval.height; 270 271 return retval; 272 } 273 274 function updateWindow(id){ 275 try { 276 chrome.windows.update(id, updateWindowData(id)); 277 } catch (e) { 278 alert(e); 279 } 280 } 281 282 function removeWindow(windowId) { 283 try { 284 chrome.windows.remove(windowId, function() { 285 appendToLog('window: ' + windowId + ' removed.'); 286 }); 287 } catch (e) { 288 alert(e); 289 } 290 } 291 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); 297 appendToLog( 298 'selected tab refreshed -- tabId: ' + tabs[0].id + 299 ' url:' + tabs[0].url); 300 }); 301 } 302 303 document.addEventListener('DOMContentLoaded', function() { 304 bootStrap(); 305 });