Skip to content

Instantly share code, notes, and snippets.

@n1k0
Created July 15, 2012 08:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save n1k0/3115959 to your computer and use it in GitHub Desktop.
Save n1k0/3115959 to your computer and use it in GitHub Desktop.
CasperJS script to retrieve keyword occurence positions within a given page
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();
});
@n1k0
Copy link
Author

n1k0 commented Jul 15, 2012

$ casperjs kw-positions.js
[
    {
        "attributes": [],
        "percentage": 87.65,
        "position": 738,
        "tag": "P",
        "text": "PhantomJS can also integrate with webservices (XML, JSONP, YQL) and with test frameworks (Jasmine, QUnit). See more examples"
    }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment