Created
July 15, 2012 08:50
-
-
Save n1k0/3115959 to your computer and use it in GitHub Desktop.
CasperJS script to retrieve keyword occurence positions within a given page
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
var casper = require('casper').create(); | |
var positions = []; | |
var kw = 'JSON'; | |
casper.start('http://phantomjs.org/', function() { | |
var pageHeight = this.evaluate(function() { | |
return document.body.scrollHeight; | |
}); | |
if (pageHeight < 1) { | |
this.echo('no page height', 'ERROR').exit(); | |
} | |
positions = this.evaluate(function(kw, pageHeight) { | |
var elements = __utils__.getElementsByXPath("//*[contains(child::text(),'" + kw + "')]"); | |
return [].map.call(elements, function(element) { | |
if (element.nodeName !== "script") { | |
var position = element.getBoundingClientRect().top; | |
return { | |
position: position, | |
percentage: Math.round((position * 100 / pageHeight) * 100) / 100, | |
tag: element.nodeName, | |
text: element.innerText | |
}; | |
} | |
}); | |
}, { | |
kw: kw, pageHeight: pageHeight | |
}); | |
}); | |
casper.run(function() { | |
require('utils').dump(positions); | |
this.exit(); | |
}); |
Author
n1k0
commented
Jul 15, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment