chatgpt.jsChatGPT 的强大客户端 JavaScript 库
chatgpt.js 是一个功能强大的 JavaScript 库,可轻松与 ChatGPT DOM 进行交互。
- 功能丰富
- 面向对象
- 易于使用
- 轻量级(但性能最优)
导入库
ES6:
(async () => {
await import('https://code.chatgptjs.org/chatgpt-latest.min.js');
// 这里是你的代码
})();
ES5:
var xhr = new XMLHttpRequest()
xhr.open('GET', 'https://code.chatgptjs.org/chatgpt-latest.min.js')
xhr.onload = function() {
if (xhr.status === 200) {
var chatgptJS = document.createElement('script')
chatgptJS.textContent = xhr.responseText
document.head.appendChild(chatgptJS)
yourCode() // 运行你的代码
}
}
xhr.send()
function yourCode() {
// 这里是你的代码
}
Greasemonkey:
笔记 使用入门模板: kudoai/chatgpt.js-greasemonkey-starter
诸如 Greasy Fork 之类的用户脚本存储库维护着预先批准的 CDN 的白名单(例如来自 cdn.jsdelivr.net 的特定于提交的引用),因此导入 URL 相当长以保持对这些站点的可发布性:
...
// @require https://cdn.jsdelivr.net/gh/kudoai/chatgpt.js@24a755998291094d0cd3b2bd395dff7c6756bbf9/dist/chatgpt-1.12.0.min.js
// ==/UserScript==
// 这里是你的代码
如果你不打算发布到这些存储库,则可以使用更简单的 https://code.chatgptjs.org/chatgpt-latest.min.js 来导入最新的缩小版本。
Chrome:
笔记 使用入门模板: kudoai/chatgpt.js-chrome-starter
由于 Google 最终将逐步淘汰 Manifest V2,远程代码将不再被允许,因此在本地导入 chatgpt.js 是理想的:
-
将 https://raw.githubusercontent.com/kudoai/chatgpt.js/main/chatgpt.js 保存到子目录 (本例中为
lib) -
将 ES6 导出语句添加到
lib/chatgpt.js的末尾
...
export { chatgpt }
- 在项目的 (V3)
manifest.json中,添加lib/chatgpt.js作为 Web 可访问资源
"web_accessible_resources": [{
"matches": ["<all_urls>"],
"resources": ["lib/chatgpt.js"]
}],
- 在需要
chatgpt.js(前景/背景相似) 的脚本中, 像这样导入它:
(async () => {
const { chatgpt } = await import(chrome.runtime.getURL('lib/chatgpt.js'));
// 这里是你的代码
})();
用法
chatgpt.js 的编写考虑到了超级灵活性。
例如:
chatgpt.getLastResponse()
chatgpt.getLastReply()
chatgpt.response.getLast()
chatgpt.get('reply', 'last')
每个调用都会同样获取最后一个回应。如果您认为它有效,那么它可能就是有效的……所以只需输入它!(谁有时间看文档?)
如果它不起作用,只需提交 issue 或 PR,它将被集成,简单易行!
评论
