casperjs 1.45 KB
#!/usr/bin/env ruby
# Ruby Wrapper for CasperJs
# by hannyu

def resolve(file_path)
    while File.symlink?(file_path) do
        file_path = File.readlink(file_path)
    end
    file_path
end

CASPER_PATH = File.dirname(File.dirname(File.expand_path(resolve(__FILE__))))

PHANTOMJS_NATIVE_ARGS = [
    '--cookies-file',
    '--config',
    '--debug',
    '--disk-cache',
    '--ignore-ssl-errors',
    '--load-images',
    '--load-plugins',
    '--local-storage-path',
    '--local-storage-quota',
    '--local-to-remote-url-access',
    '--max-disk-cache-size',
    '--output-encoding',
    '--proxy',
    '--proxy-auth',
    '--proxy-type',
    '--remote-debugger-port',
    '--remote-debugger-autorun',
    '--script-encoding',
    '--ssl-protocol',
    '--web-security',
]

CASPER_ARGS = []
PHANTOMJS_ARGS = []
ARGV.each do |arg|
    is_found = false
    PHANTOMJS_NATIVE_ARGS.each do |pna|
        if arg.start_with? pna
            PHANTOMJS_ARGS << arg
            is_found = true
            break
        end
    end
    CASPER_ARGS << arg if not is_found
end

CASPER_COMMAND = []
CASPER_COMMAND << (ENV["PHANTOMJS_EXECUTABLE"] || "phantomjs")
CASPER_COMMAND.concat PHANTOMJS_ARGS
CASPER_COMMAND << File.join(CASPER_PATH, "bin", "bootstrap.js")
CASPER_COMMAND << "--casper-path=#{CASPER_PATH}"
CASPER_COMMAND << "--cli"
CASPER_COMMAND.concat CASPER_ARGS

if system(CASPER_COMMAND.join(" ")).nil?
    puts "Fatal: Did you install phantomjs?"
end

exit $?.exitstatus || 1