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 | |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 23 | /** |
| 24 | * Receive messages from content_script, and then decide what to do next |
| 25 | */ |
| 26 | addMessageListener: function() { |
| 27 | chrome.extension.onMessage.addListener(function(request, sender, response) { |
avm99963 | 873bf6f | 2021-02-07 00:01:31 +0100 | [diff] [blame] | 28 | console.log(request); |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 29 | var obj = request; |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 30 | switch (obj.msg) { |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 31 | case 'capture_selected': |
| 32 | screenshot.captureSelected(); |
| 33 | break; |
| 34 | case 'capture_window': |
avm99963 | 873bf6f | 2021-02-07 00:01:31 +0100 | [diff] [blame] | 35 | screenshot.captureWindow(); |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 36 | break; |
| 37 | case 'capture_area': |
avm99963 | 873bf6f | 2021-02-07 00:01:31 +0100 | [diff] [blame] | 38 | screenshot.showSelectionArea(); |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 39 | break; |
| 40 | case 'capture_webpage': |
avm99963 | 873bf6f | 2021-02-07 00:01:31 +0100 | [diff] [blame] | 41 | screenshot.captureWebpage(); |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 42 | break; |
| 43 | } |
| 44 | }); |
| 45 | }, |
| 46 | |
| 47 | /** |
| 48 | * Send the Message to content-script |
| 49 | */ |
| 50 | sendMessage: function(message, callback) { |
| 51 | chrome.tabs.getSelected(null, function(tab) { |
| 52 | chrome.tabs.sendMessage(tab.id, message, callback); |
| 53 | }); |
| 54 | }, |
| 55 | |
| 56 | showSelectionArea: function() { |
| 57 | screenshot.sendMessage({msg: 'show_selection_area'}, null); |
| 58 | }, |
| 59 | |
| 60 | captureWindow: function() { |
| 61 | screenshot.sendMessage({msg: 'capture_window'}, |
| 62 | screenshot.onResponseVisibleSize); |
| 63 | }, |
| 64 | |
| 65 | captureSelected: function() { |
| 66 | screenshot.sendMessage({msg: 'capture_selected'}, |
| 67 | screenshot.onResponseVisibleSize); |
| 68 | }, |
| 69 | |
| 70 | captureWebpage: function() { |
| 71 | screenshot.sendMessage({msg: 'scroll_init'}, |
| 72 | screenshot.onResponseVisibleSize); |
| 73 | }, |
| 74 | |
| 75 | onResponseVisibleSize: function(response) { |
| 76 | switch (response.msg) { |
| 77 | case 'capture_window': |
| 78 | screenshot.captureVisible(response.docWidth, response.docHeight); |
| 79 | break; |
| 80 | case 'scroll_init_done': |
| 81 | screenshot.startX = response.startX, |
| 82 | screenshot.startY = response.startY, |
| 83 | screenshot.scrollX = response.scrollX, |
| 84 | screenshot.scrollY = response.scrollY, |
| 85 | screenshot.canvas.width = response.canvasWidth; |
| 86 | screenshot.canvas.height = response.canvasHeight; |
| 87 | screenshot.visibleHeight = response.visibleHeight, |
| 88 | screenshot.visibleWidth = response.visibleWidth, |
| 89 | screenshot.scrollXCount = response.scrollXCount; |
| 90 | screenshot.scrollYCount = response.scrollYCount; |
| 91 | screenshot.docWidth = response.docWidth; |
| 92 | screenshot.docHeight = response.docHeight; |
| 93 | screenshot.zoom = response.zoom; |
| 94 | setTimeout("screenshot.captureAndScroll()", 100); |
| 95 | break; |
| 96 | case 'scroll_next_done': |
| 97 | screenshot.scrollXCount = response.scrollXCount; |
| 98 | screenshot.scrollYCount = response.scrollYCount; |
| 99 | setTimeout("screenshot.captureAndScroll()", 100); |
| 100 | break; |
| 101 | case 'scroll_finished': |
| 102 | screenshot.captureAndScrollDone(); |
| 103 | break; |
| 104 | } |
| 105 | }, |
| 106 | |
| 107 | captureSpecialPage: function() { |
| 108 | var formatParam = localStorage.screenshootQuality || 'png'; |
| 109 | chrome.tabs.captureVisibleTab( |
| 110 | null, {format: formatParam, quality: 50}, function(data) { |
| 111 | var image = new Image(); |
| 112 | image.onload = function() { |
| 113 | screenshot.canvas.width = image.width; |
| 114 | screenshot.canvas.height = image.height; |
| 115 | var context = screenshot.canvas.getContext("2d"); |
| 116 | context.drawImage(image, 0, 0); |
| 117 | screenshot.postImage(); |
| 118 | }; |
| 119 | image.src = data; |
| 120 | }); |
| 121 | }, |
| 122 | |
| 123 | captureScreenCallback: function(data) { |
| 124 | var image = new Image(); |
| 125 | image.onload = function() { |
| 126 | screenshot.canvas.width = image.width; |
| 127 | screenshot.canvas.height = image.height; |
| 128 | var context = screenshot.canvas.getContext("2d"); |
| 129 | context.drawImage(image, 0, 0); |
| 130 | screenshot.postImage(); |
| 131 | }; |
| 132 | image.src = "data:image/bmp;base64," + data; |
| 133 | }, |
| 134 | |
| 135 | /** |
| 136 | * Use drawImage method to slice parts of a source image and draw them to |
| 137 | * the canvas |
| 138 | */ |
| 139 | capturePortion: function(x, y, width, height, |
| 140 | visibleWidth, visibleHeight, docWidth, docHeight) { |
| 141 | var formatParam = localStorage.screenshootQuality || 'png'; |
| 142 | chrome.tabs.captureVisibleTab( |
| 143 | null, {format: formatParam, quality: 50}, function(data) { |
| 144 | var image = new Image(); |
| 145 | image.onload = function() { |
| 146 | var curHeight = image.width < docWidth ? |
| 147 | image.height - screenshot.scrollBarY : image.height; |
| 148 | var curWidth = image.height < docHeight ? |
| 149 | image.width - screenshot.scrollBarX : image.width; |
| 150 | var zoomX = curWidth / visibleWidth; |
| 151 | var zoomY = curHeight / visibleHeight; |
| 152 | screenshot.canvas.width = width * zoomX; |
| 153 | screenshot.canvas.height = height * zoomY; |
| 154 | var context = screenshot.canvas.getContext("2d"); |
| 155 | context.drawImage(image, x * zoomX, y * zoomY, width * zoomX, |
| 156 | height * zoomY, 0, 0, width * zoomX, height * zoomY); |
| 157 | screenshot.postImage(); |
| 158 | }; |
| 159 | image.src = data; |
| 160 | }); |
| 161 | }, |
| 162 | |
| 163 | captureVisible: function(docWidth, docHeight) { |
| 164 | var formatParam = localStorage.screenshootQuality || 'png'; |
| 165 | chrome.tabs.captureVisibleTab( |
| 166 | null, {format: formatParam, quality: 50}, function(data) { |
| 167 | var image = new Image(); |
| 168 | image.onload = function() { |
| 169 | var width = image.height < docHeight ? |
| 170 | image.width - 17 : image.width; |
| 171 | var height = image.width < docWidth ? |
| 172 | image.height - 17 : image.height; |
| 173 | screenshot.canvas.width = width; |
| 174 | screenshot.canvas.height = height; |
| 175 | var context = screenshot.canvas.getContext("2d"); |
| 176 | context.drawImage(image, 0, 0, width, height, 0, 0, width, height); |
| 177 | screenshot.postImage(); |
| 178 | }; |
| 179 | image.src = data; |
| 180 | }); |
| 181 | }, |
| 182 | |
| 183 | /** |
| 184 | * Use the drawImage method to stitching images, and render to canvas |
| 185 | */ |
| 186 | captureAndScroll: function() { |
| 187 | var formatParam = localStorage.screenshootQuality || 'png'; |
| 188 | chrome.tabs.captureVisibleTab( |
| 189 | null, {format: formatParam, quality: 50}, function(data) { |
| 190 | var image = new Image(); |
| 191 | image.onload = function() { |
| 192 | var context = screenshot.canvas.getContext('2d'); |
| 193 | var width = 0; |
| 194 | var height = 0; |
| 195 | |
| 196 | // Get scroll bar's width. |
| 197 | screenshot.scrollBarY = |
| 198 | screenshot.visibleHeight < screenshot.docHeight ? 17 : 0; |
| 199 | screenshot.scrollBarX = |
| 200 | screenshot.visibleWidth < screenshot.docWidth ? 17 : 0; |
| 201 | |
| 202 | // Get visible width and height of capture result. |
| 203 | var visibleWidth = |
| 204 | (image.width - screenshot.scrollBarY < screenshot.canvas.width ? |
| 205 | image.width - screenshot.scrollBarY : screenshot.canvas.width); |
| 206 | var visibleHeight = |
| 207 | (image.height - screenshot.scrollBarX < screenshot.canvas.height ? |
| 208 | image.height - screenshot.scrollBarX : screenshot.canvas.height); |
| 209 | |
| 210 | // Get region capture start x coordinate. |
| 211 | var zoom = screenshot.zoom; |
| 212 | var x1 = screenshot.startX - Math.round(screenshot.scrollX * zoom); |
| 213 | var x2 = 0; |
| 214 | var y1 = screenshot.startY - Math.round(screenshot.scrollY * zoom); |
| 215 | var y2 = 0; |
| 216 | |
| 217 | if ((screenshot.scrollYCount + 1) * visibleWidth > |
| 218 | screenshot.canvas.width) { |
| 219 | width = screenshot.canvas.width % visibleWidth; |
| 220 | x1 = (screenshot.scrollYCount + 1) * visibleWidth - |
| 221 | screenshot.canvas.width + screenshot.startX - screenshot.scrollX; |
| 222 | } else { |
| 223 | width = visibleWidth; |
| 224 | } |
| 225 | |
| 226 | if ((screenshot.scrollXCount + 1) * visibleHeight > |
| 227 | screenshot.canvas.height) { |
| 228 | height = screenshot.canvas.height % visibleHeight; |
| 229 | if ((screenshot.scrollXCount + 1) * visibleHeight + |
| 230 | screenshot.scrollY < screenshot.docHeight) { |
| 231 | y1 = 0; |
| 232 | } else { |
| 233 | y1 = (screenshot.scrollXCount + 1) * visibleHeight + |
| 234 | screenshot.scrollY - screenshot.docHeight; |
| 235 | } |
| 236 | |
| 237 | } else { |
| 238 | height = visibleHeight; |
| 239 | } |
| 240 | x2 = screenshot.scrollYCount * visibleWidth; |
| 241 | y2 = screenshot.scrollXCount * visibleHeight; |
| 242 | context.drawImage(image, x1, y1, width, height, x2, y2, width, height); |
| 243 | screenshot.sendMessage({msg: 'scroll_next', visibleWidth: visibleWidth, |
| 244 | visibleHeight: visibleHeight}, screenshot.onResponseVisibleSize); |
| 245 | }; |
| 246 | image.src = data; |
| 247 | }); |
| 248 | }, |
| 249 | |
| 250 | captureAndScrollDone: function() { |
| 251 | screenshot.postImage(); |
| 252 | }, |
| 253 | |
| 254 | /** |
| 255 | * Post the image to 'showimage.html' |
| 256 | */ |
| 257 | postImage: function() { |
| 258 | chrome.tabs.getSelected(null, function(tab) { |
| 259 | screenshot.tab = tab; |
| 260 | }); |
| 261 | var date = new Date(); |
| 262 | screenshot.screenshotName = "Screenshot "+dateFormat(date, 'Y-m-d H.i.s'); |
| 263 | chrome.tabs.create({'url': 'showimage.html'}); |
| 264 | var popup = chrome.extension.getViews({type: 'popup'})[0]; |
| 265 | if (popup) |
| 266 | popup.close(); |
| 267 | }, |
| 268 | |
| 269 | isThisPlatform: function(operationSystem) { |
| 270 | return navigator.userAgent.toLowerCase().indexOf(operationSystem) > -1; |
| 271 | }, |
| 272 | |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 273 | init: function() { |
| 274 | localStorage.screenshootQuality = localStorage.screenshootQuality || 'png'; |
avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame] | 275 | screenshot.addMessageListener(); |
| 276 | } |
| 277 | }; |
| 278 | |
| 279 | screenshot.init(); |