Skip to content

Latest commit

 

History

History
1833 lines (1245 loc) · 39.4 KB

USERGUIDE.md

File metadata and controls

1833 lines (1245 loc) · 39.4 KB

chatgpt.js is a powerful JavaScript library that allows for super easy interaction w/ the ChatGPT DOM.

Table of contents

Importing the library

Note To always import the latest version (not recommended in production!) replace the versioned jsDelivr URL with: https://cdn.jsdelivr.net/npm/@kudoai/chatgpt.js/chatgpt.min.js

ES6

(async () => {
    await import('https://cdn.jsdelivr.net/npm/@kudoai/chatgpt.js@2.7.1/dist/chatgpt.min.js');
    // Your code here...
})();

ES5

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://cdn.jsdelivr.net/npm/@kudoai/chatgpt.js@2.7.1/dist/chatgpt.min.js');
xhr.onload = function () {
    if (xhr.status === 200) {
        var chatgptJS = document.createElement('script');
        chatgptJS.textContent = xhr.responseText;
        document.head.append(chatgptJS);
        yourCode(); // runs your code
    }
};
xhr.send();

function yourCode() {
    // Your code here...
}

Greasemonkey

Note To use a starter template: kudoai/chatgpt.js-greasemonkey-starter

...
// @require https://cdn.jsdelivr.net/npm/@kudoai/chatgpt.js@2.7.1/dist/chatgpt.min.js
// ==/UserScript==

// Your code here...

Chrome

Note To use a starter template: kudoai/chatgpt.js-chrome-starter

  1. Save https://raw.githubusercontent.com/KudoAI/chatgpt.js/main/chatgpt.js to a subdirectory (lib in this example)

  2. Add ES6 export statement to end of lib/chatgpt.js

...
export { chatgpt }
  1. In project's (V3) manifest.json, add lib/chatgpt.js as a web accessible resource
    "web_accessible_resources": [{
        "matches": ["<all_urls>"],
        "resources": ["lib/chatgpt.js"]
    }],
  1. In scripts that need chatgpt.js (foreground/background alike), import it like so:
(async () => {
    const { chatgpt } = await import(chrome.runtime.getURL('lib/chatgpt.js'));
    // Your code here...
})();

Library methods

Unless noted otherwise, methods are synchronous: they wait for the operation to finish, instead of returning immediately. If you need to know the result of calling asynchronous methods, use the returned promise or pass a callback function into the method.

General

detectLanguage async

Asks ChatGPT to detect the language of given text.

Parameters:

text: A string being the text to detect the language of.

Example code:

(async () => {
    const language = await chatgpt.detectLanguage('我是一個大男孩');
    console.log(language);
    /* Logs:
    Chinese (Traditional) */
})();

executeCode async

Asks ChatGPT to execute the given code.

Parameters:

code: A string being the code to execute.

Example code:

(async () => {
    console.log(await chatgpt.executeCode('return 6 + 5')); // logs '11'
})();

generateRandomIP

Returns a random IP address as a string.

Example code:

const randomIP = chatgpt.generateRandomIP();
console.log(randomIP); // Example output: '161.192.110.125'

get

Example code:

var response;

response = chatgpt.get('reply', 'last');
// Equivalent of
response = chatgpt.getLastResponse();

getUserLanguage

Returns the user language as a string.

Example code:

const userLanguage = chatgpt.getUserLanguage();
console.log(userLanguage); // Example output: 'en-US'

isChromium

Returns a boolean value. true if the browser is Chromium and false otherwise.

Example code:

if (chatgpt.isChromium()) {
    // Do something
}

isFirefox

Returns a boolean value. true if the browser is Firefox and false otherwise.

Example code:

if (chatgpt.isFirefox()) {
    // Do something
}

isFullScreen

Returns a boolean value. true if the website is fullscreen and false otherwise.

Example code:

if (chatgpt.isFullScreen()) {
    // Do something
}

isLoaded async

Resolves a promise when ChatGPT has finished loading.

Example code:

(async () => {
    await chatgpt.isLoaded();
    console.log('ChatGPT has finished loading.');
})();

isMobileDevice

Returns a boolean value. true if the user device is mobile and false otherwise.

Example code:

if (chatgpt.isMobileDevice()) {
    // Do something
}

printAllFunctions

Prints all the library functions to the console.

Example code:

chatgpt.printAllFunctions();

randomFloat

Returns a random, cryptographically secure float number between 0 (inclusive) and 1 (exclusive).

Example code:

const randomNumber = chatgpt.randomFloat();
console.log(randomNumber); // Example output: 0.9472113021060851

renderHTML

Cleans and renders given HTML code.

Parameters:

node: A string representing the HTML to be rendered.

Example code:

document.body.append(
    chatgpt.renderHTML('<div>Hello World!</div>');
);

sentiment async

Asks ChatGPT to analyze sentiment from a given text.

Parameters:

text: A string being the text to be analyzed.

entity (optional): A string being the entity to analyze sentiment towards.

Example code:

(async () => {
    const text = 'Are you an #OSS supporter? Do you love JavaScript? Then why not contribute to the future of #AI app development? https://chatgpt.js.org (a #100Builders project) is seeking collabs for exactly this! @withBackdrop';
    const sentiment = await chatgpt.sentiment(text, '100 Builders');
    console.log(sentiment);

    /* Example output:
    The sentiment of the text towards the entity "100 Builders" is strongly positive. The text encourages
    individuals who support open-source software (OSS) and have an affinity for JavaScript to get involved with
    the project. Phrases like "contribute to the future," "seeking collabs," and the inclusion of the hashtag
    #100Builders project indicate a positive and enthusiastic tone, promoting engagement and collaboration
    with the project. */
})();

suggest async

Asks ChatGPT to suggest ideas.

Parameters:

ideaType: A string being the type of idea to suggest.

details (optional): A string being details to fine-tune the suggestion.

Example code:

(async () => {
    const suggestions = await chatgpt.suggest('names', 'baby boy');
    console.log(suggestions);

    /* Example output:
    1. Liam
    2. Noah
    3. Ethan
    4. Oliver
    5. Jackson
    6. Aiden
    7. Lucas
    8. Benjamin
    9. Henry
    10. Leo
    11. Samuel
    12. Caleb
    13. Owen
    14. Daniel
    15. Elijah
    16. Matthew
    17. Alexander
    18. James
    19. Nathan
    20. Gabriel */
})();

summarize async

Asks ChatGPT to summarize given text.

Parameters:

text: A string being the text to be summarized.

Example code:

(async () => {
    const summary = await chatgpt.summarize('A very long text...');
    console.log(summary); // Example output: 'A very short text...'
})();

translate async

Asks ChatGPT to translate given text to a given language.

Parameters:

text: A string being the text to translate.

outputLang: A string representing the output language of the translation.

Example code:

(async () => {
    const translation = await chatgpt.translate('Hello, how are you?', 'spanish');
    console.log(translation); // Logs: 'Hola, ¿cómo estás?'
})();

uuidv4

Example code:

const randomID = chatgpt.uuidv4();
console.log(randomID); // Example output: '239067d1-bcb8-4fd7-91eb-9ab94619b7b3'

Page theme

activateDarkMode

Changes the website theme to dark mode.

Example code:

chatgpt.activateDarkMode();

activateLightMode

Changes the website theme to light mode.

Example code:

chatgpt.activateLightMode();

isDarkMode

Returns a boolean value. true if the theme is dark mode, false otherwise.

Example code:

console.log(chatgpt.settings.scheme.isDark()); // logs `true` or `false`

isLightMode

Returns a boolean value. true if the theme is light mode, false otherwise.

Example code:

console.log(chatgpt.settings.scheme.isDark()); // logs `true` or `false`

toggleScheme

Toggles the theme between light and dark mode.

Example code:

chatgpt.toggleScheme();

In-site notifications

alert

Creates a static alert box which displays a message. Only a user interaction can close it. Returns the HTML id property of the alert box as a string.

Parameters:

title (optional): A string which is the title of the alert.

msg (optional): A string which is the message to be displayed.

btns (optional): An array of functions which will be rendered as clickable buttons.

checkbox (optional): A function which will be rendered as a checkbox.

width (optional): An integer representing the width of the alert box in px.

Example code:

function doSomething() { /* Your code */ }

function doSomethingElse() { /* Your code */ }

function sayHello() { console.log('Hello!'); }

const alertID = chatgpt.alert('Hello, world!', 'The sky is blue.', [doSomething, doSomethingElse], sayHello, 200);
console.log(alertID); // Example output: '1693237957878'

notify

Displays a temporary notification at a specified position in the website.

Parameters:

msg: A string which is the message to be displayed.

position (optional): A string specifying the position of the notification.

notifDuration (optional): A float specifying the duration of the notification before it fades out.

shadow (optional): A string specifying if the box-shadow CSS property should be used.

Example code:

chatgpt.notify('Hello, world!', 'top left', 3, 'on');

User session

getAccessToken async

Returns an account access token as a string.

(async () => {
    const token = await chatgpt.getAccessToken();
    console.log(token); // Example output: 'abcdef[...]'
})();

getAccountDetails async

Returns a given account detail as a string.

Parameters:

detail: A string representing the account detail(s) that will be returned.

Can be the following: email, id, image, name, picture. If a single detail is passed, it will be returned as a string, if multiple are passed instead, the function will return an object with the requested details. If no details are passed, the function will return an object with all the available details.

(async () => {
    const accountName = await chatgpt.getAccountDetails('name');
    console.log(accountName); // Example output: 'chatgpt.js'

    const accountData = await chatgpt.getAccountDetails('name', 'email');
    console.log(accountData);
    /* Example output:
    {
        name: 'chatgpt.js',
        email: 'showcase@chatgptjs.org'
    }
    */
})();

logout

Logs out the user from the website.

Example code:

chatgpt.logout();

Chats

askAndGetReply async

Sends a given message to ChatGPT and returns the response as a string.

Example code:

(async () => {
    const response = await chatgpt.askAndGetReply('Hello, ChatGPT');
    console.log(response); // Example output: 'Hello user, I'm ChatGPT!'
})();

clearChats async

method (optional): The string 'api' or 'dom' representing the method to use.

Note The API method does not update DOM chat list (until session refresh)

exportChat async

Exports a given chat as a file.

Parameters:

chatToGet (optional): A string representing the chat to get the data from.

Can be the following: active, the current chat, latest, the latest chat in the list, else the index, title or id of the chat to get. Default is active if in a chat, else latest.

format (optional): A string representing the format of the export file.

Can be the following: html, md, pdf or text. Defaults to html.

Example code:

(async () => {
    await chatgpt.exportChat('latest', 'html'); // Downloads a '.html' file
})();

getChatData async

Returns the requested chat data as a string (if single detail requested) or object of key-value pairs (if multiple details requested).

Parameters:

chatToGet (optional): A string representing the chat to get the data from.

Can be the following: active, the current chat, latest, the latest chat in the list, else the index, title or id of the chat to get. Default is active if in a chat, else latest.

detailsToGet (optional): A string or array of strings representing the chat data to retrieve.

Can be the following: all to get all details, id, title, create_time, update_time or msg. To get a single detail, just use a string, to get multiple use an array of strings instead. Default is all.

If msg is the requested detail, the following parameters can be used:

sender (optional): A string representing the chat member to get the message(s) from.

Can be the following: user to get the user message(s), chatgpt to get ChatGPT's response(s), all/both to get both of them. Default is all.

msgToGet (optional): A string/number representing the chat message to retrieve.

Can be the following: all to get all the messages in the chat, latest to get the latest message/response, or the index of the message. Default is all.

Note If any user messages were edited, they are added to the index as newly sent messages

Example code for all return types:

All details from specified chat

await chatgpt.getChatData();
// or
await chatgpt.getChatData('latest'); // can also be 'active', 'title of the chat' or 'id of the chat'
// or
await chatgpt.getChatData('latest', 'all');

Returns a JSON object

{
  "create_time": "2023-07-19T13:24:05.618539+00:00",
  "id": "e193a219-2311-4232-95f5-8e3a0e466652",
  "title": "Lemons: Citrus Fruit Overview.",
  "update_time": "2023-07-19T13:24:18+00:00"
}

Specific detail(s) from specified chat

await chatgpt.getChatData('latest', ['id', 'title']);

Returns a JSON object

{
  "id": "e193a219-2311-4232-95f5-8e3a0e466652",
  "title": "Lemons: Citrus Fruit Overview."
}

All messages from both participants in a specified chat

await chatgpt.getChatData('latest', 'msg');
// or
await chatgpt.getChatData('latest', 'msg', 'all'); // all/both
// or
await chatgpt.getChatData('latest', 'msg', 'all', 'all');

Returns an array of JSON objects

In case of a response being regenerated, the chatgpt object key will be converted to an array containing all the responses.

[
  {
    "user": "what are lemons",
    "chatgpt": "Lemons are a type of citrus fruit that belongs..."
  },
  {
    "user": "be more specific",
    "chatgpt": [
      "Certainly! Here are some more specific...",
      "Certainly! Here are some specific..." // regenerated responses!
    ]
  }
]

All messages from a specific participant in a specified chat

await chatgpt.getChatData('latest', 'msg');
// or
await chatgpt.getChatData('latest', 'msg', 'chatgpt'); // user/chatgpt
// or
await chatgpt.getChatData('latest', 'msg', 'chatgpt', 'all');

Returns an array of strings/arrays

In case of a response being regenerated and the requested participant being chatgpt, it'll be converted to an array containing all the responses.

[
  "Lemons are a type of citrus fruit that belongs...",
  [
    "Certainly! Here are some more specific details...",
    "Certainly! Here are some specific..."
  ]
]

One/latest message from both participants in a specified chat

await chatgpt.getChatData('latest', 'msg', 'all', 2); // can also be 'latest' message

Returns a JSON object

In case of a response being regenerated, the chatgpt object key will be converted to an array containing all the responses.

{
  "user": "be more specific",
  "chatgpt": [
    "Certainly! Here are some more specific...",
    "Certainly! Here are some specific..."
  ]
}

One/latest message from a specific participant in a specified chat

await chatgpt.getChatData('latest', 'msg', 'chatgpt', 2);

Returns a string or an array of strings

In case of a response being regenerated, the chatgpt object key will be converted to an array containing all the responses.

"Certainly! Here are some more specific..."
// or
[
  "Certainly! Here are some more specific...",
  "Certainly! Here are some specific..."
]

getChatInput

Returns the value of the chat input field as a string.

Example code:

const chatInput = chatgpt.getChatInput();
console.log(chatInput); // Example output: 'Hello from chatgpt.js!'

getLastPrompt async

Returns the last message sent by the user as a string.

(async () => {
    const message = await chatgpt.getLastPrompt();
    console.log(message); // Example output: 'Hello from chatgpt.js!'
})();

getLastResponse async

Returns the last ChatGPT response as a string.

(async () => {
    const response = await chatgpt.getLastResponse();
    console.log(response); // Example output: 'I am ChatGPT!'
})();

getResponse

If it's a previously created chat, read chatgpt.getResponseFromDOM

If it's a new chat, read chatgpt.getResponseFromAPI

getResponseFromAPI async

Returns the Nth response ChatGPT has written in a Nth chat as a string.

Parameters:

chatToGet (optional): A number representing the index of the chat to get the response from. Defaults to latest.

responseToGet (optional): A number representing the index of the response to get. Defaults to latest.

Example code:

(async () => {
    const response = chatgpt.getResponseFromAPI();
    console.log(response); // Example output: 'Hello from ChatGPT!'
})();

getResponseFromDOM

Returns the Nth response ChatGPT has written as a string.

Parameters:

pos: A string or integer representing the position of the wanted response.

Example code:

var fifthResp;

fifthResp = chatgpt.getResponseFromDOM(5); // Returns the 5th response
fifthResp = chatgpt.getResponseFromDOM('fifth'); // Also returns the 5th response
fifthResp = chatgpt.getResponseFromDOM('five'); // Returns the 5th response too

console.log(fifthResp); // Example output: 'Hello from ChatGPT!'

isIdle async

Returns a boolean value. true if ChatGPT has finished generating a response, false otherwise.

Example code:

(async () => {
    if (await chatgpt.isIdle()) {
        // Do something
    }
})();

regenerate

Regenerates ChatGPT's response.

Example code:

chatgpt.regenerate();

resend async

Re-sends the last user message.

(async () => {
    await chatgpt.resend();
})();

scrollToBottom

Scrolls to the bottom of the chat.

Example code:

chatgpt.scrollToBottom();

send

Sends a message into the chat.

Parameters:

msg: A string representing the message to send.

method (optional): A string representing the method to send the message with, can only be click. Usually needed for mobile devices compatibility.

Example code:

// Clicks the send button instead of triggering the 'Enter' key press.
chatgpt.send('Hello, world!', 'click');

sendInNewChat

Creates a new chat and sends a message.

Parameters:

msg: A string representing the message to send.

Example code:

chatgpt.sendInNewChat('Hello, world!');

shareChat async

Makes the selected chat available to others. Returns the URL of the chat as a string.

Parameters:

chatToGet (optional): A number or string representing the index, title or id of the chat to share.

method (optional): A string representing the method to share the chat with. Defaults to clipboard.

Can be the following: copy or clipboard to copy the chat URL to clipboard, alert, notify or notification to create an alert message with the details about the shared chat in the website.

(async () => {
    await chatgpt.shareChat(1, 'copy'); // copy/clipboard
})();

speak

Text To Speech (TTS) conversion of a given message.

Parameters:

msg: A string representing the message to TTS.

options (optional): An object containing the options for the vocal synthesizer.

Available options:

  • voice: A number representing the index of voices available on the user device.
  • pitch: A float representing the pitch of the speech. From 0 to 2.
  • speed: A float representing the speed of the speech. From 0.1 to 10.

Example code:

(async () => {
    chatgpt.speak(await chatgpt.getLastResponse(), { voice: 1, pitch: 2, speed: 3 });
})();

startNewChat

Creates a new chat.

Example code:

chatgpt.startNewChat();

stop

Stops the generation of ChatGPT's response.

Example code:

chatgpt.stop();

DOM related

getChatBox

Returns the chat input as an HTML element.

Example code:

const chatbox = chatgpt.getChatBox();
console.log(chatbox.value); // Example output: 'Hello from chatgpt.js!'

getContinueGeneratingButton

Returns the 'Continue generating' button as an HTML element.

Example code:

const continueBtn = chatgpt.getContinueGeneratingButton();
continueBtn.click();

getFooterDiv

Returns the footer div as an HTML element.

Example code:

const footerDiv = chatgpt.getFooterDiv();
footerDiv.style.padding = '15px'; // make the footer taller

getHeaderDiv

Returns the header div as an HTML element.

Example code:

const headerDiv = chatgpt.getHeaderDiv();
headerDiv.style.display = none; // hide the header

getNewChatLink

Returns the button which creates a new chat as an HTML element.

Example code:

const newChatLink = chatgpt.getNewChatLink();
newChatLink.click();

getRegenerateButton

Returns the button which regenerates ChatGPT's response as an HTML element.

Example code:

const regenBtn = chatgpt.getRegenerateButton();
regenBtn.click();

getScrollToBottomButton

Returns the button which scrolls to bottom as an HTML element.

Example code:

const scrollToBottomBtn = chatgpt.getScrollToBottomButton();
scrollToBottomBtn.click();

getSendButton

Returns the button which sends the message as an HTML element.

Example code:

const sendBtn = chatgpt.getSendButton();
sendBtn.click();

getStopGeneratingButton

Returns the button which stops the generation of ChatGPT's response as an HTML element.

Example code:

const stopBtn = chatgpt.getStopGeneratingButton();
stopBtn.click();

hideFooter

Hides the footer div.

Example code:

chatgpt.hideFooter()

hideHeader

Hides the header div.

Example code:

chatgpt.hideHeader()

showFooter

Shows the footer div if hidden.

Example code:

chatgpt.showFooter()

showHeader

Shows the header div if hidden.

Example code:

chatgpt.showHeader()

Library APIs

autoRefresh api

API related to keeping the user's session alive and fresh.

activate

Activates the auto-refresh functionality.

Parameters:

interval (optional): A number representing the interval in seconds between sessions refreshes. Defaults to 30.

Example code:

chatgpt.autoRefresh.activate();

deactivate

Deactivates the auto-refresh functionality.

Example code:

chatgpt.autoRefresh.deactivate();

nowTimeStamp

Returns the current timestamp as a string (12-hour format).

Example code:

const timeStamp = chatgpt.autoRefresh.nowTimeStamp();
console.log(timeStamp); // Example output: '1:56:25 PM'

browser api

isLightMode

Returns a boolean value. true if system/browser scheme preference is set to light, false otherwise.

Example code:

console.log(chatgpt.browser.isLightMode()); // logs `true` or `false`

isDarkMode

Returns a boolean value. true if system/browser scheme preference is set to dark, false otherwise.

Example code:

console.log(chatgpt.browser.isDarkMode()); // logs `true` or `false`

isChromium

Returns a boolean value. true if the browser is Chromium and false otherwise.

Example code:

if (chatgpt.browser.isChromium()) {
    // Do something
}

isFirefox

Returns a boolean value. true if the browser is Firefox and false otherwise.

Example code:

if (chatgpt.browser.isFirefox()) {
    // Do something
}

isFullScreen

Returns a boolean value. true if the browser is fullscreen and false otherwise.

Example code:

if (chatgpt.browser.isFullScreen()) {
    // Do something
}

isMobile

Returns a boolean value. true if the browser is mobile and false otherwise.

Example code:

if (chatgpt.browser.isMobile()) {
    // Do something
}

code api

minify async

Asks ChatGPT to minify the given code.

Parameters:

code: A string being the code to be minified.

Example code:

(async () => {
    const minifiedCode = await chatgpt.code.minify(`
        function autosizeBox() {
            const newLength = replyBox.value.length
            if (newLength < prevLength) { // if deleting txt
                replyBox.style.height = 'auto' // ...auto-fit height
                if (parseInt(getComputedStyle(replyBox).height) < 55) { // if down to one line
                    replyBox.style.height = '2.15rem' } // ...reset to original height
            }
            replyBox.style.height = replyBox.scrollHeight + 'px'
            prevLength = newLength
        }`);
    console.log(minifiedCode);

    /* Logs:
    'function autosizeBox(){const n=replyBox.value.length;if(n<prevLength){replyBox.style.height='auto';if(parseInt(getComputedStyle(replyBox).height)<55){replyBox.style.height='2.15rem'}}replyBox.style.height=replyBox.scrollHeight+'px';prevLength=n}' */
})();

execute async

Asks ChatGPT to execute the given code.

Parameters:

code: A string being the code to execute.

Example code:

(async () => {
    console.log(await chatgpt.code.execute('return 6 + 5')); // logs '11'
})();

extract

Extracts pure code from response.

Parameters:

msg: A string being the response to extract code from.

Example code:

(async () => {
    chatgpt.send('What is a short script to delete files?');
    await chatgpt.isIdle();
    const response = await chatgpt.getChatData('active', 'msg', 'chatgpt', 'latest'),
          scriptCode = chatgpt.code.extract(response);
    console.log(scriptCode);

    /* Logs:    
    const fs = require('fs');

    // Specify the path of the file(s) you want to delete
    const filePath = 'path/to/your/file.txt';

    // Delete the file
    fs.unlink(filePath, (err) => {
        if (err) {
            console.error('Error deleting file:', err);
        } else {
            console.log('File deleted successfully');
        }
    }); */
})();

obfuscate async

Asks ChatGPT to obfuscate the given code.

Parameters:

code: A string being the code to obfuscate.

Example code:

(async () => {
    const code = `window[elem].addEventListener('mouseover', toggleTooltip)`
    const obfuscatedCode = await chatgpt.code.obfuscate(code);
    console.log(obfuscatedCode);

    /* Logs:
    '(window[elem])[btoa('YWxlcnRWaWV3')](btoa('bW91c2VyYm94ZXJOYW1l'), btoa('dG9nZ2VkT3V0d2FsbA==')); */
})();

refactor async

Asks ChatGPT to refactor the given code.

Parameters:

code: A string being the code to refactor.

objective (optional): A string reprenting the objective of the refactoring. Defaults to brevity.

Example code:

(async () => {
    const code =  `
        if (6 > 5) {
            return true;
        } else {
            return false;
        }`;
    const refactoredCode = await chatgpt.code.refactor(code, 'brevity');
    console.log(refactoredCode);

    /* Logs:
    return 6 > 5; */
})();

review async

Asks ChatGPT to review given code.

Parameters:

code: A string being the code to review.

Example code:

(async () => {
    console.log(await chatgpt.code.review('btoa("Hello World")'));

    /* Example output:
    The code appears to be correct. It uses the `btoa` function to encode the string "Hello World" in base64. */
})();

unminify async

Asks ChatGPT to unminify the given code.

Parameters:

code: A string being the code to unminify.

Example code:

(async () => {
    const code = `function autosizeBox(){const n=replyBox.value.length;if(n<prevLength){replyBox.style.height='auto';if(parseInt(getComputedStyle(replyBox).height)<55){replyBox.style.height='2.15rem'}}replyBox.style.height=replyBox.scrollHeight+'px';prevLength=n}`;

    const minifiedCode = await chatgpt.code.unminify(code);
    console.log(minifiedCode);

    /* Logs:
    function autosizeBox() {
        const newLength = replyBox.value.length
        if (newLength < prevLength) { // if deleting txt
            replyBox.style.height = 'auto' // ...auto-fit height
            if (parseInt(getComputedStyle(replyBox).height) < 55) { // if down to one line
                replyBox.style.height = '2.15rem' } // ...reset to original height
        }
        replyBox.style.height = replyBox.scrollHeight + 'px'
        prevLength = newLength
    }` */
})();

write async

Asks ChatGPT to write code given a prompt.

Parameters:

prompt: A string describing the code to generate.

outputLang: A string representing the code language to generate the prompt with.

Example code:

(async () => {
    const code = await chatgpt.code.write('Repeat a task every 10 seconds', 'javascript');
    console.log(code);

    /* Logs:
    setInterval(function() {
        // Your task code here
    }, 10000); */
})();

history api

API related to the chat history.

isLoaded async

Resolves a promise when chat history has finished loading.

Example code:

(async () => {
    await chatgpt.history.isLoaded();
    console.log('ChatGPT history has finished loading.');
})();

instructions api

add async

Adds a custom instruction for either the user or ChatGPT.

Parameters:

instruction: A string being the instruction to be added.

target: A string representing the target of the instruction. Can be either user or chatgpt.

Example code:

(async () => {
    await chatgpt.instructions.add('Detailed and well-explained answers', 'chatgpt');
})();

clear async

Clears the custom instructions of either the user or ChatGPT.

Parameters:

target: A string representing the target of the instruction. Can be either user or chatgpt.

Example code:

(async () => {
    await chatgpt.instructions.clear('user');
})();

turnOff async

Turns off custom instructions.

Example code:

(async () => {
    await chatgpt.instructions.turnOff();
})();

turnOn async

Turns on custom instructions.

Example code:

(async () => {
    await chatgpt.instructions.turnOn();
})();

toggle async

Toggles on/off custom instructions.

Example code:

(async () => {
    await chatgpt.instructions.toggle();
})();

menu api

The small menu that shows up when clicking on the account button.

open

Opens the menu.

Example code:

chatgpt.menu.open();

close

Closes the menu.

Example code:

chatgpt.menu.close();

response api

API related to ChatGPT's responses.

get

If it's a previously created chat, read chatgpt.getResponseFromDOM

If it's a new chat, read chatgpt.getResponseFromAPI

getFromAPI async

Read chatgpt.getResponseFromAPI

getFromDOM

Read chatgpt.getResponseFromDOM

getLast async

Read chatgpt.getLastResponse

regenerate

Read chatgpt.regenerate

stopGenerating

Read chatgpt.stop

settings api

API for interfacing with ChatGPT user settings.

scheme api subset

isDark

Returns a boolean value. true if the theme is dark mode, false otherwise.

Example code:

console.log(chatgpt.settings.scheme.isDark()); // logs `true` or `false`

isLight

Returns a boolean value. true if the theme is light mode, false otherwise.

Example code:

console.log(chatgpt.settings.scheme.isLight()); // logs `true` or `false`

set

Sets the theme to light, dark or system.

Paremeters:

value: A string being the value to set the theme to.

Example code:

chatgpt.settings.scheme.set('dark');

toggle

Toggles the theme between light and dark mode.

Example code:

chatgpt.settings.scheme.toggle();

sidebar api

API related to the sidebar's behavior.

append

Appends a new element to the sidebar. Returns the id property of the element.

Parameters:

element: A string being the name of the element to append.

Currently supported elements are button and dropdown.

attrs: An object which contains the attributes of the element to append.

Attributes for button

label: A string being the label (displayed text) of the button. Defaults to chatgpt.js button.

icon: A string being either a url to an image or a base64 encoded string of the image data. Defaults to this icon.

onclick: A function which is called when the button is clicked. Defaults to function() {}.

Attributes for dropdown

items: An array of objects where the text key is the displayed text of the option, and the value key is the value of the option.

Example item object:

{
    text: 'The text to display in the option',
    value: 'The value of the option'
}

Example code:

const buttonId = chatgpt.sidebar.append('button', {
    label: 'I am a button!',
    icon: 'https://chat.openai.com/favicon-32x32.png',
    onclick: function() {
        console.log('Clicked!');
    }
});
console.log(buttonId); // Example output: 1693295258727

const dropdownId = chatgpt.sidebar.append('dropdown', {
    items: [
        { text: 'Hello world', value: 'helloworld' },
        { text: 'Hello there', value: 'hellothere' }
    ]
});
console.log(dropdownId); // Example output: 1693294795240

isOn

Returns a boolean value. true if the sidebar is open, false otherwise.

Example code:

if (chatgpt.sidebar.isOn()) {
    // Do something
}

isOff

Returns a boolean value. true if the sidebar is closed, false otherwise.

Example code:

if (chatgpt.sidebar.isOff()) {
    // Do something
}

hide

Hides the sidebar.

Example code:

chatgpt.sidebar.hide();

show

Shows the sidebar.

Example code:

chatgpt.sidebar.show();

toggle

Toggles the visibility of the sidebar.

Example code:

chatgpt.sidebar.toggle();


Discuss / Back to top ↑