node-horseman
node-horseman copied to clipboard
feat: Add function to randmoly click on any link
Proposal for issue #300 .
Add a function to click on a random anchor link.
I am open to others' thoughts, but I feel this feature is unnecessary. Also, I feel the specific types of links you are filtering out would vary from use to use.
I doubt it would be used often, and it can somewhat easily be done with existing functionality:
var HORSEMAN_CLICK_ATRR = 'HORSEMAN_SELECTED_RANDOM_LINK';
horseman
.evaluate(function pickALink(attr) {
// Find all the links
var links = document.getElementsByTagName('a');
// Select specific kinds of links
var goodLinks = Array.prototype.filter.call(links, function checkLink(l) {
// Check whatever link stuff you like
return foo(l);
});
// Pick a random link
var link = goodLinks[Math.floor(Math.random()*goodLinks.length)];
// Mark the link
link.setAttribute(attr, '');
}, HORSEMAN_CLICK_ATTR)
.click('[' + HORSEMAN_CLICK_ATTR + ']') // Click the marked link