* 填写表单(增强版)
(data)
| 261 | * 填写表单(增强版) |
| 262 | */ |
| 263 | function fillForm(data) { |
| 264 | let filledCount = 0; |
| 265 | const results = {}; |
| 266 | |
| 267 | // 先检查是否有全名字段 |
| 268 | const fullNameField = findFullNameField(); |
| 269 | if (fullNameField && data.firstName && data.lastName) { |
| 270 | const fullName = `${data.firstName} ${data.lastName}`; |
| 271 | simulateInput(fullNameField, fullName); |
| 272 | filledCount++; |
| 273 | results['fullName'] = 'filled'; |
| 274 | } |
| 275 | |
| 276 | for (const [fieldName, value] of Object.entries(data)) { |
| 277 | if (!value) continue; |
| 278 | |
| 279 | // address 字段延迟到最后填写,避免被邮编自动填充覆盖 |
| 280 | if (fieldName === 'address') { |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | // 跳过 gender 字段(日本网站通常不需要,且容易误匹配其他 radio) |
| 285 | if (fieldName === 'gender') { |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | // 密码字段需要填写所有匹配项(密码 + 确认密码) |
| 290 | if (fieldName === 'password') { |
| 291 | const elements = findAllFields('password'); |
| 292 | if (elements.length > 0) { |
| 293 | elements.forEach(element => { |
| 294 | simulateInput(element, value); |
| 295 | filledCount++; |
| 296 | }); |
| 297 | results[fieldName] = `filled ${elements.length} field(s)`; |
| 298 | } else { |
| 299 | results[fieldName] = 'not found'; |
| 300 | } |
| 301 | continue; |
| 302 | } |
| 303 | |
| 304 | // 电话字段去掉区号,只填写号码部分 |
| 305 | if (fieldName === 'phone') { |
| 306 | const element = findField(fieldName); |
| 307 | if (element) { |
| 308 | // 根据字段类型决定是否保留格式 |
| 309 | const phoneNumber = value.replace(/^\+\d+\s*/, ''); |
| 310 | simulateInput(element, phoneNumber); |
| 311 | filledCount++; |
| 312 | results[fieldName] = 'filled'; |
| 313 | } else { |
| 314 | results[fieldName] = 'not found'; |
| 315 | } |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | // 性别字段特殊处理(可能是 radio) |
| 320 | if (fieldName === 'gender') { |
no test coverage detected