lang/js/ MySnippets1


// jQuery-lite:
const q = (x,y=document) => y.querySelector(x)
const qq = (x,y=document) => Array.from(y.querySelectorAll(x))
// For taking a chunk of a webpage and making it the whole page.
const maxi = (x,y=document) => { let a; if( a = q(x,y) ) { document.body.innerHTML = a.innerHTML } }
const kill = (x,y=document) => qq(x,y).forEach(x => x.remove())

scraping example: get hrefs for all pdf links in a div element with class download

copy(Array.from(document.querySelectorAll("div.download a"))
  .map(x => x.getAttribute("href"))
  .filter(x => x.match(/\.pdf/))
  .join("\n"))

Assembling contents for headers

The basic idea is this:

cs = [0,0,0,0,0,0]
a = qq("h1, h2, h3, h4, h5, h6")
a.forEach(x => {
    tag = x.tagName
    lvl = tag.substr(1)|0
    for(let i=lvl;i<6;i++) {
        cs[i] = 0;
    }
    cs[lvl-1]++
    console.log(cs.join(".")+" "+x.textContent)
})

Then we can: