Skip to content

Instantly share code, notes, and snippets.

@JimLiu
Created October 30, 2023 02:38
Show Gist options
  • Save JimLiu/883be4e9ddbf9a5951e57be5659c006e to your computer and use it in GitHub Desktop.
Save JimLiu/883be4e9ddbf9a5951e57be5659c006e to your computer and use it in GitHub Desktop.
Get React state of ChatGPT from React Dev Tools
(function() {
const getMessagesWithReactDevTools = () => {
const messages = [];
function traverseComponentTree(fiberNode) {
let parts = fiberNode.memoizedProps?.parts;
if (Array.isArray(parts)) {
// console.log(fiberNode, parts);
if (typeof parts[0] === 'string') {
messages.push(parts.join(''));
}
}
let child = fiberNode.child;
while (child) {
traverseComponentTree(child);
child = child.sibling;
}
}
const devtools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
const rootFiber = devtools?.getFiberRoots(1)?.values()?.next()?.value
?.current;
if (rootFiber) {
traverseComponentTree(rootFiber);
} else {
console.error('No root fiber found');
}
return messages;
}
const messages = getMessagesWithReactDevTools();
console.log(messages);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment