For the sake of me i can't get the Filter (.filter(function(d,i){return d})) to eliminate "undefined" or "0" working on this array.
The script runs within a Applescript applet and should return all resulting urls as string.
var x = Array.prototype.slice.call(document.querySelectorAll(".product_card"))
.map(function(d,i){
var title = d.querySelector(".product_card__title"),
link = d.querySelector("a");
console.log(title);
console.log(link);
if(title && link && /Rocker/gi.test(title.textContent)){
return link.href
}
})
document.getElementById("result").textContent = JSON.stringify(x);
<div class="product_card powersearch__product_card">
<a href="/shop/XYZ" class="js-search-product-link">
<div class="product_card__image" style="background-image:url(https://image.jpg);"></div>
<div class="product_card__title">some rocker</div>
<div class="product_card__meta">€14</div></a></div>
<br>
<div class="product_card powersearch__product_card">
<a href="/shop/ZXY" class="js-search-product-link">
<div class="product_card__image" style="background-image:url(https://image.jpg);"></div>
<div class="product_card__title">returns undefined</div>
<div class="product_card__meta">€14</div></a></div>
<br>
<div id="result">
See Question&Answers more detail:os