avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 | // source code is governed by a BSD-style license that can be found in the |
| 3 | // LICENSE file. |
| 4 | |
| 5 | var screenshot = { |
| 6 | tab: 0, |
| 7 | canvas: document.createElement("canvas"), |
| 8 | startX: 0, |
| 9 | startY: 0, |
| 10 | scrollX: 0, |
| 11 | scrollY: 0, |
| 12 | docHeight: 0, |
| 13 | docWidth: 0, |
| 14 | visibleWidth: 0, |
| 15 | visibleHeight: 0, |
| 16 | scrollXCount: 0, |
| 17 | scrollYCount: 0, |
| 18 | scrollBarX: 17, |
| 19 | scrollBarY: 17, |
| 20 | captureStatus: true, |
| 21 | screenshotName: null, |
| 22 | |
| 23 | handleHotKey: function(keyCode) { |
| 24 | if (HotKey.isEnabled()) { |
| 25 | switch (keyCode) { |
| 26 | case HotKey.getCharCode('area'): |
| 27 | screenshot.showSelectionArea(); |
| 28 | break; |
| 29 | case HotKey.getCharCode('viewport'): |
| 30 | screenshot.captureWindow(); |
| 31 | break; |
| 32 | case HotKey.getCharCode('fullpage'): |
| 33 | screenshot.captureWebpage(); |
| 34 | break; |
| 35 | case HotKey.getCharCode('screen'): |
| 36 | screenshot.captureScreen(); |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | }, |
| 41 | |
| 42 | /** |
| 43 | * Receive messages from content_script, and then decide what to do next |
| 44 | */ |
| 45 | addMessageListener: function() { |
| 46 | chrome.extension.onMessage.addListener(function(request, sender, response) { |
| 47 | var obj = request; |
| 48 | var hotKeyEnabled = HotKey.isEnabled(); |
| 49 | switch (obj.msg) { |
| 50 | case 'capture_hot_key': |
| 51 | screenshot.handleHotKey(obj.keyCode); |
| 52 | break; |
| 53 | case 'capture_selected': |
| 54 | screenshot.captureSelected(); |
| 55 | break; |
| 56 | case 'capture_window': |
| 57 | if (hotKeyEnabled) { |
| 58 | screenshot.captureWindow(); |
| 59 | } |
| 60 | break; |
| 61 | case 'capture_area': |
| 62 | if (hotKeyEnabled) { |
| 63 | screenshot.showSelectionArea(); |
| 64 | } |
| 65 | break; |
| 66 | case 'capture_webpage': |
| 67 | if (hotKeyEnabled) { |
| 68 | screenshot.captureWebpage(); |
| 69 | } |
| 70 | break; |
| 71 | } |
| 72 | }); |
| 73 | }, |
| 74 | |
| 75 | /** |
| 76 | * Send the Message to content-script |
| 77 | */ |
| 78 | sendMessage: function(message, callback) { |
| 79 | chrome.tabs.getSelected(null, function(tab) { |
| 80 | chrome.tabs.sendMessage(tab.id, message, callback); |
| 81 | }); |
| 82 | }, |
| 83 | |
| 84 | showSelectionArea: function() { |
| 85 | screenshot.sendMessage({msg: 'show_selection_area'}, null); |
| 86 | }, |
| 87 | |
| 88 | captureWindow: function() { |
| 89 | screenshot.sendMessage({msg: 'capture_window'}, |
| 90 | screenshot.onResponseVisibleSize); |
| 91 | }, |
| 92 | |
| 93 | captureSelected: function() { |
| 94 | screenshot.sendMessage({msg: 'capture_selected'}, |
| 95 | screenshot.onResponseVisibleSize); |
| 96 | }, |
| 97 | |
| 98 | captureWebpage: function() { |
| 99 | screenshot.sendMessage({msg: 'scroll_init'}, |
| 100 | screenshot.onResponseVisibleSize); |
| 101 | }, |
| 102 | |
| 103 | onResponseVisibleSize: function(response) { |
| 104 | switch (response.msg) { |
| 105 | case 'capture_window': |
| 106 | screenshot.captureVisible(response.docWidth, response.docHeight); |
| 107 | break; |
| 108 | case 'scroll_init_done': |
| 109 | screenshot.startX = response.startX, |
| 110 | screenshot.startY = response.startY, |
| 111 | screenshot.scrollX = response.scrollX, |
| 112 | screenshot.scrollY = response.scrollY, |
| 113 | screenshot.canvas.width = response.canvasWidth; |
| 114 | screenshot.canvas.height = response.canvasHeight; |
| 115 | screenshot.visibleHeight = response.visibleHeight, |
| 116 | screenshot.visibleWidth = response.visibleWidth, |
| 117 | screenshot.scrollXCount = response.scrollXCount; |
| 118 | screenshot.scrollYCount = response.scrollYCount; |
| 119 | screenshot.docWidth = response.docWidth; |
| 120 | screenshot.docHeight = response.docHeight; |
| 121 | screenshot.zoom = response.zoom; |
| 122 | setTimeout("screenshot.captureAndScroll()", 100); |
| 123 | break; |
| 124 | case 'scroll_next_done': |
| 125 | screenshot.scrollXCount = response.scrollXCount; |
| 126 | screenshot.scrollYCount = response.scrollYCount; |
| 127 | setTimeout("screenshot.captureAndScroll()", 100); |
| 128 | break; |
| 129 | case 'scroll_finished': |
| 130 | screenshot.captureAndScrollDone(); |
| 131 | break; |
| 132 | } |
| 133 | }, |
| 134 | |
| 135 | captureSpecialPage: function() { |
| 136 | var formatParam = localStorage.screenshootQuality || 'png'; |
| 137 | chrome.tabs.captureVisibleTab( |
| 138 | null, {format: formatParam, quality: 50}, function(data) { |
| 139 | var image = new Image(); |
| 140 | image.onload = function() { |
| 141 | screenshot.canvas.width = image.width; |
| 142 | screenshot.canvas.height = image.height; |
| 143 | var context = screenshot.canvas.getContext("2d"); |
| 144 | context.drawImage(image, 0, 0); |
| 145 | screenshot.postImage(); |
| 146 | }; |
| 147 | image.src = data; |
| 148 | }); |
| 149 | }, |
| 150 | |
| 151 | captureScreenCallback: function(data) { |
| 152 | var image = new Image(); |
| 153 | image.onload = function() { |
| 154 | screenshot.canvas.width = image.width; |
| 155 | screenshot.canvas.height = image.height; |
| 156 | var context = screenshot.canvas.getContext("2d"); |
| 157 | context.drawImage(image, 0, 0); |
| 158 | screenshot.postImage(); |
| 159 | }; |
| 160 | image.src = "data:image/bmp;base64," + data; |
| 161 | }, |
| 162 | |
| 163 | /** |
| 164 | * Use drawImage method to slice parts of a source image and draw them to |
| 165 | * the canvas |
| 166 | */ |
| 167 | capturePortion: function(x, y, width, height, |
| 168 | visibleWidth, visibleHeight, docWidth, docHeight) { |
| 169 | var formatParam = localStorage.screenshootQuality || 'png'; |
| 170 | chrome.tabs.captureVisibleTab( |
| 171 | null, {format: formatParam, quality: 50}, function(data) { |
| 172 | var image = new Image(); |
| 173 | image.onload = function() { |
| 174 | var curHeight = image.width < docWidth ? |
| 175 | image.height - screenshot.scrollBarY : image.height; |
| 176 | var curWidth = image.height < docHeight ? |
| 177 | image.width - screenshot.scrollBarX : image.width; |
| 178 | var zoomX = curWidth / visibleWidth; |
| 179 | var zoomY = curHeight / visibleHeight; |
| 180 | screenshot.canvas.width = width * zoomX; |
| 181 | screenshot.canvas.height = height * zoomY; |
| 182 | var context = screenshot.canvas.getContext("2d"); |
| 183 | context.drawImage(image, x * zoomX, y * zoomY, width * zoomX, |
| 184 | height * zoomY, 0, 0, width * zoomX, height * zoomY); |
| 185 | screenshot.postImage(); |
| 186 | }; |
| 187 | image.src = data; |
| 188 | }); |
| 189 | }, |
| 190 | |
| 191 | captureVisible: function(docWidth, docHeight) { |
| 192 | var formatParam = localStorage.screenshootQuality || 'png'; |
| 193 | chrome.tabs.captureVisibleTab( |
| 194 | null, {format: formatParam, quality: 50}, function(data) { |
| 195 | var image = new Image(); |
| 196 | image.onload = function() { |
| 197 | var width = image.height < docHeight ? |
| 198 | image.width - 17 : image.width; |
| 199 | var height = image.width < docWidth ? |
| 200 | image.height - 17 : image.height; |
| 201 | screenshot.canvas.width = width; |
| 202 | screenshot.canvas.height = height; |
| 203 | var context = screenshot.canvas.getContext("2d"); |
| 204 | context.drawImage(image, 0, 0, width, height, 0, 0, width, height); |
| 205 | screenshot.postImage(); |
| 206 | }; |
| 207 | image.src = data; |
| 208 | }); |
| 209 | }, |
| 210 | |
| 211 | /** |
| 212 | * Use the drawImage method to stitching images, and render to canvas |
| 213 | */ |
| 214 | captureAndScroll: function() { |
| 215 | var formatParam = localStorage.screenshootQuality || 'png'; |
| 216 | chrome.tabs.captureVisibleTab( |
| 217 | null, {format: formatParam, quality: 50}, function(data) { |
| 218 | var image = new Image(); |
| 219 | image.onload = function() { |
| 220 | var context = screenshot.canvas.getContext('2d'); |
| 221 | var width = 0; |
| 222 | var height = 0; |
| 223 | |
| 224 | // Get scroll bar's width. |
| 225 | screenshot.scrollBarY = |
| 226 | screenshot.visibleHeight < screenshot.docHeight ? 17 : 0; |
| 227 | screenshot.scrollBarX = |
| 228 | screenshot.visibleWidth < screenshot.docWidth ? 17 : 0; |
| 229 | |
| 230 | // Get visible width and height of capture result. |
| 231 | var visibleWidth = |
| 232 | (image.width - screenshot.scrollBarY < screenshot.canvas.width ? |
| 233 | image.width - screenshot.scrollBarY : screenshot.canvas.width); |
| 234 | var visibleHeight = |
| 235 | (image.height - screenshot.scrollBarX < screenshot.canvas.height ? |
| 236 | image.height - screenshot.scrollBarX : screenshot.canvas.height); |
| 237 | |
| 238 | // Get region capture start x coordinate. |
| 239 | var zoom = screenshot.zoom; |
| 240 | var x1 = screenshot.startX - Math.round(screenshot.scrollX * zoom); |
| 241 | var x2 = 0; |
| 242 | var y1 = screenshot.startY - Math.round(screenshot.scrollY * zoom); |
| 243 | var y2 = 0; |
| 244 | |
| 245 | if ((screenshot.scrollYCount + 1) * visibleWidth > |
| 246 | screenshot.canvas.width) { |
| 247 | width = screenshot.canvas.width % visibleWidth; |
| 248 | x1 = (screenshot.scrollYCount + 1) * visibleWidth - |
| 249 | screenshot.canvas.width + screenshot.startX - screenshot.scrollX; |
| 250 | } else { |
| 251 | width = visibleWidth; |
| 252 | } |
| 253 | |
| 254 | if ((screenshot.scrollXCount + 1) * visibleHeight > |
| 255 | screenshot.canvas.height) { |
| 256 | height = screenshot.canvas.height % visibleHeight; |
| 257 | if ((screenshot.scrollXCount + 1) * visibleHeight + |
| 258 | screenshot.scrollY < screenshot.docHeight) { |
| 259 | y1 = 0; |
| 260 | } else { |
| 261 | y1 = (screenshot.scrollXCount + 1) * visibleHeight + |
| 262 | screenshot.scrollY - screenshot.docHeight; |
| 263 | } |
| 264 | |
| 265 | } else { |
| 266 | height = visibleHeight; |
| 267 | } |
| 268 | x2 = screenshot.scrollYCount * visibleWidth; |
| 269 | y2 = screenshot.scrollXCount * visibleHeight; |
| 270 | context.drawImage(image, x1, y1, width, height, x2, y2, width, height); |
| 271 | screenshot.sendMessage({msg: 'scroll_next', visibleWidth: visibleWidth, |
| 272 | visibleHeight: visibleHeight}, screenshot.onResponseVisibleSize); |
| 273 | }; |
| 274 | image.src = data; |
| 275 | }); |
| 276 | }, |
| 277 | |
| 278 | captureAndScrollDone: function() { |
| 279 | screenshot.postImage(); |
| 280 | }, |
| 281 | |
| 282 | /** |
| 283 | * Post the image to 'showimage.html' |
| 284 | */ |
| 285 | postImage: function() { |
| 286 | chrome.tabs.getSelected(null, function(tab) { |
| 287 | screenshot.tab = tab; |
| 288 | }); |
| 289 | var date = new Date(); |
| 290 | screenshot.screenshotName = "Screenshot "+dateFormat(date, 'Y-m-d H.i.s'); |
| 291 | chrome.tabs.create({'url': 'showimage.html'}); |
| 292 | var popup = chrome.extension.getViews({type: 'popup'})[0]; |
| 293 | if (popup) |
| 294 | popup.close(); |
| 295 | }, |
| 296 | |
| 297 | isThisPlatform: function(operationSystem) { |
| 298 | return navigator.userAgent.toLowerCase().indexOf(operationSystem) > -1; |
| 299 | }, |
| 300 | |
| 301 | executeScriptsInExistingTabs: function() { |
| 302 | chrome.windows.getAll(null, function(wins) { |
| 303 | for (var j = 0; j < wins.length; ++j) { |
| 304 | chrome.tabs.getAllInWindow(wins[j].id, function(tabs) { |
| 305 | for (var i = 0; i < tabs.length; ++i) { |
| 306 | if (tabs[i].url.indexOf("chrome://") != 0) { |
| 307 | chrome.tabs.executeScript(tabs[i].id, { file: 'js/page.js' }); |
| 308 | chrome.tabs.executeScript(tabs[i].id, { file: 'js/shortcut.js' }); |
| 309 | } |
| 310 | } |
| 311 | }); |
| 312 | } |
| 313 | }); |
| 314 | }, |
| 315 | |
| 316 | init: function() { |
| 317 | localStorage.screenshootQuality = localStorage.screenshootQuality || 'png'; |
| 318 | screenshot.executeScriptsInExistingTabs(); |
| 319 | screenshot.addMessageListener(); |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | screenshot.init(); |