JavaScript - Visible Elements ?

Posted at

I was looking for only the visible links in sourceforge.net,
in order to rewrite them to direct-download instead of their redirect page,

there is simple heuristics, to extract just the *right* <a> elements,
but few are theme are (although "real"..) hidden, meaning there is no use to run the javascript rewrite-algorithm on those, so we should save the overhead..

..but how do we know if the link is visible (for example, a "download button"),
or a "shadow one" (minor size, or simply not user-accessible)?

2016-01-21_215557

well, it is quite simple:

(function(elements, style, is_in){
is_in = function(haystack, needle){
haystack = haystack.toLowerCase();
needle = needle.toLowerCase();

return -1 !== haystack.indexOf(needle);
};

elements = Array.prototype.filter.call(elements,function(element){
style = getComputedStyle(element);

return false === is_in(style.display, "none") &&
false === is_in(style.visibility, "hidden") &&
"0" !== style.width.replace(/[^\\d]*/g,'') &&
"0" !== style.height.replace(/[^\\d]*/g,'')
});

return elements;
}(
document.querySelectorAll('a[href*="/projects/"][href*="/files/"][href*="/download"]')
, null
, null
));