admin管理员组文章数量:1026989
I want to find average color of all images in the page. I am using Puppeteer to launch chromium and then run javascript inside browser.
Problem is that I am using Promies.all to asynchronously calculate all colors inside browser.
I want to return calculated image colors to page.evaluate. How can I do that? Is it possible?
Following code does not work, because imgcolors is no defined obviously, it is returned inside Promise.all callback.
const returned = await page.evaluate(async () => {
const fac = new FastAverageColor();
const imgs = Array.from(document.querySelectorAll('img'));
const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
Promise.all(imgpromises).then(imgcolors => { return imgcolors;});
return {
document:{
height: document.body.scrollHeight,
width:document.body.scrollWidth
},
imgcolors:imgcolors
};
});
I have tried following:
Promise.all(imgpromises).then(imgcolors => {
return {
elements:elements,
document:{
height: document.body.scrollHeight,
width:document.body.scrollWidth
},
imgcolors:imgcolors
};
});
Now I am not returing data to page.evaluate puppeteer function. So const returned is undefined.
How to do that? Thank you in advance!
I want to find average color of all images in the page. I am using Puppeteer to launch chromium and then run javascript inside browser.
Problem is that I am using Promies.all to asynchronously calculate all colors inside browser.
I want to return calculated image colors to page.evaluate. How can I do that? Is it possible?
Following code does not work, because imgcolors is no defined obviously, it is returned inside Promise.all callback.
const returned = await page.evaluate(async () => {
const fac = new FastAverageColor();
const imgs = Array.from(document.querySelectorAll('img'));
const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
Promise.all(imgpromises).then(imgcolors => { return imgcolors;});
return {
document:{
height: document.body.scrollHeight,
width:document.body.scrollWidth
},
imgcolors:imgcolors
};
});
I have tried following:
Promise.all(imgpromises).then(imgcolors => {
return {
elements:elements,
document:{
height: document.body.scrollHeight,
width:document.body.scrollWidth
},
imgcolors:imgcolors
};
});
Now I am not returing data to page.evaluate puppeteer function. So const returned is undefined.
How to do that? Thank you in advance!
Share Improve this question asked Nov 26, 2020 at 0:55 Tomas ZubrikTomas Zubrik 5176 silver badges14 bronze badges1 Answer
Reset to default 6In the page.evaluate
function, you can return a promise, which will resolve to a value. In this case Promise.all
returns a promise, which you can chain to return the value you want. It'd look something like this:
const returned = await page.evaluate(() => {
const fac = new FastAverageColor();
const imgs = Array.from(document.querySelectorAll('img'));
const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
return Promise.all(imgpromises).then(imgcolors => {
return {
document:{
height: document.body.scrollHeight,
width: document.body.scrollWidth
},
imgcolors: imgcolors
};
});
});
Alternatively, since you are using async
functions, you can just use the await
operator to wait for values to be resolved:
const returned = await page.evaluate(async () => {
const fac = new FastAverageColor();
const imgs = Array.from(document.querySelectorAll('img'));
const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
const imgcolors = await Promise.all(imgpromises);
return {
document:{
height: document.body.scrollHeight,
width: document.body.scrollWidth
},
imgcolors: imgcolors
};
});
Note that page.evaluate
can only return serializable values, i.e. things that can be JSON.stringify
'd. It looks like imgcolors
should be serializable, but in other cases, you might need to convert the values to something serializable.
I want to find average color of all images in the page. I am using Puppeteer to launch chromium and then run javascript inside browser.
Problem is that I am using Promies.all to asynchronously calculate all colors inside browser.
I want to return calculated image colors to page.evaluate. How can I do that? Is it possible?
Following code does not work, because imgcolors is no defined obviously, it is returned inside Promise.all callback.
const returned = await page.evaluate(async () => {
const fac = new FastAverageColor();
const imgs = Array.from(document.querySelectorAll('img'));
const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
Promise.all(imgpromises).then(imgcolors => { return imgcolors;});
return {
document:{
height: document.body.scrollHeight,
width:document.body.scrollWidth
},
imgcolors:imgcolors
};
});
I have tried following:
Promise.all(imgpromises).then(imgcolors => {
return {
elements:elements,
document:{
height: document.body.scrollHeight,
width:document.body.scrollWidth
},
imgcolors:imgcolors
};
});
Now I am not returing data to page.evaluate puppeteer function. So const returned is undefined.
How to do that? Thank you in advance!
I want to find average color of all images in the page. I am using Puppeteer to launch chromium and then run javascript inside browser.
Problem is that I am using Promies.all to asynchronously calculate all colors inside browser.
I want to return calculated image colors to page.evaluate. How can I do that? Is it possible?
Following code does not work, because imgcolors is no defined obviously, it is returned inside Promise.all callback.
const returned = await page.evaluate(async () => {
const fac = new FastAverageColor();
const imgs = Array.from(document.querySelectorAll('img'));
const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
Promise.all(imgpromises).then(imgcolors => { return imgcolors;});
return {
document:{
height: document.body.scrollHeight,
width:document.body.scrollWidth
},
imgcolors:imgcolors
};
});
I have tried following:
Promise.all(imgpromises).then(imgcolors => {
return {
elements:elements,
document:{
height: document.body.scrollHeight,
width:document.body.scrollWidth
},
imgcolors:imgcolors
};
});
Now I am not returing data to page.evaluate puppeteer function. So const returned is undefined.
How to do that? Thank you in advance!
Share Improve this question asked Nov 26, 2020 at 0:55 Tomas ZubrikTomas Zubrik 5176 silver badges14 bronze badges1 Answer
Reset to default 6In the page.evaluate
function, you can return a promise, which will resolve to a value. In this case Promise.all
returns a promise, which you can chain to return the value you want. It'd look something like this:
const returned = await page.evaluate(() => {
const fac = new FastAverageColor();
const imgs = Array.from(document.querySelectorAll('img'));
const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
return Promise.all(imgpromises).then(imgcolors => {
return {
document:{
height: document.body.scrollHeight,
width: document.body.scrollWidth
},
imgcolors: imgcolors
};
});
});
Alternatively, since you are using async
functions, you can just use the await
operator to wait for values to be resolved:
const returned = await page.evaluate(async () => {
const fac = new FastAverageColor();
const imgs = Array.from(document.querySelectorAll('img'));
const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
const imgcolors = await Promise.all(imgpromises);
return {
document:{
height: document.body.scrollHeight,
width: document.body.scrollWidth
},
imgcolors: imgcolors
};
});
Note that page.evaluate
can only return serializable values, i.e. things that can be JSON.stringify
'd. It looks like imgcolors
should be serializable, but in other cases, you might need to convert the values to something serializable.
本文标签:
版权声明:本文标题:node.js - How to return data from page.evaluate() in Puppeteer, when there is Promise.all() inside browser in JavaScript - Stack 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745654250a2161502.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论