Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions lib/spunkmeyer/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,44 @@ def self.cookie_path
when :osx
"#{Dir.home}/Library/Application Support/Google/Chrome/Default/Cookies"
when :linux
"#{Dir.home}/.config/google-chrome/Default/Cookies"
case browser
when :chrome
"#{Dir.home}/.config/google-chrome/Default/Cookies"
when :firefox
Dir.glob("#{Dir.home}/.mozilla/firefox/*.default/cookies.sqlite").first
end
else
raise 'Spunkmeyer::Chrome doesn\'t know this operating system.'
end
end

def self.browser
case Spunkmeyer.os
when :osx
:chrome
when :linux
if File.exists?("#{Dir.home}/.config/google-chrome/Default/Cookies")
:chrome
elsif File.exists?(Dir.glob("#{Dir.home}/.mozilla/firefox/*.default/cookies.sqlite").first)
:firefox
end
end
end

def self.cookies(domain)
hk = host_key domain
db = SQLite3::Database.open cookie_path
fields = [ :host_key, :path, :name, :value, :expires_utc ]
sql_fields = fields.map { |f| f.to_s }.join(', ')
rows = db.execute "select #{sql_fields} from cookies where host_key like '%#{hk}%'"
case browser
when :firefox
fields = [ :host, :path, :name, :value, :expiry ]
sql_fields = fields.map { |f| f.to_s }.join(', ')
rows = db.execute "select #{sql_fields} from moz_cookies where host like '%#{hk}%'"
fields = [ :host_key, :path, :name, :value, :expires_utc ]
when :chrome
fields = [ :host_key, :path, :name, :value, :expires_utc ]
sql_fields = fields.map { |f| f.to_s }.join(', ')
rows = db.execute "select #{sql_fields} from cookies where host_key like '%#{hk}%'"
end
rows.map! { |r| Hash[fields.zip r] }
names = rows.map { |r| r[:name] }
Hash[names.zip rows]
Expand Down