Advertisement
Cool_boy21

DataScrapper

Jan 16th, 2019
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*DataScrapper by prettydude v1.0.*/
  2. /*
  3.   elements - list of elements to find (css or html form)
  4.   all - return all found entries or first
  5. */
  6. function find(elements, all) {
  7.   let output = {};
  8.   //split lines
  9.   elements = elements.split("\n");
  10.  
  11.   elements.forEach(function(element) {
  12.     //init variables, split key and selector
  13.     let key = element.split(":")[0],
  14.       selector = element.slice(key.length + 1).trim(),
  15.       selected,
  16.       out = [];
  17.     //make css selector from element
  18.     if (selector.startsWith("<")) {
  19.       let div = document.createElement('div');
  20.       div.innerHTML = selector.trim();
  21.       let element = div.firstChild;
  22.       //if class not provided - don't include it
  23.       let classes = element.className ? "."+element.className.replace(" ", ".") : "";
  24.       //if element has unique id - use only it
  25.       selector = element.id ? "#" + element.id : element.tagName.toLowerCase() + classes;
  26.     }
  27.     //select elements with css selector
  28.     selected = document.querySelectorAll(selector);
  29.     //ES6 way to do loops
  30.     for (let elem of selected) {
  31.       //get element text
  32.       out.push(elem.textContent);
  33.       //return only first found value, not all
  34.       if (!all) break;
  35.     }
  36.     output[key.trim()] = out;
  37.   });
  38.   //return to body
  39.   document.body.innerHTML = JSON.stringify(output);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement