Created
October 7, 2011 11:23
-
-
Save marcinb/1270094 to your computer and use it in GitHub Desktop.
Chicken soup recipes
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
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