GeminiBot commited on
Commit
add57ca
·
1 Parent(s): b3c25c2

Simplify eval context to fix undefined document error

Browse files
Files changed (1) hide show
  1. src/duckai.ts +23 -10
src/duckai.ts CHANGED
@@ -150,19 +150,32 @@ export class DuckAI {
150
  return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
151
  };
152
 
153
- // Ensure document is globally available for eval
154
- (mockWindow as any).document = mockWindow.document;
 
 
 
 
 
155
 
156
- const result = await mockWindow.eval(`
157
- (function() {
158
- const window = this;
159
- const document = this.document;
160
- ${jsScript}
161
- }).call(window)
162
- `) as any;
 
 
 
 
 
163
 
164
  if (!result || !result.client_hashes) {
165
- throw new Error("Invalid Challenge Response");
 
 
 
166
  }
167
 
168
  result.client_hashes[0] = ua;
 
150
  return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
151
  };
152
 
153
+ // --- CONTEXT PREPARATION ---
154
+ // Inject everything directly into the window context to avoid scope issues
155
+ mockWindow.document = mockWindow.document;
156
+ mockWindow.window = mockWindow;
157
+ mockWindow.self = mockWindow;
158
+ mockWindow.parent = mockWindow;
159
+ mockWindow.top = mockWindow;
160
 
161
+ // Immortal DOM patches (already applied above) are good.
162
+
163
+ // Execute script directly in the window context
164
+ // We wrap it in a try-catch block INSIDE the eval to capture script errors
165
+ const result = mockWindow.eval(`
166
+ try {
167
+ ${jsScript}
168
+ } catch(e) {
169
+ console.error("Script Eval Error:", e);
170
+ null;
171
+ }
172
+ `);
173
 
174
  if (!result || !result.client_hashes) {
175
+ // If result is null, maybe the script put the result in a global variable?
176
+ // Sometimes DDG scripts do that. Let's check window.result or similar if needed.
177
+ // But for now, throw detailed error
178
+ throw new Error("Script executed but returned invalid result (no client_hashes)");
179
  }
180
 
181
  result.client_hashes[0] = ua;