Browse files

Initial commit

  • Loading branch information...
0 parents commit ae2ca4d7cbee3e8996db6a0d9dc02ef69794fa71 superbrothers committed Jun 17, 2012
Showing with 223 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +22 −0 LICENSE.txt
  3. +11 −0 Makefile
  4. +34 −0 README.md
  5. +75 −0 capture.js
  6. +58 −0 opts.json
  7. +21 −0 package.json
2 .gitignore
@@ -0,0 +1,2 @@
+node_modules
+.DS_Store
22 LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Kazuki Suda <kazuki.suda@gmail.com>
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 Makefile
@@ -0,0 +1,11 @@
+NAME=`node -pe 'require("./package.json").name'`
+VERSION=`node -pe 'require("./package.json").version'`
+
+jshint: capture.js
+ jshint $^
+clean:
+ rm -rf node_modules
+ rm -f *.png *.jpg *.gif *.tgz
+tar: clean
+ tar czf $(NAME)-$(VERSION).tgz .
+.PHONY: jshint clean tar
34 README.md
@@ -0,0 +1,34 @@
+# Capturejs
+
+## Installation
+
+```
+$ brew install phantomjs
+$ npm install -g capturejs
+```
+
+## Usage
+
+```
+Usage: capturejs [options]
+URI
+ -u, --uri <value> (required)
+Output Image File
+ -o, --output <value> (required)
+CSS Selector
+ -s, --selector <value>
+UserAgent
+ -A, --user-agent <value>
+Inject external script code on Web page
+ -J, --javascript-file <value>
+ViewPortSize {width}x{height}
+ -V, --viewportsize <value>
+Cookies File
+ -c, --cookies-file <value>
+HTTP Timeout (ms)
+ -T, --timeout <value>
+```
+
+## Copyright
+
+Copyright (c) 2012 Kazuki Suda. See LICENSE.txt for further details.
75 capture.js
@@ -0,0 +1,75 @@
+#! /usr/bin/env node
+/*jshint node: true, browser: true, indent: 2, laxcomma: true */
+/*vim: set fenc=utf-8 ts=2 sts=2 sw=2 : */
+"use strict";
+
+var opts = require("opts")
+ , fs = require("fs")
+ , phantom = require("phantom")
+ ;
+
+opts.parse(require("./opts.json"));
+
+// phantomjs Command-line Options
+var args = [];
+["cookies-files"].forEach(function (key) {
+ if (opts.get(key)) args.push(["--", key, "=", opts.get("cookies-file")].join(""));
+});
+phantom.create.apply(phantom, (function () {
+ args.push(function (ph) {
+ ph.createPage(function (page) {
+ // viewportSize
+ if (opts.get("viewportsize")) {
+ var matches = opts.get("viewportsize").match(/^(\d+)x(\d+)$/);
+ if (matches !== null) {
+ page.set("viewportSize", {width: matches[1], height: matches[2]});
+ }
+ }
+ // UserAgent
+ if (opts.get("useragent")) {
+ page.set("settings.userAgent", opts.get("useragent"));
+ }
+ // HTTP Timeout
+ if (opts.get("timeout")) {
+ var timer = setTimeout(function () {
+ console.error("Error: " + opts.get("uri") + " is timeout");
+ ph.exit();
+ process.exit(1);
+ }, +opts.get("timeout"));
+ }
+ page.open(opts.get("uri"), function (status) {
+ if (status === "fail") {
+ console.error("Error: " + opts.get("uri") + "failed");
+ ph.exit();
+ process.exit(1);
+ }
+ if (typeof timer !== "undefined") clearTimeout(timer);
+
+ // inject external script code
+ if (opts.get("javascript-file")) {
+ page.injectJs(opts.get("javascript-file"));
+ }
+ /**
+ * pass parameters into the webpage function.
+ *
+ * http://code.google.com/p/phantomjs/issues/detail?id=132
+ * from comment #43.
+ */
+ page.evaluate((function () {
+ var func = function (selector) {
+ document.body.bgColor = "white";
+ if (typeof selector === "undefined") return null;
+ var elem = document.querySelector(selector);
+ return (elem !== null) ? elem.getBoundingClientRect() : null;
+ };
+ return "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify([opts.get("selector")]) + ");}";
+ }()), function (rect) {
+ if (rect !== null) page.set("clipRect", rect);
+ page.render(opts.get("output"));
+ ph.exit();
+ });
+ });
+ });
+ });
+ return args;
+}()));
58 opts.json
@@ -0,0 +1,58 @@
+[
+ {
+ "short": "T",
+ "long": "timeout",
+ "description": "HTTP Timeout (ms)",
+ "value": true,
+ "required": false
+ },
+ {
+ "short": "c",
+ "long": "cookies-file",
+ "description": "Cookies File",
+ "value": true,
+ "required": false
+ },
+ {
+ "short": "V",
+ "long": "viewportsize",
+ "description": "ViewPortSize {width}x{height}",
+ "value": true,
+ "required": false
+ },
+ {
+ "short": "J",
+ "long": "javascript-file",
+ "description": "Inject external script code on Web page",
+ "value": true,
+ "required": false
+ },
+ {
+ "short": "A",
+ "long": "user-agent",
+ "description": "UserAgent",
+ "value": true,
+ "required": false
+ },
+ {
+ "short": "s",
+ "long": "selector",
+ "description": "CSS Selector",
+ "value": true,
+ "required": false
+ },
+ {
+ "short": "o",
+ "long": "output",
+ "description": "Output Image File",
+ "value": true,
+ "required": true
+ },
+ {
+ "short": "u",
+ "long": "uri",
+ "description": "URI",
+ "value": true,
+ "required": true
+ }
+]
21 package.json
@@ -0,0 +1,21 @@
+{
+ "author": "Kazuki Suda <kazuki.suda@gmail.com>",
+ "name": "capturejs",
+ "version": "0.0.1",
+ "repository": {
+ "url": "git://github.com/superbrothers/capturejs.git"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "dependencies": {
+ "phantom": "*",
+ "opts": "*"
+ },
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "bin": {"capturejs": "./capture.js"},
+ "engines": {
+ "node": "*"
+ }
+}

0 comments on commit ae2ca4d

Please sign in to comment.