Skip to content

Instantly share code, notes, and snippets.

@gwing33
Created October 12, 2012 23:14
Show Gist options
  • Select an option

  • Save gwing33/3882163 to your computer and use it in GitHub Desktop.

Select an option

Save gwing33/3882163 to your computer and use it in GitHub Desktop.

Revisions

  1. gwing33 created this gist Oct 12, 2012.
    1 change: 1 addition & 0 deletions cli
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    casperjs google_search_location.js pizza 'phoenix, az'
    54 changes: 54 additions & 0 deletions google_search_location.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    var casper = require('casper').create({
    clientScripts: [ 'includes/jquery.min.js' ],
    pageSettings: { userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'}
    });

    function getLinks() {
    var links = document.querySelectorAll('h3.r a');
    return Array.prototype.map.call(links, function(e) {
    return e.getAttribute('href');
    });
    }

    var search_arg = casper.cli.args[0];
    var location_arg = casper.cli.args[1];

    casper.on('remote.message', function(msg) {
    this.echo('Browser: ' + msg);
    });

    casper.start('http://google.com/', function() {
    this.fill('form[action="/search"]', { q: search_arg }, true);
    });

    casper.then(function() {

    console.log("Changing Location");

    this.evaluate(function(loc) {
    var my_loc = $('.tbos');
    console.log('My Location - ' + my_loc.text());

    console.log('New Destination - ' + loc);
    var form = document.getElementById('change_location_form');
    var input = document.getElementById('lc-input');

    input.value = loc;
    google.x(form,function(){ google.loc.submit(); });

    setTimeout( function() {
    var loc = $('.tbos');
    console.log('Current Location - ' + loc.text());

    var links = $('h3.r a');
    var my_links = Array.prototype.map.call(links, function(e) {
    return e.getAttribute('href');
    });

    console.log(my_links.length + ' links found:');
    console.log(' - ' + my_links.join('\n - '));
    }, 5000);
    }, { loc: location_arg });
    });

    casper.run(function() {});