I came to know we cannot use getComputedStyle in Node environment because that's a browser behavior.
However, I wanted to get all the background image urls in Node and download them like so
const divs = document.querySelectorAll("div");
Array.from(divs).filter((div) => {
let backgroundImage = window.getComputedStyle(div).getPropertyValue("background-image");
But I cannot do that.
How to get all the divs having background-image url as styles in NodeJS?
CodePudding user response:
Use a headless browser like puppeteer to render the webpage. Extract all the information you need from the browser and send it back to Node.js. I think this will help.
Regards, omi
CodePudding user response:
const divs = document.querySelectorAll("div");
const backgroundImages = divs.map(div=>div.style.backgroundImage)
console.log(backgroundImages)
// Output: ['url("https://www.image.com/profil.png")','url("https://www.image.com/profil2.png")']