I'm trying to extract the full code from a CodeMirror editor on chatgpt using the browser console. My current approach only retrieves the visible portion of the code, likely because CodeMirror is virtualizing the rendering and only displaying the lines in view. When I query for elements with the class .cm-line, I only get part of the content.
Here is my current code:
(() => {
// Function to extract visible content from the editor
const extractVisibleContent = () => {
const editorElement = document.querySelector('#codemirror .cm-editor');
if (!editorElement) {
console.warn('Editor not found.');
return null;
}
const lines = Array.from(editorElement.querySelectorAll('.cm-line'));
if (!lines.length) {
console.warn('No visible lines found.');
return null;
}
const visibleContent = lines.map(line => line.textContent).join('\n');
console.log('Using fallback of visible lines. Content may be incomplete.');
return visibleContent;
};
// Execute after DOM load with a delay to ensure the editor is rendered
const onReady = () => {
setTimeout(() => {
const content = extractVisibleContent();
if (content !== null) {
console.log('Extracted editor content:', content);
}
}, 1000);
};
if (document.readyState === "complete" || document.readyState === "interactive") {
onReady();
} else {
document.addEventListener('DOMContentLoaded', onReady);
}
})();
When I run this on chatgpt, the output is cut off. For example, it logs:
Extracted editor content: [ { "disciplina": "Direito Constitucional", "enunciado": "Qual princípio constitucional está relacionado à proibição do retrocesso social?", ...
I suspect that because CodeMirror only renders the visible lines, the rest of the content is not in the DOM at the time of extraction. Is there a way to access the complete content of the CodeMirror editor on chatgpt? Can I use a CodeMirror API or another method to retrieve all of the content regardless of what is visible?