Skip to content

Commit 39470b0

Browse files
committed
added some async to remove callback nesting hell
1 parent 9f10f4d commit 39470b0

File tree

1 file changed

+47
-54
lines changed

1 file changed

+47
-54
lines changed

anon.js

+47-54
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ async function takeScreenshot(url) {
122122

123123
const filename = Date.now() + '.png'
124124
const selector = 'table.diff.diff-contentalign-left'
125-
const padding = 0
126125

127126
const rect = await page.evaluate(selector => {
128127
const element = document.querySelector(selector)
@@ -133,77 +132,74 @@ async function takeScreenshot(url) {
133132
await page.screenshot({
134133
path: filename,
135134
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
140139
}
141140
})
142141

143142
await browser.close()
144143
return filename
145144
}
146145

147-
function sendStatus(account, status, edit) {
146+
async function sendStatus(account, status, edit) {
148147
console.log(status)
149148

150149
if (!argv.noop && (!account.throttle || !isRepeat(edit))) {
151150

152-
takeScreenshot(edit.url).then(function(screenshot) {
151+
const screenshot = await takeScreenshot(edit.url)
153152

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
169160
}
170161

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+
}
175168

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'})
182173

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+
}
187180

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) {
189195
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)
195197
}
196-
twitter.post('statuses/update', params, function(err) {
197-
if (err) {
198-
console.log(err)
199-
}
200-
})
201-
fs.unlinkSync(screenshot)
202198
})
199+
fs.unlinkSync(screenshot)
203200
})
204-
}
205-
206-
})
201+
})
202+
}
207203
}
208204
}
209205

@@ -274,9 +270,6 @@ function main() {
274270
})
275271
}
276272

277-
async function getClip(page, selector, path, padding=16) {
278-
}
279-
280273
if (require.main === module) {
281274
main()
282275
}

0 commit comments

Comments
 (0)