Mercurial > nightly_tester_tools
comparison chrome/content/screenshot/screenshot.js @ 2:472a16863ecc
expanded nightly.jar
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Tue, 02 Dec 2008 20:38:20 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:93e46514f20d | 2:472a16863ecc |
---|---|
1 const Ci = Components.interfaces; | |
2 const Cc = Components.classes; | |
3 | |
4 var shotWindow = window.opener; | |
5 | |
6 var timer = 0; | |
7 | |
8 var canvas = null; | |
9 var bundle = null; | |
10 | |
11 var windows = []; | |
12 | |
13 function init(event) | |
14 { | |
15 canvas = document.getElementById("canvas"); | |
16 drawScreenshot(); | |
17 //canvas.parentNode.addEventListener("mousedown", startAreaSelect, true); | |
18 | |
19 buildWinPopup() | |
20 | |
21 var winlist = document.getElementById("winlist"); | |
22 winlist.addEventListener("ValueChange", winChange, false); | |
23 | |
24 var winpopup = document.getElementById("winpopup"); | |
25 winpopup.addEventListener("popupshowing", buildWinPopup, false); | |
26 | |
27 bundle = document.getElementById("bundle"); | |
28 | |
29 try | |
30 { | |
31 var win = getTopWin(); | |
32 var webnav = win.content.QueryInterface(Ci.nsIInterfaceRequestor) | |
33 .getInterface(Ci.nsIWebNavigation); | |
34 if (webnav.loadURI) | |
35 document.getElementById("imgsubmit").hidden = false; | |
36 } | |
37 catch (e) | |
38 { | |
39 } | |
40 } | |
41 | |
42 function getTopWin() | |
43 { | |
44 var windowManager = Cc['@mozilla.org/appshell/window-mediator;1'] | |
45 .getService(Ci.nsIWindowMediator); | |
46 return windowManager.getMostRecentWindow("navigator:browser"); | |
47 } | |
48 | |
49 function timedCapture() | |
50 { | |
51 if (timer==0) | |
52 { | |
53 timer = 5; | |
54 var button = document.getElementById("timerbtn"); | |
55 button.checked = true; | |
56 window.setTimeout(captureTimer, 1000); | |
57 } | |
58 } | |
59 | |
60 function captureTimer() | |
61 { | |
62 var button = document.getElementById("timerbtn"); | |
63 timer--; | |
64 if (timer==0) | |
65 { | |
66 drawScreenshot(); | |
67 button.setAttribute("label", bundle.getFormattedString("screenshot.timer.label", [5])); | |
68 button.checked = false; | |
69 } | |
70 else | |
71 { | |
72 button.setAttribute("label", bundle.getFormattedString("screenshot.timer.label", [timer])); | |
73 window.setTimeout(captureTimer, 1000); | |
74 } | |
75 } | |
76 | |
77 function submitScreenshot() | |
78 { | |
79 var fileService = ImageShack; | |
80 | |
81 var data = canvas.toDataURL("image/png"); | |
82 var pos = data.indexOf(";",5); | |
83 var contenttype = data.substring(5,pos); | |
84 var npos = data.indexOf(",",pos+1); | |
85 var encoding = data.substring(pos+1,npos); | |
86 data = data.substring(npos+1); | |
87 | |
88 var fd = Cc["@blueprintit.co.uk/multipartformdata;1"] | |
89 .createInstance(Ci.nttIMultipartFormData); | |
90 fileService.addFormFields(fd); | |
91 fd.addFileData(fileService.getFileFormField(), "screenshot.png", contenttype, encoding, data); | |
92 | |
93 var ioService = Cc["@mozilla.org/network/io-service;1"] | |
94 .getService(Ci.nsIIOService); | |
95 | |
96 var referer = ioService.newURI(fileService.getReferer(), "UTF8", null); | |
97 | |
98 var win = getTopWin(); | |
99 var webnav = win.content.QueryInterface(Ci.nsIInterfaceRequestor) | |
100 .getInterface(Ci.nsIWebNavigation); | |
101 webnav.loadURI(fileService.getSubmissionURL(), Ci.nsIWebNavigation.LOAD_FLAGS_NONE | |
102 , referer, fd.getPostDataStream(), fd.getHeaderStream()); | |
103 } | |
104 | |
105 function saveScreenshot() | |
106 { | |
107 var fp = Cc["@mozilla.org/filepicker;1"] | |
108 .createInstance(Ci.nsIFilePicker); | |
109 fp.init(window, bundle.getString("screenshot.filepicker.title"), fp.modeSave); | |
110 fp.appendFilter(bundle.getString("screenshot.filepicker.filterPNG"), "*.png"); | |
111 fp.appendFilter(bundle.getString("screenshot.filepicker.filterJPG"), "*.jpg"); | |
112 fp.filterIndex = 0; | |
113 fp.defaultString="screenshot"; | |
114 | |
115 var result = fp.show(); | |
116 if (result==fp.returnOK || result==fp.returnReplace) | |
117 { | |
118 var file = fp.file; | |
119 var mimetype = "image/png"; | |
120 var options = ""; | |
121 var extension = "png"; | |
122 if (fp.filterIndex == 1) | |
123 { | |
124 extension = "jpg"; | |
125 mimetype = "image/jpeg"; | |
126 options = "quality=80"; | |
127 } | |
128 | |
129 if (file.leafName.indexOf(".") < 0) | |
130 file.leafName += "." + extension; | |
131 | |
132 var ioService = Cc["@mozilla.org/network/io-service;1"] | |
133 .getService(Ci.nsIIOService); | |
134 | |
135 var source = ioService.newURI(canvas.toDataURL(mimetype, options), "UTF8", null); | |
136 var target = ioService.newFileURI(file) | |
137 | |
138 var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] | |
139 .createInstance(Ci.nsIWebBrowserPersist); | |
140 | |
141 persist.persistFlags = Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES; | |
142 persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; | |
143 | |
144 var tr = Cc["@mozilla.org/transfer;1"] | |
145 .createInstance(Ci.nsITransfer); | |
146 | |
147 tr.init(source, target, "", null, null, null, persist); | |
148 persist.progressListener = tr; | |
149 persist.saveURI(source, null, null, null, null, file); | |
150 } | |
151 } | |
152 | |
153 function copyScreenshot() | |
154 { | |
155 var image = document.getElementById("previewImage"); | |
156 var docshell = window.QueryInterface(Ci.nsIInterfaceRequestor) | |
157 .getInterface(Ci.nsIWebNavigation) | |
158 .QueryInterface(Ci.nsIDocShell); | |
159 var edit = docshell.contentViewer.QueryInterface(Ci.nsIContentViewerEdit); | |
160 document.popupNode = image; | |
161 edit.copyImage(Ci.nsIContentViewerEdit.COPY_IMAGE_DATA); | |
162 } | |
163 | |
164 function buildWinPopup(event) | |
165 { | |
166 var winlist = document.getElementById("winlist"); | |
167 var winpopup = document.getElementById("winpopup"); | |
168 | |
169 windows = []; | |
170 while (winpopup.firstChild) | |
171 winpopup.removeChild(winpopup.firstChild); | |
172 | |
173 var wm = Cc["@mozilla.org/appshell/window-mediator;1"] | |
174 .getService(Ci.nsIWindowMediator); | |
175 var wins = wm.getEnumerator(null); | |
176 var pos = 0; | |
177 while (wins.hasMoreElements()) | |
178 { | |
179 var win = wins.getNext().QueryInterface(Ci.nsIDOMWindow); | |
180 if (win != window) | |
181 { | |
182 windows[pos] = win; | |
183 var item = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuitem"); | |
184 if (win.document.title) | |
185 item.setAttribute("label", win.document.title); | |
186 else | |
187 item.setAttribute("label", win.document.location.href); | |
188 item.setAttribute("value", pos); | |
189 winpopup.appendChild(item); | |
190 | |
191 if (!event && win==shotWindow) | |
192 winlist.value=pos; | |
193 | |
194 pos++; | |
195 } | |
196 } | |
197 } | |
198 | |
199 function winChange(event) | |
200 { | |
201 var winlist = document.getElementById("winlist"); | |
202 shotWindow = windows[winlist.value]; | |
203 drawScreenshot(); | |
204 } | |
205 | |
206 function drawScreenshot() | |
207 { | |
208 var width = shotWindow.innerWidth; | |
209 var height = shotWindow.innerHeight; | |
210 canvas.width = width; | |
211 canvas.height = height; | |
212 canvas.style.width = width + "px"; | |
213 canvas.style.minWidth = width + "px"; | |
214 canvas.style.maxWidth = width + "px"; | |
215 canvas.style.height = height + "px"; | |
216 canvas.style.minHeight = height + "px"; | |
217 canvas.style.maxHeight = height + "px"; | |
218 | |
219 var ctx = canvas.getContext("2d"); | |
220 | |
221 var winbo = shotWindow.document.getBoxObjectFor(shotWindow.document.documentElement); | |
222 var winx = winbo.screenX; | |
223 var winy = winbo.screenY; | |
224 | |
225 // This draws the main window | |
226 try | |
227 { | |
228 ctx.drawWindow(shotWindow, shotWindow.scrollX, shotWindow.scrollY, | |
229 shotWindow.innerWidth, shotWindow.innerHeight, | |
230 "rgba(255,255,255,255)"); | |
231 } | |
232 catch (e) | |
233 { | |
234 } | |
235 | |
236 // Must also draw inner windows as inner-content from chrome are not included | |
237 var docshell = shotWindow.QueryInterface(Ci.nsIInterfaceRequestor) | |
238 .getInterface(Ci.nsIWebNavigation) | |
239 .QueryInterface(Ci.nsIDocShell); | |
240 var shells = docshell.getDocShellEnumerator(Ci.nsIDocShellTreeItem.typeAll, Ci.nsIDocShell.ENUMERATE_FORWARDS); | |
241 while (shells.hasMoreElements()) | |
242 { | |
243 var shell = shells.getNext().QueryInterface(Ci.nsIDocShell); | |
244 try | |
245 { | |
246 if (shell == docshell) | |
247 continue; | |
248 | |
249 shell.QueryInterface(Ci.nsIBaseWindow); | |
250 if (!shell.visibility) | |
251 continue; | |
252 | |
253 var shellwin = shell.QueryInterface(Ci.nsIInterfaceRequestor) | |
254 .getInterface(Ci.nsIDOMWindow); | |
255 var shellbo = shellwin.document.getBoxObjectFor(shellwin.document.documentElement); | |
256 | |
257 ctx.save(); | |
258 try | |
259 { | |
260 ctx.translate(shellbo.screenX - winx + shellwin.scrollX, | |
261 shellbo.screenY - winy + shellwin.scrollY); | |
262 ctx.drawWindow(shellwin, shellwin.scrollX, shellwin.scrollY, | |
263 shellwin.innerWidth, shellwin.innerHeight, | |
264 "rgba(255,255,255,255)"); | |
265 } | |
266 catch (e) | |
267 { | |
268 } | |
269 ctx.restore(); | |
270 } | |
271 catch (e) | |
272 { | |
273 } | |
274 } | |
275 | |
276 var url = canvas.toDataURL(); | |
277 var oldImage = document.getElementById("previewImage"); | |
278 if (oldImage) | |
279 oldImage.parentNode.removeChild(oldImage); | |
280 | |
281 var image = document.createElementNS("http://www.w3.org/1999/xhtml", "img"); | |
282 image.id = "previewImage"; | |
283 document.getElementById("container").appendChild(image); | |
284 | |
285 image.style.width = width + "px"; | |
286 image.style.height = height + "px"; | |
287 image.width = width; | |
288 image.height = height; | |
289 image.src = url; | |
290 } | |
291 | |
292 window.addEventListener("load", init, false); |