Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
Trying to make a function that returns selected CSS properties but is pretty laggy

I was trying to make a function that gives you the selected CSS properties of an element those you want. But it's pretty laggy if used in console as of it needs to get and match all CSS properties.

function styleOf(elementUseSelectors, propertiesToCheck, tellInConsole) {
  var element = elementUseSelectors;
  var Arguments = propertiesToCheck;
  var calculatedProperties = [];
  var matchedProperties = [];
  if (tellInConsole !== undefined && tellInConsole == true) {
    console.warn("Running styleOf() Please Don't Do Other Calculations This Function Disables Console.")
  }
  for (var i = 0; i < Object.keys(getComputedStyle(element)).length; i++) {
    var value = getComputedStyle(element).getPropertyValue(Object.entries(getComputedStyle(element))[i][0].replace(/([A-Z])/g, ' $1').trim().replaceAll(" ", "-").toLowerCase());
    if (value !== "") {
      calculatedProperties.push(Object.entries(getComputedStyle(element))[i][0].replace(/([A-Z])/g, ' $1').trim().replaceAll(" ", "-").toLowerCase() + ": " + value);
    }
  }
  for (var i = 0; i < calculatedProperties.length; i++) {
    for (var a = 0; a < Arguments.length; a++) {
      if (calculatedProperties[i].includes(Arguments[a])) {
        window.splitted = calculatedProperties[i].split("");
        window.joinThis = [];
        for (var k = 0; k < splitted.indexOf(":"); k++) {
          joinThis.push(splitted[k]);
        };
        if (joinThis.join("") == Arguments[a]) {
          matchedProperties.push(calculatedProperties[i]);
        }
      }
    }
  }
  if (tellInConsole !== undefined && tellInConsole == true) {
    console.warn("StyleOf() Calculations Completed You Can Now Use Console.")
  }
  return matchedProperties
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The TreeWalker object is designed to quickly parse DOM nodes in a document. If you expand on the example given above in the MDN Web Docs you can output the computed CSS properties for a given node.

The first property of the method is the node you want to traverse – in this case it's document.body:

var treeWalker = document.createTreeWalker(
  document.body,
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
  false
);

var nodeList = [];
var currentNode = treeWalker.currentNode;

while(currentNode) {
  nodeList.push(currentNode);
  const style = getComputedStyle(currentNode)
  console.log(style)
  currentNode = treeWalker.nextNode();
  console.log("moving to next node...");
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Welp @kaiido answered the question.

function styleOf(element, properties) { 
const computed = getComputedStyle(element); 
return properties.map( key => key + ": "  +  computed[ key ] )};

var style = styleOf(document.getElementsByTagName("body")[0], ["height", "width", "background-color", "font-size", "color", "font-family"]);

console.log(style);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda