convertCaptchaWebPostMessagesFromJs function

Map<String, String?> convertCaptchaWebPostMessagesFromJs(
  1. JSAny jsAny
)

Converts a JavaScript message object (JSAny) into a Dart Map.

Returns:

  • {"type": "...", "payload": "..."} if the conversion succeeds
  • {"type": null, "payload": null} if parsing fails or the object does not match JsCaptchaMessage.

Example:

final data = convertCaptchaWebPostMessagesFromJs(jsMessage);
if (data['type'] == 'success') {
  print('Captcha token: ${data['payload']}');
}

Implementation

Map<String, String?> convertCaptchaWebPostMessagesFromJs(JSAny jsAny) {
  try {
    final jsObject = jsAny as JsCaptchaMessage;

    // Safely convert JSString? to Dart String, defaulting to empty string
    final type = jsObject.type?.toDart ?? '';
    final payload = jsObject.payload?.toDart ?? '';

    return {'type': type, 'payload': payload};
  } catch (e) {
    // Fallback in case jsAny isn't a JsCaptchaMessage
    return {'type': null, 'payload': null};
  }
}