@@ -122,7 +122,6 @@ async function takeScreenshot(url) {
122
122
123
123
const filename = Date . now ( ) + '.png'
124
124
const selector = 'table.diff.diff-contentalign-left'
125
- const padding = 0
126
125
127
126
const rect = await page . evaluate ( selector => {
128
127
const element = document . querySelector ( selector )
@@ -133,77 +132,74 @@ async function takeScreenshot(url) {
133
132
await page . screenshot ( {
134
133
path : filename ,
135
134
clip : {
136
- x : rect . left - padding ,
137
- y : rect . top - padding ,
138
- width : rect . width + padding * 2 ,
139
- height : rect . height + padding * 2
135
+ x : rect . left ,
136
+ y : rect . top ,
137
+ width : rect . width ,
138
+ height : rect . height
140
139
}
141
140
} )
142
141
143
142
await browser . close ( )
144
143
return filename
145
144
}
146
145
147
- function sendStatus ( account , status , edit ) {
146
+ async function sendStatus ( account , status , edit ) {
148
147
console . log ( status )
149
148
150
149
if ( ! argv . noop && ( ! account . throttle || ! isRepeat ( edit ) ) ) {
151
150
152
- takeScreenshot ( edit . url ) . then ( function ( screenshot ) {
151
+ const screenshot = await takeScreenshot ( edit . url )
153
152
154
- // Mastodon
155
- if ( account . mastodon ) {
156
- const mastodon = new Mastodon ( account . mastodon )
157
- mastodon . post ( 'media' , { file : fs . createReadStream ( screenshot ) } )
158
- . then ( function ( response ) {
159
- if ( ! response . data . id ) {
160
- console . log ( 'error uploading screenshot to mastodon' )
161
- return
162
- }
163
- mastodon . post ( 'statuses' , { 'status' : status , media_ids : [ response . data . id ] } , function ( err ) {
164
- if ( err ) {
165
- console . log ( err )
166
- }
167
- } )
168
- } )
153
+ // Mastodon
154
+ if ( account . mastodon ) {
155
+ const mastodon = new Mastodon ( account . mastodon )
156
+ const response = await mastodon . post ( 'media' , { file : fs . createReadStream ( screenshot ) } )
157
+ if ( ! response . data . id ) {
158
+ console . log ( 'error uploading screenshot to mastodon' )
159
+ return
169
160
}
170
161
171
- // Twitter
172
- if ( account . access_token ) {
173
- const twitter = new Twit ( account )
174
- const b64content = fs . readFileSync ( screenshot , { encoding : 'base64' } )
162
+ await mastodon . post (
163
+ 'statuses' ,
164
+ { 'status' : status , media_ids : [ response . data . id ] } ,
165
+ err => console . log ( `mastodon post failed: ${ err } ` )
166
+ )
167
+ }
175
168
176
- // upload the screenshot to twitter
177
- twitter . post ( 'media/upload' , { media_data : b64content } , function ( err , data , response ) {
178
- if ( err ) {
179
- console . log ( err )
180
- return
181
- }
169
+ // Twitter
170
+ if ( account . access_token ) {
171
+ const twitter = new Twit ( account )
172
+ const b64content = fs . readFileSync ( screenshot , { encoding : 'base64' } )
182
173
183
- // add alt text for the media, for use by screen readers
184
- const mediaIdStr = data . media_id_string
185
- const altText = "Screenshot of edit to " + edit . page
186
- const metaParams = { media_id : mediaIdStr , alt_text : { text : altText } }
174
+ // upload the screenshot to twitter
175
+ twitter . post ( 'media/upload' , { media_data : b64content } , function ( err , data , response ) {
176
+ if ( err ) {
177
+ console . log ( err )
178
+ return
179
+ }
187
180
188
- twitter . post ( 'media/metadata/create' , metaParams , function ( err , data , response ) {
181
+ // add alt text for the media, for use by screen readers
182
+ const mediaIdStr = data . media_id_string
183
+ const altText = "Screenshot of edit to " + edit . page
184
+ const metaParams = { media_id : mediaIdStr , alt_text : { text : altText } }
185
+
186
+ twitter . post ( 'media/metadata/create' , metaParams , function ( err , data , response ) {
187
+ if ( err ) {
188
+ console . log ( 'metadata upload for twitter screenshot alt text failed with error' , err )
189
+ }
190
+ const params = {
191
+ 'status' : status ,
192
+ 'media_ids' : [ mediaIdStr ]
193
+ }
194
+ twitter . post ( 'statuses/update' , params , function ( err ) {
189
195
if ( err ) {
190
- console . log ( 'metadata upload for twitter screenshot alt text failed with error' , err )
191
- }
192
- const params = {
193
- 'status' : status ,
194
- 'media_ids' : [ mediaIdStr ]
196
+ console . log ( err )
195
197
}
196
- twitter . post ( 'statuses/update' , params , function ( err ) {
197
- if ( err ) {
198
- console . log ( err )
199
- }
200
- } )
201
- fs . unlinkSync ( screenshot )
202
198
} )
199
+ fs . unlinkSync ( screenshot )
203
200
} )
204
- }
205
-
206
- } )
201
+ } )
202
+ }
207
203
}
208
204
}
209
205
@@ -274,9 +270,6 @@ function main() {
274
270
} )
275
271
}
276
272
277
- async function getClip ( page , selector , path , padding = 16 ) {
278
- }
279
-
280
273
if ( require . main === module ) {
281
274
main ( )
282
275
}
0 commit comments