Home > Software design >  Trying to make a function that returns selected CSS properties but is pretty laggy
Trying to make a function that returns selected CSS properties but is pretty laggy

Time:09-17

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
}

CodePudding user response:

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...");
}

CodePudding user response:

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);
  • Related