Created
December 5, 2022 06:38
-
-
Save genki/85c1f6f3c57c3e20daa27408a10d9740 to your computer and use it in GitHub Desktop.
Talk with ChatGPT from CLI.
This file contains 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/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