Commit 10316371 authored by nextime's avatar nextime

Simplify JavaScript code structure for better reliability

- Removed complex escape sequence handling that caused syntax errors
- Simplified cleanJsonText function to focus on essential HTML entity decoding
- Streamlined malformed JSON handling with cleaner pattern matching
- Reduced nested try-catch complexity for better maintainability
- Maintained core functionality while improving code reliability
parent d5d6c396
...@@ -472,22 +472,16 @@ async def detect_json_response_with_id(page, container_selector, request_id, pro ...@@ -472,22 +472,16 @@ async def detect_json_response_with_id(page, container_selector, request_id, pro
} }
}; };
// Function to clean and decode HTML entities and escape sequences // Function to clean and decode HTML entities and escape sequences - simplified
const cleanJsonText = (text) => { const cleanJsonText = (text) => {
if (!text) return '';
return text return text
// Decode HTML entities
.replace(/"/g, '"') .replace(/"/g, '"')
.replace(/&/g, '&') .replace(/&/g, '&')
.replace(/&lt;/g, '<') .replace(/&lt;/g, '<')
.replace(/&gt;/g, '>') .replace(/&gt;/g, '>')
.replace(/&#x27;/g, "'") .replace(/&#x27;/g, "'")
.replace(/&#x2F;/g, '/') .replace(/&#x2F;/g, '/')
// Handle escaped characters
.replace(/\\\\n/g, '\\n')
.replace(/\\\\t/g, '\\t')
.replace(/\\\\r/g, '\\r')
.replace(/\\\\"/g, '\\"')
// Remove extra whitespace and normalize
.trim(); .trim();
}; };
...@@ -523,32 +517,19 @@ async def detect_json_response_with_id(page, container_selector, request_id, pro ...@@ -523,32 +517,19 @@ async def detect_json_response_with_id(page, container_selector, request_id, pro
return jsonObj.response; return jsonObj.response;
} }
} catch (e) { } catch (e) {
// Try to fix common JSON issues // Try to extract response value from malformed JSON - simplified
try { try {
let fixedJson = match
.replace(/\\\\n/g, '\\n')
.replace(/\\\\t/g, '\\t')
.replace(/\\\\r/g, '\\r')
.replace(/\\\\"/g, '\\"');
// Try to extract just the response value if JSON parsing fails
const responsePattern = /"response"\s*:\s*"([^"]*)"/; const responsePattern = /"response"\s*:\s*"([^"]*)"/;
const responseMatch = fixedJson.match(responsePattern); const responseMatch = match.match(responsePattern);
if (responseMatch && fixedJson.includes(requestId)) { if (responseMatch && match.includes(requestId)) {
const responseValue = responseMatch[1]; const responseValue = responseMatch[1];
if (responseValue) { if (responseValue && responseValue.length > 0) {
console.log(`Extracted response from malformed JSON: ${responseValue.substring(0, 100)}...`); console.log(`Extracted response from malformed JSON: ${responseValue.substring(0, 100)}...`);
return responseValue; return responseValue;
} }
} }
const jsonObj = JSON.parse(fixedJson);
if (jsonObj.id && jsonObj.id.includes(requestId) && jsonObj.response) {
console.log(`Found fixed JSON response with ID: ${jsonObj.id}`);
return jsonObj.response;
}
} catch (e2) { } catch (e2) {
console.log(`Failed to parse JSON: ${match.substring(0, 100)}...`); console.log(`Failed to extract response from JSON: ${match.substring(0, 100)}...`);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment