|
3 | 3 | const fs = require('fs')
|
4 | 4 | const Twit = require('twit')
|
5 | 5 | const async = require('async')
|
6 |
| -const phantom = require('phantom') |
7 | 6 | const minimist = require('minimist')
|
8 | 7 | const Mastodon = require('mastodon')
|
9 | 8 | const Mustache = require('mustache')
|
| 9 | +const puppeteer = require('puppeteer') |
10 | 10 | const {WikiChanges} = require('wikichanges')
|
11 | 11 | const {Address4, Address6} = require('ip-address')
|
12 | 12 |
|
@@ -114,44 +114,34 @@ function isRepeat(edit) {
|
114 | 114 | return r
|
115 | 115 | }
|
116 | 116 |
|
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 | + } |
154 | 141 | })
|
| 142 | + |
| 143 | + await browser.close() |
| 144 | + return filename |
155 | 145 | }
|
156 | 146 |
|
157 | 147 | function sendStatus(account, status, edit) {
|
@@ -284,6 +274,9 @@ function main() {
|
284 | 274 | })
|
285 | 275 | }
|
286 | 276 |
|
| 277 | +async function getClip(page, selector, path, padding=16) { |
| 278 | +} |
| 279 | + |
287 | 280 | if (require.main === module) {
|
288 | 281 | main()
|
289 | 282 | }
|
|
0 commit comments