我通过搜索大量 JS 数据找到了一个解决方案,但我可以提取一个返回 React 应用程序状态对象的函数。
我注意到状态嵌套在实际的 React 根 DOM 节点下方 6 到 10 层,因此我编写了一个函数来循环遍历 React 应用程序并通过属性名称标识状态。
此代码已使用 React 16.7 进行测试
function getAppState(rootNode, globalAttributeName) {
var $root = jQuery(rootNode);
var reactState = false;
var searchStack = [];
var searched = [];
// Fetch the "__reactInternalInstance$..." attribute from the DOM node.
for ( const key in $root[0] ) {
// The full name changes on every page load, e.g. "__reactInternalInstance$ek3wnas4g6"
if (0 === key.indexOf('__reactInternalInstance$') ) {
searchStack.push($root[0][key]);
break;
}
}
// Find the state by examining the object structure.
while (stack.length) {
var obj = stack.pop();
if (obj && typeof obj == 'object' && -1 === searched.indexOf(obj)) {
if ( undefined !== obj[ globalAttributeName ] ) {
reactState = obj;
break;
}
for (i in obj) {
stack.push(obj[i]);
}
searched.push(obj);
}
}
return reactState;
}
// In my example, I know that the app uses "state.activeModule",
// so I search for the key "activeModule" to identify the state:
getAppState('#root-id', 'activeModule');
// Here is the full path to the state, in my example:
// __reactInternalInstance$ek3wnas4g6g.alternate.return.alternate.return.alternate.memoizedProps._owner.alternate.memoizedState
这个答案极大地帮助我找到了这个解决方案:https : //stackoverflow.com/a/12103127/313501