Skip to content

Instantly share code, notes, and snippets.

@genki
Created December 5, 2022 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genki/85c1f6f3c57c3e20daa27408a10d9740 to your computer and use it in GitHub Desktop.
Save genki/85c1f6f3c57c3e20daa27408a10d9740 to your computer and use it in GitHub Desktop.
Talk with ChatGPT from CLI.
#!/usr/bin/ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'shellwords'
end
CHATGPT_URI = "https://chat.openai.com/chat"
def get_page_info
`chrome-cli info`.split("\n").map{|line| line.split(": ")}.to_h
end
def exec_js(js)
js = Shellwords.escape(js)
`chrome-cli execute #{js}`
end
info = get_page_info
unless info["Url"].start_with?(CHATGPT_URI)
puts "Please open #{CHATGPT_URI} in Chrome."
exit
end
trap("INT") { exit }
loop do
puts "Input text: until empty line"
text = ""
while line = gets
line.chomp!
break if line.empty?
text += line + " "
end
exec_js <<-JS
document.querySelector("form textarea").value = "#{text}";
document.querySelector("form button").click();
JS
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment