@@ -9,7 +9,6 @@ function BugForm() {
9
9
this . reportButton = $ ( '#js-ReportBug' ) ;
10
10
this . loaderImage = $ ( '.js-Loader' ) ;
11
11
this . uploadLoader = $ ( '.js-Upload-Loader' ) ;
12
- this . screenshotData = '' ;
13
12
// by default, submission type is anonymous
14
13
this . submitType = 'github-proxy-report' ;
15
14
this . UPLOAD_LIMIT = 1024 * 1024 * 4 ;
@@ -77,23 +76,34 @@ function BugForm() {
77
76
// Set up listener for message events from screenshot-enabled add-ons
78
77
window . addEventListener ( 'message' , _ . bind ( function ( event ) {
79
78
// Make sure the data is coming from ~*inside the house*~!
80
- // (i.e., our add-on sent it)
79
+ // (i.e., our add-on or some other priviledged code sent it)
81
80
if ( location . origin === event . origin ) {
82
- this . screenshotData = event . data ;
83
-
84
- // The final size of Base64-encoded binary data is ~equal to
85
- // 1.37 times the original data size + 814 bytes (for headers).
86
- // so, bytes = (encoded_string.length - 814) / 1.37)
87
- // see https://en.wikipedia.org/wiki/Base64#MIME
88
- if ( ( String ( this . screenshotData ) . length - 814 / 1.37 ) > this . UPLOAD_LIMIT ) {
89
- this . downsampleImageAndUpload ( this . screenshotData ) ;
81
+ // See https://github.com/webcompat/webcompat.com/issues/1252 to track
82
+ // the work of only accepting blobs, which should simplify things.
83
+ if ( event . data instanceof Blob ) {
84
+ // showUploadPreview will take care of converting from blob to
85
+ // dataURI, and will send the result to resampleIfNecessaryAndUpload.
86
+ this . showUploadPreview ( event . data ) ;
90
87
} else {
91
- this . addPreviewBackgroundAndUpload ( this . screenshotData ) ;
88
+ // ...the data is already a data URI string
89
+ this . resampleIfNecessaryAndUpload ( event . data ) ;
92
90
}
93
91
}
94
92
} , this ) , false ) ;
95
93
} ;
96
94
95
+ this . resampleIfNecessaryAndUpload = function ( screenshotData ) {
96
+ // The final size of Base64-encoded binary data is ~equal to
97
+ // 1.37 times the original data size + 814 bytes (for headers).
98
+ // so, bytes = (encoded_string.length - 814) / 1.37)
99
+ // see https://en.wikipedia.org/wiki/Base64#MIME
100
+ if ( ( String ( screenshotData ) . length - 814 / 1.37 ) > this . UPLOAD_LIMIT ) {
101
+ this . downsampleImageAndUpload ( screenshotData ) ;
102
+ } else {
103
+ this . addPreviewBackgroundAndUpload ( screenshotData ) ;
104
+ }
105
+ } ;
106
+
97
107
this . downsampleImageAndUpload = function ( dataURI ) {
98
108
var img = document . createElement ( 'img' ) ;
99
109
var canvas = document . createElement ( 'canvas' ) ;
@@ -110,16 +120,16 @@ function BugForm() {
110
120
// Note: this will convert GIFs to JPEG, which breaks
111
121
// animated GIFs. However, this only will happen if they
112
122
// were above the upload limit size. So... sorry?
113
- this . screenshotData = canvas . toDataURL ( 'image/jpeg' , 0.8 ) ;
123
+ var screenshotData = canvas . toDataURL ( 'image/jpeg' , 0.8 ) ;
114
124
115
125
// The limit is 4MB (which is crazy big!), so let the user know if their
116
126
// file is unreasonably large at this point (after 1 round of downsampling)
117
- if ( this . screenshotData > this . UPLOAD_LIMIT ) {
127
+ if ( screenshotData > this . UPLOAD_LIMIT ) {
118
128
this . makeInvalid ( 'image' , { altHelp : true } ) ;
119
129
return ;
120
130
}
121
131
122
- this . addPreviewBackgroundAndUpload ( this . screenshotData ) ;
132
+ this . addPreviewBackgroundAndUpload ( screenshotData ) ;
123
133
img = null , canvas = null ;
124
134
} , this ) ;
125
135
@@ -184,7 +194,9 @@ function BugForm() {
184
194
} else {
185
195
this . makeValid ( 'image' ) ;
186
196
if ( event ) {
187
- this . showUploadPreview ( event ) ;
197
+ // We can just grab the 0th one, because we only allow uploading
198
+ // a single image at a time (for now)
199
+ this . showUploadPreview ( event . target . files [ 0 ] ) ;
188
200
}
189
201
}
190
202
} ;
@@ -312,30 +324,23 @@ function BugForm() {
312
324
If the users browser understands the FileReader API, show a preview
313
325
of the image they're about to load.
314
326
*/
315
- this . showUploadPreview = function ( event ) {
327
+ this . showUploadPreview = function ( blobOrFile ) {
316
328
if ( ! ( window . FileReader && window . File ) ) {
317
329
return ;
318
330
}
319
- // We can just grab the 0th one, because we only allow uploading
320
- // a single image at a time (for now)
321
- var img = event . target . files [ 0 ] ;
322
331
323
332
// One last image type validation check.
324
- if ( ! img . type . match ( 'image.*' ) ) {
333
+ if ( ! blobOrFile . type . match ( 'image.*' ) ) {
325
334
this . makeInvalid ( 'image' ) ;
326
335
return ;
327
336
}
328
337
329
338
var reader = new FileReader ( ) ;
330
339
reader . onload = _ . bind ( function ( event ) {
331
340
var dataURI = event . target . result ;
332
- if ( ( String ( dataURI ) . length - 814 / 1.37 ) > this . UPLOAD_LIMIT ) {
333
- this . downsampleImageAndUpload ( dataURI ) ;
334
- } else {
335
- this . addPreviewBackgroundAndUpload ( dataURI ) ;
336
- }
341
+ this . resampleIfNecessaryAndUpload ( dataURI ) ;
337
342
} , this ) ;
338
- reader . readAsDataURL ( img ) ;
343
+ reader . readAsDataURL ( blobOrFile ) ;
339
344
} ;
340
345
341
346
this . addPreviewBackgroundAndUpload = function ( dataURI ) {
0 commit comments