Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chicken soup recipes
page = require('webpage').create()
injectjQuery = ->
# phantom allows to dynamically inject any javascript
# into page context.
# Make sure you add jquery to your script dir.
page.injectJs './jquery.js'
# ALWAYS do that when injecting jQuery into other page
page.evaluate ->
jQuery.noConflict()
doSearch = ->
page.evaluate ->
jQuery('input#search-keywords').val('chicken soup')
jQuery('form[action="/food/recipes/search"]').submit()
return true
displayRecipes = ->
page.evaluate ->
jQuery('.article.no-image .left h3 a').each (i) ->
console.log [i+1, jQuery(this).html()].join(': ')
return true
page.onLoadFinished = (status) ->
if status == 'success'
injectjQuery()
# state is undefined when we first run this script
if !phantom.state
doSearch()
# we have done the search, and will be shortly
# re-directed to results page, define state on phantom
phantom.state = 'results'
else
# state will only be defined when search has already
# been made so this load brings search results.
displayRecipes()
phantom.exit()
else
console.log('Connection failed.')
phantom.exit()
# console messages send from within page context are ingnored by default
# this puts them back where they belong.
page.onConsoleMessage = (msg) ->
console.log(msg)
page.open('http://www.bbc.co.uk/food/recipes/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment