|
|
@ -1,11 +1,11 @@
|
|
|
|
/* global domtoimage */
|
|
|
|
/* global domtoimage */
|
|
|
|
const ARBITRARY_WAIT_TIME = 500
|
|
|
|
const ARBITRARY_WAIT_TIME = 250
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = browser => async (req, res) => {
|
|
|
|
module.exports = browser => async (req, res) => {
|
|
|
|
const page = await browser.newPage()
|
|
|
|
const page = await browser.newPage()
|
|
|
|
const { state } = req.body
|
|
|
|
const { state } = req.body
|
|
|
|
|
|
|
|
|
|
|
|
if (!state) res.status(400).send()
|
|
|
|
if (!state) res.status(400).send('Invalid Request')
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const path = require.resolve('dom-to-image')
|
|
|
|
const path = require.resolve('dom-to-image')
|
|
|
@ -19,34 +19,47 @@ module.exports = browser => async (req, res) => {
|
|
|
|
const targetElement = await page.$('.export-container')
|
|
|
|
const targetElement = await page.$('.export-container')
|
|
|
|
|
|
|
|
|
|
|
|
const dataUrl = await page.evaluate((target = document) => {
|
|
|
|
const dataUrl = await page.evaluate((target = document) => {
|
|
|
|
|
|
|
|
const query = new URLSearchParams(document.location.search.slice(1))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const EXPORT_SIZES_HASH = {
|
|
|
|
|
|
|
|
'1x': '1',
|
|
|
|
|
|
|
|
'2x': '2',
|
|
|
|
|
|
|
|
'4x': '4'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const exportSize = EXPORT_SIZES_HASH[query.get('es')] || '2'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
target.querySelectorAll('span[role="presentation"]').forEach(node => {
|
|
|
|
|
|
|
|
if (node.innerText && node.innerText.match(/%\S\S/)) {
|
|
|
|
|
|
|
|
node.innerText = encodeURIComponent(node.innerText)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const width = target.offsetWidth * exportSize
|
|
|
|
|
|
|
|
const height = query.get('si')
|
|
|
|
|
|
|
|
? target.offsetWidth * exportSize
|
|
|
|
|
|
|
|
: target.offsetHeight * exportSize
|
|
|
|
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
const config = {
|
|
|
|
style: {
|
|
|
|
style: {
|
|
|
|
transform: 'scale(2)',
|
|
|
|
transform: `scale(${exportSize})`,
|
|
|
|
'transform-origin': 'center'
|
|
|
|
'transform-origin': 'center',
|
|
|
|
|
|
|
|
background: query.get('si') ? query.get('bg') : 'none'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
filter: n => {
|
|
|
|
filter: n => {
|
|
|
|
// %[00 -> 19] cause failures
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
n.innerText &&
|
|
|
|
|
|
|
|
n.innerText.match(/%\d\S/) &&
|
|
|
|
|
|
|
|
n.className &&
|
|
|
|
|
|
|
|
n.className.startsWith('cm-') // is CodeMirror primitive string
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
n.innerText = encodeURIComponent(n.innerText)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n.className) {
|
|
|
|
if (n.className) {
|
|
|
|
return String(n.className).indexOf('eliminateOnRender') < 0
|
|
|
|
return String(n.className).indexOf('eliminateOnRender') < 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
width: target.offsetWidth * 2,
|
|
|
|
width,
|
|
|
|
height: target.offsetHeight * 2
|
|
|
|
height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return domtoimage.toPng(target, config)
|
|
|
|
return domtoimage.toPng(target, config)
|
|
|
|
}, targetElement)
|
|
|
|
}, targetElement)
|
|
|
|
|
|
|
|
|
|
|
|
res.status(200).json({ dataUrl })
|
|
|
|
res.status(200).send(dataUrl)
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.error(e)
|
|
|
|
console.error(e)
|
|
|
|