Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*DataScrapper by prettydude v1.0.*/
- /*
- elements - list of elements to find (css or html form)
- all - return all found entries or first
- */
- function find(elements, all) {
- let output = {};
- //split lines
- elements = elements.split("\n");
- elements.forEach(function(element) {
- //init variables, split key and selector
- let key = element.split(":")[0],
- selector = element.slice(key.length + 1).trim(),
- selected,
- out = [];
- //make css selector from element
- if (selector.startsWith("<")) {
- let div = document.createElement('div');
- div.innerHTML = selector.trim();
- let element = div.firstChild;
- //if class not provided - don't include it
- let classes = element.className ? "."+element.className.replace(" ", ".") : "";
- //if element has unique id - use only it
- selector = element.id ? "#" + element.id : element.tagName.toLowerCase() + classes;
- }
- //select elements with css selector
- selected = document.querySelectorAll(selector);
- //ES6 way to do loops
- for (let elem of selected) {
- //get element text
- out.push(elem.textContent);
- //return only first found value, not all
- if (!all) break;
- }
- output[key.trim()] = out;
- });
- //return to body
- document.body.innerHTML = JSON.stringify(output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement