Created
May 31, 2013 12:52
-
-
Save davidbgk/5684767 to your computer and use it in GitHub Desktop.
Formation CasperJS avec @n1k0 chez SFEIR le 31 mai 2013 pour scopyleft.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Returns the label of empty empty (or #) links in a web page | |
given a selector id (passed via the first CLI arg). | |
*/ | |
var selector = "body", | |
links = [], | |
casper = require('casper').create(); | |
/* The first argument of the CLI is the HTML id you want to target */ | |
if (casper.cli.has(0)) { | |
selector = "#" + casper.cli.get(0); | |
} | |
function getEmptyLinks(selector) { | |
var links = document.querySelectorAll(selector + ' a'); | |
return Array.prototype.filter.call(links, function(e) { | |
var href = e.getAttribute('href'); | |
return href === "#" || href === ""; | |
}).map(function(e) { | |
return e.textContent.trim(); | |
}); | |
} | |
casper.start('http://127.0.0.1:8000/'); | |
casper.then(function() { | |
links = this.evaluate(getEmptyLinks, selector); | |
}); | |
casper.run(function() { | |
this.echo(links.length + ' empty links found in the #' + selector + ' selector:'); | |
this.echo(links.join('\n')); | |
this.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
C'est du détail, mais la fonction
getEmptyLinks
peut être simplifiée pour éviter d'itérer sur tous les liens :edit : merci pour le partage :)