Skip to content

Instantly share code, notes, and snippets.

@m-kirstetter
Forked from n1k0/casper-google-suggest.md
Last active August 29, 2015 14:23
Show Gist options
  • Save m-kirstetter/83bcec300016f57cda04 to your computer and use it in GitHub Desktop.
Save m-kirstetter/83bcec300016f57cda04 to your computer and use it in GitHub Desktop.

CasperJS Google Suggest

The script:

/*global casper:true*/
var casper = require('casper').create({
    pageSettings: {
        userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:31.0) Gecko/20100101 Firefox/31.0"
    }
});
var suggestions = [];
var word = casper.cli.get(0);

if (!word) {
    casper.echo('please provide a word').exit(1);
}

casper.start('http://www.google.com/', function() {
    this.sendKeys('input[name=q]', word);
});

casper.waitFor(function() {
  return this.fetchText('.gsq_a table span').indexOf(word) === 0
}, function() {
  suggestions = this.evaluate(function() {
      var nodes = document.querySelectorAll('.gsq_a table span');
      return [].map.call(nodes, function(node){
          return node.textContent;
      });
  });
});

casper.run(function() {
  this.echo(suggestions.join('\n')).exit();
});

Usage:

$ casperjs google-suggest.js why
whyd
why not
why not productions
why paris
why don't you do right
why
why not sushi
why why love
why so serious
whisky

Works as well with sentences actually:

$ casperjs google-suggest.js "why do unicorns"
why do unicorns have horns
why do unicorns exist
why do unicorns drink
why do unicorns poop rainbows
why do unicorns not exist
why do unicorns fart rainbows
why do unicorns like virgins
why do unicorns fly
why do unicorns have wings
why do unicorns eat

Profit.

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