feat(app/metrics): add support for postscripts processors
This commit is contained in:
@@ -25,7 +25,7 @@ export default async function metrics({login, q}, {graphql, rest, plugins, conf,
|
||||
const pending = []
|
||||
const {queries} = conf
|
||||
const extras = {css:(conf.settings.extras?.css ?? conf.settings.extras?.default) ? q["extras.css"] ?? "" : "", js:(conf.settings.extras?.js ?? conf.settings.extras?.default) ? q["extras.js"] ?? "" : ""}
|
||||
const data = {q, animated:true, large:false, base:{}, config:{}, errors:[], plugins:{}, computed:{}, extras}
|
||||
const data = {q, animated:true, large:false, base:{}, config:{}, errors:[], plugins:{}, computed:{}, extras, postscripts:[]}
|
||||
const imports = {
|
||||
plugins:Plugins,
|
||||
templates:Templates,
|
||||
@@ -189,7 +189,7 @@ export default async function metrics({login, q}, {graphql, rest, plugins, conf,
|
||||
console.debug(`metrics/compute/${login} > verified SVG, no parsing errors found`)
|
||||
}
|
||||
//Resizing
|
||||
const {resized, mime} = await imports.svg.resize(rendered, {paddings:q["config.padding"] || conf.settings.padding, convert:convert === "svg" ? null : convert, js:extras.js || null})
|
||||
const {resized, mime} = await imports.svg.resize(rendered, {paddings:q["config.padding"] || conf.settings.padding, convert:convert === "svg" ? null : convert, scripts:[...data.postscripts, extras.js || null].filter(x => x)})
|
||||
rendered = resized
|
||||
|
||||
//Result
|
||||
|
||||
@@ -401,7 +401,7 @@ export const svg = {
|
||||
return {rendered, mime:"application/pdf"}
|
||||
},
|
||||
/**Render and resize svg */
|
||||
async resize(rendered, {paddings, convert, js}) {
|
||||
async resize(rendered, {paddings, convert, scripts = []}) {
|
||||
//Instantiate browser if needed
|
||||
if (!svg.resize.browser) {
|
||||
svg.resize.browser = await puppeteer.launch()
|
||||
@@ -436,16 +436,16 @@ export const svg = {
|
||||
let height, resized, width
|
||||
try {
|
||||
({resized, width, height} = await page.evaluate(
|
||||
async (padding, js) => {
|
||||
//Execute user JavaScript if provided
|
||||
if (js) {
|
||||
async (padding, scripts) => {
|
||||
//Execute additional JavaScript
|
||||
for (const script of scripts) {
|
||||
try {
|
||||
console.debug(`metrics/svg/resize > executing ${js}`)
|
||||
await new Function("document", `return (async () => {${js}})()`)(document) //eslint-disable-line no-new-func
|
||||
console.debug(`metrics/svg/resize > executing ${script}`)
|
||||
await new Function("document", `return (async () => {${script}})()`)(document) //eslint-disable-line no-new-func
|
||||
console.debug("metrics/svg/resize > successfully executed user javascript")
|
||||
}
|
||||
catch (error) {
|
||||
console.debug(`an error occured while evaluating user js: ${error}`)
|
||||
console.debug(`an error occured while evaluating script: ${error}`)
|
||||
}
|
||||
}
|
||||
//Disable animations
|
||||
@@ -472,7 +472,7 @@ export const svg = {
|
||||
return {resized:new XMLSerializer().serializeToString(document.querySelector("svg")), height, width}
|
||||
},
|
||||
padding,
|
||||
js,
|
||||
scripts,
|
||||
))
|
||||
}
|
||||
catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user