Getting the URL of a CSS background image in NodeJS and Express: A step-by-step guide

I've recently learned that using getComputedStyle in a Node environment is not possible due to it being a browser-specific behavior.

Despite this, I am still interested in retrieving all background image URLs within Node and downloading them as shown below

const divs = document.querySelectorAll("div");
Array.from(divs).filter((div) => {
    let backgroundImage = window.getComputedStyle(div).getPropertyValue("background-image");

Unfortunately, this approach isn't feasible.

Is there a way to retrieve all div elements with background-image URLs as styles in NodeJS?

Answer №1

To retrieve the data required, consider utilizing a headless browser such as puppeteer to display the webpage. Extract all necessary information from the browser and transmit it back to Node.js. This approach should prove beneficial.

Best regards, omi

Answer №2

let allDivs = document.querySelectorAll("div");
let divBackgrounds = allDivs.map(div=>div.style.backgroundImage)
console.log(divBackgrounds)
// Result: ['url("https://www.picture.com/profile.png")','url("https://www.picture.com/profile2.png")']

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

express-jwt versus jsonwebtoken: A Comparison of Two Popular Authentication Middleware

Seeking some clarity here - can someone explain the distinction between the jsonwebtoken npm package and the express-jwt npm package? My understanding is that express-jwt is built upon jsonwebtoken and specifically validates incoming tokens while assigni ...

Why does i18next only function on http://localhost:3000/de (landing page) in NextJS and not on http://localhost:3000/de/about?

I'm new to this and feeling lost on how to tackle this issue. I tried following the instructions on https://github.com/i18next/next-i18next, but I'm struggling with index.js. When I toggle /de on my landing page, it translates correctly in the UR ...

Pictures Unavailable to Show in Table

I've been working on a project where I need to insert some images into a table. Usually, this isn't an issue for me. However, today when I added the images to the table, all I see is a square border with the alt text inside it. I'm confident ...

Positioning of content in bootstrap's navbar and dropdown menus

The issue lies in the drop-down functionality (you can check out the problematic html code on this codepen, originally inspired by this source). <header class="pb-3"> <nav class="navbar navbar-expand-md navbar-light bg-white borde ...

Leverage JSON files for pagination in NextJS

I am currently developing a science website where the post URLs are stored in a static JSON file. ScienceTopics.json- [ { "Subject": "Mathematics", "chapters": "mathematics", "contentList": [ ...

How to remove previous and next buttons in product carousel using Bootstrap 3.3.5

Currently, I am tackling a small project and venturing into the world of bootstrap for the first time. I have hit a roadblock when it comes to disabling (not hiding) product carousel controls when the active state is on the item:first & item:last classes. ...

Encountered a problem while attempting to add the react-redux dependency

I've encountered an issue while trying to install the react-redux package on my create-react-app project. Despite attempting various solutions like deleting and reinstalling the node_modules folder, as well as installing with admin permissions, I am c ...

jquery mobile listview extension not functioning as expected

My JQM menu is displayed as a listview, and I want it to be normal on every page except one where it should have 2 extra items. Despite searching online for solutions, nothing seems to work. Here are some of the things I've attempted: -location.reloa ...

Can you clarify the functionality of this loop? I am having trouble grasping how it produces the final result

Seeking clarification on the highlighted section] https://i.sstatic.net/dyrKS.png I need assistance in understanding how the use of "text" helps to print the following literal. ...

Tips for utilizing maps in a react component within a Next.js application

I received an array of data from the backend that I need to display on a React component. home.js import Head from "next/head"; import Header from "../src/components/Header"; import * as React from 'react'; import { styled } ...

Fill in a data table beginning with the column next to the first

My issue involves a datatable retrieving JSON data from an API. The table is configured so that the first column should only display a checkbox. However, upon data retrieval, the first column gets populated as well. https://i.sstatic.net/izb5B.png $.getJ ...

Having issues with the jQuery toggle functionality

var resultsList = $("#test"); resultsList.text("Hello. This is jQuery!"); var tB = jQuery("#toggleButton"); tB.on("click", function() { resultsList.toggle(400); }); The syntax appears to be correct as there are no errors reported in the browser cons ...

I developed a straightforward MVC application that sends an HttpGet request and delivers an array containing 7 randomly generated, unidentified numbers to the View. [RESOLVED

A new MVC app was launched with a simple task of generating 7 random numbers between 0 and 9 to be placed at positions 1 through 7 (without starting with 0). Initially, the numbers are hidden with "X" marks. When the user clicks on the "Submit" button and ...

What is the best way to manage errors on my server to ensure it remains stable and never crashes?

Consider this server route example using expressjs: app.get('/cards', function(req, res) { anUndefinedVariable // Server doesn't crash dbClient.query('select * from cards', function(err, result) { anUndefinedVariab ...

How to stop Accordion from automatically collapsing when clicking on AccordionDetails in Material-UI

I am working on a React web project with two identical menus. To achieve this, I created a component for the menu and rendered it twice in the App component. For the menu design, I opted to use the Material UI Accordion. However, I encountered an issue wh ...

Unable to keep button contained within form's card design

Currently enhancing my Bootstrap skills and seeking assistance with a card and form alignment issue. Struggling to properly place a form within a card. Any suggestions or insights on resolving this? <div class="row row-content col-sm-8"> &l ...

angularjs code to dynamically change the selected index of an option in a document

This code snippet demonstrates how to achieve this functionality using pure JavaScript: document.getElementById("mySelect").selectedIndex = "0" <select class="selectpicker" id="mySelect"> <option>English &nbsp;</option> < ...

Exploring HTML list with padding to determine optimal wrapping configurations

Having an issue with a section of links. The blocks for the links are lining up next to each other when the viewport is wide, but I want one block labeled MOBOT to drop down onto a new line in a small responsive space. The goal is for the MOBOT square to ...

Ensure that buttons are cropped when extending beyond the border of another DIV

My issue involves a main DIV with buttons contained within it. I am seeking a solution to ensure that the buttons are cut off at the edge of the DIV, similar to the concept illustrated in this image. In the image, the blue represents the body, the light gr ...

Live calculation feature for dropdown menus

I have a form where I need to calculate the total result after submission. The package and event ID should be added together, and the guest field should always be calculated (guest value * 5). The final result should be displayed in <div id="result"> ...