Created
June 1, 2023 13:04
-
-
Save LaurentiuGabriel/ebc0a9d4074e8555ae74e91e72305dd7 to your computer and use it in GitHub Desktop.
The JS code that will add an input file button to ChatGPT. You can add a file and feed it into ChatGPT
This file contains hidden or 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
// Create the file input element | |
var fileInput = document.createElement('input'); | |
fileInput.type = 'file'; | |
fileInput.id = 'fileInput'; | |
fileInput.style.display = 'none'; // Hide the file input | |
fileInput.accept = '.txt'; // Accept only .txt files | |
// Create the button element | |
var button = document.createElement('button'); | |
button.textContent = 'Upload txt file'; | |
button.style.display = 'block'; // Make the button block-level to appear on its own line | |
// Attach event listener to button | |
button.addEventListener('click', function() { | |
// Trigger the file input click | |
fileInput.click(); | |
}); | |
// Attach event listener to file input | |
fileInput.addEventListener('change', async function() { | |
if (this.files && this.files[0]) { | |
var myFile = this.files[0]; | |
var reader = new FileReader(); | |
reader.addEventListener('load', async function(e) { | |
var fileContent = e.target.result; | |
// Split the file content into chunks | |
var chunks = chunkSubstr(fileContent, 15000); | |
// For each chunk, simulate text input and submission | |
for (var i = 0; i < chunks.length; i++) { | |
var chunk = chunks[i]; | |
// Insert chunk into the textarea | |
var textArea = document.getElementById('prompt-textarea'); | |
textArea.value = chunk; | |
// Simulate a 'change' event to make sure React picks up the change | |
var event = new Event('input', { bubbles: true }); | |
textArea.dispatchEvent(event); | |
// This should ideally "submit" the text, but it might not work | |
// due to security measures put in place by many websites | |
var sendButton = textArea.parentNode.querySelector('button'); | |
if (sendButton) { | |
sendButton.click(); | |
} | |
// Sleep for a second (or more if needed) to ensure previous message was processed | |
await sleep(1000); | |
} | |
}); | |
reader.readAsText(myFile); | |
} | |
}); | |
// Insert the button and file input after the textarea | |
var textArea = document.getElementById('prompt-textarea'); | |
textArea.insertAdjacentElement('afterend', button); | |
textArea.insertAdjacentElement('afterend', fileInput); | |
// Helper function to sleep for a specified duration | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
// Helper function to split string into chunks | |
function chunkSubstr(str, size) { | |
const numChunks = Math.ceil(str.length / size); | |
const chunks = new Array(numChunks); | |
for(let i = 0, o = 0; i < numChunks; ++i, o += size) { | |
chunks[i] = str.substr(o, size); | |
} | |
return chunks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment