Skip to content

Commit 9f10f4d

Browse files
committed
replace phantom with puppeteer/chromium
1 parent b6ede40 commit 9f10f4d

File tree

4 files changed

+34
-1842
lines changed

4 files changed

+34
-1842
lines changed

anon.js

+31-38
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
const fs = require('fs')
44
const Twit = require('twit')
55
const async = require('async')
6-
const phantom = require('phantom')
76
const minimist = require('minimist')
87
const Mastodon = require('mastodon')
98
const Mustache = require('mustache')
9+
const puppeteer = require('puppeteer')
1010
const {WikiChanges} = require('wikichanges')
1111
const {Address4, Address6} = require('ip-address')
1212

@@ -114,44 +114,34 @@ function isRepeat(edit) {
114114
return r
115115
}
116116

117-
function takeScreenshot(url) {
118-
return new Promise(function(resolve, reject) {
119-
phantom.create(['--ignore-ssl-errors=yes']).then(function(browser) {
120-
var filename = new Date().toString() + '.png'
121-
browser.createPage().then(function(page) {
122-
page.property('viewportSize', {width: 1024, height: 768}).then(function() {
123-
page.open(url).then(function(status) {
124-
if (status === 'fail') {
125-
cb('fail', null)
126-
} else {
127-
page.evaluate(function() {
128-
try {
129-
var diffBoundingRect = document.querySelector('table.diff.diff-contentalign-left').getBoundingClientRect()
130-
// for some reason phantomjs doesn't seem to get the sizing right
131-
return {
132-
top: diffBoundingRect.top,
133-
left: diffBoundingRect.left,
134-
width: diffBoundingRect.width + 75,
135-
height: diffBoundingRect.height,
136-
}
137-
} catch(e) {
138-
console.log('Error: no diff found on wikipedia page')
139-
}
140-
}).then(function(clipRect) {
141-
page.property('clipRect', clipRect).then(function() {
142-
page.render(filename).then(function() {
143-
browser.exit().then(function() {
144-
resolve(filename)
145-
})
146-
})
147-
})
148-
})
149-
}
150-
})
151-
})
152-
})
153-
})
117+
async function takeScreenshot(url) {
118+
const browser = await puppeteer.launch({headless: true, defaultViewport: null})
119+
const page = await browser.newPage()
120+
await page.goto(url, {waitUntil: 'networkidle2'})
121+
await page.setViewport({width: 1024, height: 768})
122+
123+
const filename = Date.now() + '.png'
124+
const selector = 'table.diff.diff-contentalign-left'
125+
const padding = 0
126+
127+
const rect = await page.evaluate(selector => {
128+
const element = document.querySelector(selector)
129+
const {x, y, width, height} = element.getBoundingClientRect()
130+
return {left: x, top: y, width, height, id: element.id}
131+
}, selector)
132+
133+
await page.screenshot({
134+
path: filename,
135+
clip: {
136+
x: rect.left - padding,
137+
y: rect.top - padding,
138+
width: rect.width + padding * 2,
139+
height: rect.height + padding * 2
140+
}
154141
})
142+
143+
await browser.close()
144+
return filename
155145
}
156146

157147
function sendStatus(account, status, edit) {
@@ -284,6 +274,9 @@ function main() {
284274
})
285275
}
286276

277+
async function getClip(page, selector, path, padding=16) {
278+
}
279+
287280
if (require.main === module) {
288281
main()
289282
}

0 commit comments

Comments
 (0)