diff --git a/lib/spunkmeyer/chrome.rb b/lib/spunkmeyer/chrome.rb index aa23495..f345622 100644 --- a/lib/spunkmeyer/chrome.rb +++ b/lib/spunkmeyer/chrome.rb @@ -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]