-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreact.go
More file actions
133 lines (124 loc) · 3.93 KB
/
Copy pathreact.go
File metadata and controls
133 lines (124 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package engine
import (
"encoding/json"
"fmt"
"github.com/go-rod/rod"
)
// ReactComponent represents a React component in the fiber tree.
type ReactComponent struct {
Name string `json:"name"`
Type string `json:"type"`
Key string `json:"key,omitempty"`
Props json.RawMessage `json:"props,omitempty"`
State json.RawMessage `json:"state,omitempty"`
Children []ReactComponent `json:"children,omitempty"`
}
// ReactTree extracts the React component tree from the page.
// Requires React DevTools hook (__REACT_DEVTOOLS_GLOBAL_HOOK__).
func ReactTree(page *rod.Page, maxDepth int) ([]ReactComponent, error) {
if maxDepth <= 0 {
maxDepth = 10
}
res, err := page.Eval(fmt.Sprintf(`() => {
const hook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
if (!hook || !hook.renderers || hook.renderers.size === 0) return null;
function walk(fiber, depth) {
if (!fiber || depth > %d) return null;
const name = fiber.type
? (typeof fiber.type === 'string' ? fiber.type : fiber.type.displayName || fiber.type.name || 'Anonymous')
: 'Fragment';
const node = {
name: name,
type: typeof fiber.type === 'string' ? 'host' : 'composite',
key: fiber.key || '',
};
if (fiber.memoizedProps && typeof fiber.type !== 'string') {
try {
const p = {};
for (const [k,v] of Object.entries(fiber.memoizedProps)) {
if (typeof v !== 'function' && typeof v !== 'object') p[k] = v;
}
if (Object.keys(p).length > 0) node.props = p;
} catch(e) {}
}
if (fiber.memoizedState && typeof fiber.memoizedState === 'object' && fiber.memoizedState !== null) {
try {
const s = fiber.memoizedState.memoizedState;
if (s !== undefined && typeof s !== 'function') node.state = s;
} catch(e) {}
}
const children = [];
let child = fiber.child;
while (child) {
const c = walk(child, depth + 1);
if (c) children.push(c);
child = child.sibling;
}
if (children.length > 0) node.children = children;
return node;
}
const roots = [];
hook.renderers.forEach((renderer, id) => {
const fiberRoots = hook.getFiberRoots ? hook.getFiberRoots(id) : new Set();
fiberRoots.forEach(root => {
const tree = walk(root.current, 0);
if (tree) roots.push(tree);
});
});
return roots.length > 0 ? roots : null;
}`, maxDepth))
if err != nil {
return nil, fmt.Errorf("react tree: %w", err)
}
if res.Value.Val() == nil {
return nil, fmt.Errorf("no React detected on this page (missing __REACT_DEVTOOLS_GLOBAL_HOOK__)")
}
raw, err := res.Value.MarshalJSON()
if err != nil {
return nil, err
}
var components []ReactComponent
if err := json.Unmarshal(raw, &components); err != nil {
return nil, fmt.Errorf("parse react tree: %w", err)
}
return components, nil
}
// ReactSuspense returns the current Suspense boundary states.
func ReactSuspense(page *rod.Page) ([]map[string]any, error) {
res, err := page.Eval(`() => {
const hook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
if (!hook || !hook.renderers || hook.renderers.size === 0) return null;
const boundaries = [];
function walk(fiber) {
if (!fiber) return;
if (fiber.tag === 13) {
boundaries.push({
fallback: !!(fiber.memoizedState),
name: fiber.type?.displayName || fiber.type?.name || 'Suspense',
});
}
walk(fiber.child);
walk(fiber.sibling);
}
hook.renderers.forEach((renderer, id) => {
const fiberRoots = hook.getFiberRoots ? hook.getFiberRoots(id) : new Set();
fiberRoots.forEach(root => walk(root.current));
});
return boundaries.length > 0 ? boundaries : null;
}`)
if err != nil {
return nil, fmt.Errorf("react suspense: %w", err)
}
if res.Value.Val() == nil {
return nil, fmt.Errorf("no React Suspense boundaries found")
}
raw, err := res.Value.MarshalJSON()
if err != nil {
return nil, err
}
var boundaries []map[string]any
if err := json.Unmarshal(raw, &boundaries); err != nil {
return nil, err
}
return boundaries, nil
}