diff --git a/sample-functions/Phantomjs/Dockerfile b/sample-functions/Phantomjs/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..1320cc98129f2e198915d254dbbd11526f87e221 --- /dev/null +++ b/sample-functions/Phantomjs/Dockerfile @@ -0,0 +1,9 @@ +FROM alexellis2/phantomjs-docker:latest + +ADD https://github.com/alexellis/faas/releases/download/0.5.6-alpha/fwatchdog /usr/bin +RUN chmod +x /usr/bin/fwatchdog + +ENV fprocess="phantomjs /dev/stdin" + +HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1 +CMD ["fwatchdog"] diff --git a/sample-functions/Phantomjs/README.md b/sample-functions/Phantomjs/README.md new file mode 100644 index 0000000000000000000000000000000000000000..6f7a97677d0d5ceaafe5f402de0d823a3ac0e051 --- /dev/null +++ b/sample-functions/Phantomjs/README.md @@ -0,0 +1,21 @@ +### Phantomjs function + +[Phantomjs](http://phantomjs.org) is a headless web-browser used for scraping and automation testing. This function will scrape a web-page with the JavaScript sent in through the function. + +Example usage: + +``` +$ time curl --data-binary @cnn.js http://localhost:8080/function/phantomjs + +Status: success +CNN - Breaking News, Latest News and Videos + +real 0m8.729s +``` + +This script visits the front page of CNN and once it is fully loaded - scrapes the title. + +See [cnn.js](https://github.com/alexellis/faas/tree/master/sample-functions/Phantomjs/cnn.js) as an example of a Phantomjs script. + +Another example script [feedly_subscribers.js](https://github.com/alexellis/faas/tree/master/sample-functions/Phantomjs/feedly_subscribers.js) gives the count of subscribers for an RSS Feed registered on Feedly. + diff --git a/sample-functions/Phantomjs/cnn.js b/sample-functions/Phantomjs/cnn.js new file mode 100644 index 0000000000000000000000000000000000000000..83202e41457fa80f4e09fe53e1bf5b192a008faa --- /dev/null +++ b/sample-functions/Phantomjs/cnn.js @@ -0,0 +1,10 @@ +var webPage = require('webpage'); +var page = webPage.create(); + +page.open('https://www.cnn.com/', function(status) { + console.log('Status: ' + status); + console.log(page.title); + // Do other things here... + phantom.exit(); + +}); diff --git a/sample-functions/Phantomjs/feedly_subscribers.js b/sample-functions/Phantomjs/feedly_subscribers.js new file mode 100644 index 0000000000000000000000000000000000000000..3f9218676ddbc523404e4b8591e9e2923aa8f153 --- /dev/null +++ b/sample-functions/Phantomjs/feedly_subscribers.js @@ -0,0 +1,13 @@ +var webPage = require('webpage'); +var page = webPage.create(); + +var feed = "http://blog.alexellis.io/rss/"; +var url = "https://feedly.com/i/subscription/feed/" + feed; + +page.open(url, function(status) { + var title = page.evaluate(function(s) { + return document.querySelector(s).innerText; + }, ".count-followers"); + console.log(title.split(" ")[0]); + phantom.exit(); +});