Dup Ver Goto 📝

CopyAllMatchingHrefs

PT2/lang/js/user-scripts/examples does not exist
To
28 lines, 131 words, 948 chars Page 'CopyAllMatchingHrefs' does not exist.

This function takes a selector or element, and a predicate, and returns an array of the hrefs of all a elements in the given element matching the predicate. Useful for manual scraping.

window.q = (x,y=document) => y.querySelector(x)
window.qq = (x,y=document) => Array.from(y.querySelectorAll(x))

function get_matching(elt,pred = x => true) {
    if( typeof elt == "string" ) elt = q(elt)
    return qq("a",elt).filter(x => pred(x.href)).map(x => x.href)
}

You could of course make this more general, and make the above a special case:

window.q = (x,y=document) => y.querySelector(x)
window.qq = (x,y=document) => Array.from(y.querySelectorAll(x))

function get_filter_map(elt,sel,pred = x => true,trans = x => x) {
    if( typeof elt == "string" ) elt = q(elt)
    return qq(sel,elt).filter(pred).map(trans)
}
function get_matching(elt,pred = x => true) {
    return get_filter_map(elt,"a",x => pred(x.href),x => x.href)
}