Created
October 30, 2023 02:38
-
-
Save JimLiu/883be4e9ddbf9a5951e57be5659c006e to your computer and use it in GitHub Desktop.
Get React state of ChatGPT from React Dev Tools
This file contains 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
(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