Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda