-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsandbox.html
More file actions
212 lines (177 loc) · 7.17 KB
/
sandbox.html
File metadata and controls
212 lines (177 loc) · 7.17 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sandbox</title>
</head>
<body>
<script>
// Helper function to add timestamp prefix to console logs
function getTimestamp() {
var now = new Date();
return '[' + now.toISOString() + ']';
}
// This sandboxed page can execute arbitrary scripts without CSP restrictions
// It communicates with the content script via postMessage
// Queue to handle parallel requests sequentially
var requestQueue = [];
var isProcessing = false;
function processNextRequest() {
if (isProcessing || requestQueue.length === 0) {
return;
}
isProcessing = true;
var request = requestQueue.shift();
var requestId = request.requestId;
var htmlContent = request.html;
var source = request.source;
console.log(getTimestamp(), 'Processing request', requestId);
console.log(getTimestamp(), 'HTML length:', htmlContent.length);
// First, try to extract APP_INITIALIZATION_STATE directly from the HTML string using regex
// This is more reliable than trying to execute scripts
var statePatterns = [
/window\.APP_INITIALIZATION_STATE\s*=\s*(\[[\s\S]*?\]);/,
/var\s+APP_INITIALIZATION_STATE\s*=\s*(\[[\s\S]*?\]);/,
/APP_INITIALIZATION_STATE\s*=\s*(\[[\s\S]*?\]);/,
/"APP_INITIALIZATION_STATE"\s*:\s*(\[[\s\S]*?\])\s*[,}]/,
/APP_INITIALIZATION_STATE\s*=\s*(\[[^\]]*\])/
];
var appStateString = null;
var matchedPattern = null;
for (var p = 0; p < statePatterns.length; p++) {
var match = htmlContent.match(statePatterns[p]);
if (match && match[1]) {
appStateString = match[1];
matchedPattern = p;
console.log(getTimestamp(), 'Found APP_INITIALIZATION_STATE using pattern', p, 'for request', requestId);
break;
}
}
if (appStateString) {
// Try to parse the extracted data
try {
var appState = JSON.parse(appStateString);
console.log(getTimestamp(), 'Successfully parsed APP_INITIALIZATION_STATE for request', requestId);
source.postMessage({
action: 'appStateExtracted',
requestId: requestId,
success: true,
data: appState
}, '*');
// Process next request
isProcessing = false;
setTimeout(processNextRequest, 10);
return;
} catch (parseErr) {
console.error(getTimestamp(), 'Failed to parse APP_INITIALIZATION_STATE:', parseErr.message);
console.log(getTimestamp(), 'Extracted string preview:', appStateString.substring(0, 500));
}
}
// If regex extraction failed, try executing scripts
console.log(getTimestamp(), 'Regex extraction failed, trying script execution...');
// Extract all inline script content
var scriptRegex = /<script(?:\s+[^>]*)?>([\s\S]*?)<\/script>/gi;
var scripts = [];
var scriptMatch;
while ((scriptMatch = scriptRegex.exec(htmlContent)) !== null) {
// Skip script tags with src attribute (external scripts)
var scriptTag = scriptMatch[0];
if (!/\ssrc\s*=/.test(scriptTag)) {
scripts.push(scriptMatch[1]);
}
}
console.log(getTimestamp(), 'Found ' + scripts.length + ' inline scripts to execute for request', requestId);
// Execute each script in this window's global context
try {
for (var i = 0; i < scripts.length; i++) {
try {
// Use indirect eval to execute in global scope
(1, eval)(scripts[i]);
} catch (scriptErr) {
console.warn(getTimestamp(), 'Script ' + i + ' execution error:', scriptErr.message);
// Continue executing other scripts
}
}
// Check for APP_INITIALIZATION_STATE with retries
var attempts = 0;
var maxAttempts = 10;
var checkForState = function() {
attempts++;
if (typeof window.APP_INITIALIZATION_STATE !== 'undefined') {
console.log(getTimestamp(), 'APP_INITIALIZATION_STATE found on attempt', attempts, 'for request', requestId);
// Clone the data before deleting
var data = window.APP_INITIALIZATION_STATE;
source.postMessage({
action: 'appStateExtracted',
requestId: requestId,
success: true,
data: data
}, '*');
// Clean up
delete window.APP_INITIALIZATION_STATE;
// Process next request
isProcessing = false;
setTimeout(processNextRequest, 10);
} else if (attempts < maxAttempts) {
setTimeout(checkForState, 300);
} else {
console.error(getTimestamp(), 'APP_INITIALIZATION_STATE not found after', maxAttempts, 'attempts for request', requestId);
console.log(getTimestamp(), 'Available window properties:', Object.keys(window).filter(function(k) {
return k.includes('APP') || k.includes('STATE') || k.includes('INIT');
}));
console.log(getTimestamp(), 'HTML preview:', htmlContent.substring(0, 1000));
source.postMessage({
action: 'appStateExtracted',
requestId: requestId,
success: false,
error: 'APP_INITIALIZATION_STATE not found after ' + maxAttempts + ' attempts'
}, '*');
// Process next request
isProcessing = false;
setTimeout(processNextRequest, 10);
}
};
// Start checking after a brief delay
setTimeout(checkForState, 100);
} catch (err) {
source.postMessage({
action: 'appStateExtracted',
requestId: requestId,
success: false,
error: 'Script execution error: ' + err.message
}, '*');
// Process next request
isProcessing = false;
setTimeout(processNextRequest, 10);
}
}
window.addEventListener('message', function(event) {
try {
if (event.data.action === 'extractAppState') {
console.log(getTimestamp(), 'Received extractAppState request:', event.data.requestId);
// Add request to queue
requestQueue.push({
requestId: event.data.requestId,
html: event.data.html,
source: event.source
});
console.log(getTimestamp(), 'Queue length:', requestQueue.length);
// Start processing if not already processing
processNextRequest();
}
} catch (err) {
console.error(getTimestamp(), 'Error handling message:', err);
event.source.postMessage({
action: 'appStateExtracted',
requestId: event.data.requestId,
success: false,
error: 'Sandbox error: ' + err.message
}, '*');
}
});
// Signal that sandbox is ready
window.parent.postMessage({action: 'sandboxReady'}, '*');
console.log(getTimestamp(), 'Sandbox initialized and ready');
</script>
</body>
</html>