Created
December 26, 2017 05:15
-
-
Save denvazh/a53c1c135e872321c7903603205276a8 to your computer and use it in GitHub Desktop.
Extract screenshots from recorded profile in Google Chrome Dev tools
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
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'json' | |
require 'base64' | |
require 'pathname' | |
module Screenshot | |
class Snapshot | |
attr_reader :name, :ts, :blob, :format | |
def initialize(name, ts, blob) | |
@name = name | |
@ts = ts | |
@blob = blob | |
@format = 'jpeg' | |
end | |
def save!(directory) | |
filename = Pathname.new(directory).join("#{name}-#{ts}.#{format}") | |
File.write(filename, Base64.decode64(blob)) | |
end | |
end | |
def self.parse(frame) | |
Snapshot.new(frame['name'], frame['ts'], frame['args']['snapshot']) | |
end | |
end | |
options = {} | |
opt_parser = OptionParser.new do |opts| | |
opts.banner = 'Usage: extract_screenshots [options]' | |
options[:file] = nil | |
opts.on('-f', '--file FILE', String, 'File with recorder profiling data') do |f| | |
options[:file] = f | |
end | |
opts.on('-h', '--help', 'Show help') do |h| | |
options[:help] = h | |
puts opt_parser | |
exit | |
end | |
end | |
begin | |
opt_parser.parse!(ARGV) | |
file = options.fetch(:file) | |
# might take a while if file is very big | |
frames = JSON.parse(File.read(file)).select { |r| r['name'] == 'Screenshot' } | |
frames.each do |frame| | |
snapshot = Screenshot.parse(frame) | |
snapshot.save!(Dir.pwd) # save all files to current directory | |
end | |
rescue OptionParser::InvalidOption, OptionParser::MissingArgument | |
puts $!.to_s | |
puts opt_parser | |
exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment