web-agent-bridge 1.2.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.ar.md +572 -446
- package/README.md +968 -933
- package/bin/agent-runner.js +465 -0
- package/bin/cli.js +138 -80
- package/bin/wab.js +80 -80
- package/examples/bidi-agent.js +119 -119
- package/examples/mcp-agent.js +94 -94
- package/examples/next-app-router/README.md +44 -0
- package/examples/puppeteer-agent.js +108 -108
- package/examples/saas-dashboard/README.md +55 -0
- package/examples/shopify-hydrogen/README.md +74 -0
- package/examples/vision-agent.js +171 -171
- package/examples/wordpress-elementor/README.md +77 -0
- package/package.json +71 -78
- package/public/.well-known/ai-assets.json +59 -0
- package/public/admin/login.html +84 -84
- package/public/ai.html +196 -0
- package/public/cookies.html +208 -208
- package/public/css/premium.css +317 -0
- package/public/css/styles.css +1235 -1235
- package/public/dashboard.html +704 -704
- package/public/demo.html +259 -0
- package/public/docs.html +585 -585
- package/public/feed.xml +89 -0
- package/public/index.html +581 -332
- package/public/js/auth-nav.js +31 -31
- package/public/js/auth-redirect.js +12 -12
- package/public/js/cookie-consent.js +56 -56
- package/public/js/wab-demo-page.js +721 -0
- package/public/js/ws-client.js +74 -74
- package/public/llms-full.txt +309 -0
- package/public/llms.txt +85 -0
- package/public/login.html +83 -83
- package/public/openapi.json +580 -0
- package/public/premium-dashboard.html +2487 -0
- package/public/premium.html +791 -0
- package/public/privacy.html +295 -295
- package/public/register.html +103 -103
- package/public/robots.txt +87 -0
- package/public/script/wab-consent.d.ts +36 -0
- package/public/script/wab-consent.js +104 -0
- package/public/script/wab-schema.js +131 -0
- package/public/script/wab.d.ts +108 -0
- package/public/script/wab.min.js +405 -0
- package/public/sitemap.xml +93 -0
- package/public/sovereign.html +660 -0
- package/public/terms.html +254 -254
- package/public/video/tutorial.mp4 +0 -0
- package/script/ai-agent-bridge.js +1558 -1513
- package/sdk/README.md +55 -55
- package/sdk/index.d.ts +118 -0
- package/sdk/index.js +257 -203
- package/sdk/package.json +14 -14
- package/sdk/schema-discovery.js +83 -0
- package/server/config/secrets.js +94 -92
- package/server/index.js +2 -9
- package/server/middleware/adminAuth.js +30 -30
- package/server/middleware/auth.js +41 -41
- package/server/middleware/rateLimits.js +24 -24
- package/server/migrations/001_add_analytics_indexes.sql +7 -7
- package/server/migrations/002_premium_features.sql +418 -0
- package/server/models/adapters/index.js +33 -33
- package/server/models/adapters/mysql.js +183 -183
- package/server/models/adapters/postgresql.js +172 -172
- package/server/models/adapters/sqlite.js +7 -7
- package/server/models/db.js +561 -561
- package/server/routes/admin-premium.js +671 -0
- package/server/routes/admin.js +247 -247
- package/server/routes/api.js +131 -138
- package/server/routes/auth.js +51 -51
- package/server/routes/billing.js +45 -45
- package/server/routes/discovery.js +406 -329
- package/server/routes/license.js +240 -240
- package/server/routes/noscript.js +543 -543
- package/server/routes/premium-v2.js +686 -0
- package/server/routes/premium.js +724 -0
- package/server/routes/sovereign.js +307 -0
- package/server/routes/wab-api.js +476 -476
- package/server/services/agent-memory.js +625 -0
- package/server/services/email.js +204 -204
- package/server/services/fairness.js +420 -420
- package/server/services/negotiation.js +439 -0
- package/server/services/plugins.js +747 -0
- package/server/services/premium.js +1883 -0
- package/server/services/reputation.js +465 -0
- package/server/services/self-healing.js +843 -0
- package/server/services/stripe.js +192 -192
- package/server/services/swarm.js +788 -0
- package/server/services/verification.js +481 -0
- package/server/services/vision.js +871 -0
- package/server/utils/cache.js +125 -125
- package/server/utils/migrate.js +81 -81
- package/server/utils/secureFields.js +50 -50
- package/server/ws.js +101 -101
- package/templates/artisan-marketplace.yaml +104 -0
- package/templates/book-price-scout.yaml +98 -0
- package/templates/electronics-price-tracker.yaml +108 -0
- package/templates/flight-deal-hunter.yaml +113 -0
- package/templates/freelancer-direct.yaml +116 -0
- package/templates/grocery-price-compare.yaml +93 -0
- package/templates/hotel-direct-booking.yaml +113 -0
- package/templates/local-services.yaml +98 -0
- package/templates/olive-oil-tunisia.yaml +88 -0
- package/templates/organic-farm-fresh.yaml +101 -0
- package/templates/restaurant-direct.yaml +97 -0
- package/docs/DEPLOY.md +0 -118
- package/docs/SPEC.md +0 -1540
- package/wab-mcp-adapter/README.md +0 -136
- package/wab-mcp-adapter/index.js +0 -555
- package/wab-mcp-adapter/package.json +0 -17
|
@@ -1,1513 +1,1558 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Web Agent Bridge
|
|
3
|
-
* Open
|
|
4
|
-
* https://github.com/web-agent-bridge
|
|
5
|
-
* License: MIT
|
|
6
|
-
*/
|
|
7
|
-
(function (global) {
|
|
8
|
-
'use strict';
|
|
9
|
-
|
|
10
|
-
const VERSION = '
|
|
11
|
-
const
|
|
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
|
-
this.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
this.logs.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
this.
|
|
117
|
-
this.
|
|
118
|
-
this.
|
|
119
|
-
this.
|
|
120
|
-
this.
|
|
121
|
-
this.
|
|
122
|
-
this.
|
|
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
|
-
this._escalationAttempts
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (
|
|
181
|
-
if (
|
|
182
|
-
if (
|
|
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
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
for (let i = 0; i <
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
//
|
|
359
|
-
//
|
|
360
|
-
//
|
|
361
|
-
//
|
|
362
|
-
//
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
},
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
const
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
el.dispatchEvent(new MouseEvent('
|
|
397
|
-
await this.delay(
|
|
398
|
-
el.dispatchEvent(new MouseEvent('
|
|
399
|
-
await this.delay(
|
|
400
|
-
el.dispatchEvent(new MouseEvent('
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
el.
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
el.dispatchEvent(new
|
|
416
|
-
el.
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
const
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
return
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
return
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
this.
|
|
571
|
-
this.
|
|
572
|
-
this.
|
|
573
|
-
this.
|
|
574
|
-
this.
|
|
575
|
-
this.
|
|
576
|
-
this.
|
|
577
|
-
this.
|
|
578
|
-
this.
|
|
579
|
-
this.
|
|
580
|
-
this.
|
|
581
|
-
this.
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
this.
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
this.
|
|
608
|
-
this.
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
const
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
const
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
const
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
const
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
}
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
if (
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
}
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
if (
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
}
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
this.
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Web Agent Bridge v2.0.0
|
|
3
|
+
* Open-source middleware for AI agent ↔ website interaction
|
|
4
|
+
* https://github.com/web-agent-bridge
|
|
5
|
+
* License: MIT
|
|
6
|
+
*/
|
|
7
|
+
(function (global) {
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
const VERSION = '2.0.0';
|
|
11
|
+
const LICENSING_SERVER = 'https://api.webagentbridge.com';
|
|
12
|
+
|
|
13
|
+
// ─── Default Configuration ────────────────────────────────────────────
|
|
14
|
+
const DEFAULT_CONFIG = {
|
|
15
|
+
agentPermissions: {
|
|
16
|
+
readContent: true,
|
|
17
|
+
click: true,
|
|
18
|
+
fillForms: false,
|
|
19
|
+
scroll: true,
|
|
20
|
+
navigate: false,
|
|
21
|
+
apiAccess: false,
|
|
22
|
+
automatedLogin: false,
|
|
23
|
+
extractData: false
|
|
24
|
+
},
|
|
25
|
+
restrictions: {
|
|
26
|
+
allowedSelectors: [],
|
|
27
|
+
blockedSelectors: ['.private', '[data-private]', '[data-no-agent]'],
|
|
28
|
+
requireLoginForActions: [],
|
|
29
|
+
rateLimit: { maxCallsPerMinute: 60 }
|
|
30
|
+
},
|
|
31
|
+
logging: {
|
|
32
|
+
enabled: false,
|
|
33
|
+
level: 'basic'
|
|
34
|
+
},
|
|
35
|
+
subscriptionTier: 'free',
|
|
36
|
+
licenseKey: null,
|
|
37
|
+
/** Public site id from dashboard (preferred). Used with configEndpoint for token exchange — license key stays off the page. */
|
|
38
|
+
siteId: null,
|
|
39
|
+
/** Base URL for /api/license/* (verify, track). Default: current origin, else LICENSING_SERVER. */
|
|
40
|
+
apiBaseUrl: null,
|
|
41
|
+
features: {
|
|
42
|
+
advancedAnalytics: false,
|
|
43
|
+
realTimeUpdates: false,
|
|
44
|
+
customActions: false,
|
|
45
|
+
webhooks: false,
|
|
46
|
+
/** Send execute events to POST /api/license/track (populates admin analytics). */
|
|
47
|
+
reportUsage: true
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// ─── Rate Limiter ─────────────────────────────────────────────────────
|
|
52
|
+
class RateLimiter {
|
|
53
|
+
constructor(maxPerMinute) {
|
|
54
|
+
this.maxPerMinute = maxPerMinute;
|
|
55
|
+
this.calls = [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
check() {
|
|
59
|
+
const now = Date.now();
|
|
60
|
+
this.calls = this.calls.filter(t => now - t < 60000);
|
|
61
|
+
if (this.calls.length >= this.maxPerMinute) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
this.calls.push(now);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get remaining() {
|
|
69
|
+
const now = Date.now();
|
|
70
|
+
this.calls = this.calls.filter(t => now - t < 60000);
|
|
71
|
+
return Math.max(0, this.maxPerMinute - this.calls.length);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ─── Logger ───────────────────────────────────────────────────────────
|
|
76
|
+
class BridgeLogger {
|
|
77
|
+
constructor(config) {
|
|
78
|
+
this.enabled = config.enabled;
|
|
79
|
+
this.level = config.level;
|
|
80
|
+
this.logs = [];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
log(action, details, level = 'basic') {
|
|
84
|
+
if (!this.enabled) return;
|
|
85
|
+
if (level === 'detailed' && this.level !== 'detailed') return;
|
|
86
|
+
|
|
87
|
+
const entry = {
|
|
88
|
+
timestamp: new Date().toISOString(),
|
|
89
|
+
action,
|
|
90
|
+
details,
|
|
91
|
+
level
|
|
92
|
+
};
|
|
93
|
+
this.logs.push(entry);
|
|
94
|
+
|
|
95
|
+
if (this.logs.length > 1000) {
|
|
96
|
+
this.logs = this.logs.slice(-500);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
getLogs(filter) {
|
|
101
|
+
if (!filter) return [...this.logs];
|
|
102
|
+
return this.logs.filter(l => l.action === filter);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
clear() {
|
|
106
|
+
this.logs = [];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ─── Security Sandbox ──────────────────────────────────────────────────
|
|
111
|
+
// Isolates the bridge with origin validation, session tokens, and audit log
|
|
112
|
+
class SecuritySandbox {
|
|
113
|
+
constructor(config) {
|
|
114
|
+
this._sessionToken = this._generateToken();
|
|
115
|
+
this._allowedOrigins = config.security?.allowedOrigins || [location.origin];
|
|
116
|
+
this._auditLog = [];
|
|
117
|
+
this._maxAuditEntries = 500;
|
|
118
|
+
this._commandCounter = 0;
|
|
119
|
+
this._blockedCommands = new Set();
|
|
120
|
+
this._escalationAttempts = 0;
|
|
121
|
+
this._maxEscalationAttempts = 5;
|
|
122
|
+
this._locked = false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
_generateToken() {
|
|
126
|
+
const arr = new Uint8Array(32);
|
|
127
|
+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
128
|
+
crypto.getRandomValues(arr);
|
|
129
|
+
} else {
|
|
130
|
+
for (let i = 0; i < 32; i++) arr[i] = Math.floor(Math.random() * 256);
|
|
131
|
+
}
|
|
132
|
+
return Array.from(arr, b => b.toString(16).padStart(2, '0')).join('');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
get sessionToken() { return this._sessionToken; }
|
|
136
|
+
|
|
137
|
+
validateOrigin(origin) {
|
|
138
|
+
if (!origin) return this._allowedOrigins.includes(location.origin);
|
|
139
|
+
return this._allowedOrigins.includes(origin);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
audit(action, details, status = 'ok') {
|
|
143
|
+
const entry = {
|
|
144
|
+
id: ++this._commandCounter,
|
|
145
|
+
timestamp: Date.now(),
|
|
146
|
+
action,
|
|
147
|
+
status,
|
|
148
|
+
fingerprint: details?.agentId || 'anonymous'
|
|
149
|
+
};
|
|
150
|
+
this._auditLog.push(entry);
|
|
151
|
+
if (this._auditLog.length > this._maxAuditEntries) {
|
|
152
|
+
this._auditLog = this._auditLog.slice(-250);
|
|
153
|
+
}
|
|
154
|
+
return entry;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
getAuditLog(limit = 50) {
|
|
158
|
+
return this._auditLog.slice(-limit);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
checkEscalation(requestedTier, currentTier) {
|
|
162
|
+
const tierLevel = { free: 0, starter: 1, pro: 2, enterprise: 3 };
|
|
163
|
+
if ((tierLevel[requestedTier] || 0) > (tierLevel[currentTier] || 0)) {
|
|
164
|
+
this._escalationAttempts++;
|
|
165
|
+
this.audit('escalation_attempt', { requestedTier, currentTier }, 'blocked');
|
|
166
|
+
if (this._escalationAttempts >= this._maxEscalationAttempts) {
|
|
167
|
+
this._locked = true;
|
|
168
|
+
this.audit('bridge_locked', { reason: 'Too many escalation attempts' }, 'critical');
|
|
169
|
+
}
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
get isLocked() { return this._locked; }
|
|
176
|
+
|
|
177
|
+
validateCommand(command) {
|
|
178
|
+
if (this._locked) return { valid: false, error: 'Bridge is locked due to security violations' };
|
|
179
|
+
if (typeof command !== 'object' || !command) return { valid: false, error: 'Invalid command format' };
|
|
180
|
+
if (typeof command.method !== 'string') return { valid: false, error: 'Command method must be a string' };
|
|
181
|
+
if (command.method.length > 200) return { valid: false, error: 'Command method too long' };
|
|
182
|
+
if (this._blockedCommands.has(command.method)) return { valid: false, error: 'Command is blocked' };
|
|
183
|
+
return { valid: true };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ─── Self-Healing Selectors ───────────────────────────────────────────
|
|
188
|
+
// Resilient element resolution for SPAs with dynamic DOM
|
|
189
|
+
class SelfHealingSelector {
|
|
190
|
+
constructor() {
|
|
191
|
+
this._fingerprints = new Map(); // name → element fingerprint
|
|
192
|
+
this._healingStats = { healed: 0, failed: 0 };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fingerprint(el) {
|
|
196
|
+
if (!el) return null;
|
|
197
|
+
return {
|
|
198
|
+
tag: el.tagName.toLowerCase(),
|
|
199
|
+
id: el.id || null,
|
|
200
|
+
classes: Array.from(el.classList),
|
|
201
|
+
text: (el.textContent || '').trim().slice(0, 100),
|
|
202
|
+
ariaLabel: el.getAttribute('aria-label') || null,
|
|
203
|
+
name: el.getAttribute('name') || null,
|
|
204
|
+
type: el.getAttribute('type') || null,
|
|
205
|
+
role: el.getAttribute('role') || null,
|
|
206
|
+
dataTestId: el.getAttribute('data-testid') || null,
|
|
207
|
+
dataWabId: el.getAttribute('data-wab-id') || null,
|
|
208
|
+
href: el.tagName === 'A' ? el.getAttribute('href') : null,
|
|
209
|
+
placeholder: el.getAttribute('placeholder') || null,
|
|
210
|
+
parentTag: el.parentElement ? el.parentElement.tagName.toLowerCase() : null,
|
|
211
|
+
index: el.parentElement ? Array.from(el.parentElement.children).indexOf(el) : -1
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
store(actionName, selector) {
|
|
216
|
+
const el = safeQuerySelector(selector);
|
|
217
|
+
if (el) {
|
|
218
|
+
this._fingerprints.set(actionName, {
|
|
219
|
+
selector,
|
|
220
|
+
fp: this.fingerprint(el),
|
|
221
|
+
storedAt: Date.now()
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
resolve(actionName, originalSelector) {
|
|
227
|
+
// Try original selector first
|
|
228
|
+
const el = safeQuerySelector(originalSelector);
|
|
229
|
+
if (el) return { element: el, selector: originalSelector, healed: false };
|
|
230
|
+
|
|
231
|
+
// Try self-healing
|
|
232
|
+
const stored = this._fingerprints.get(actionName);
|
|
233
|
+
if (!stored || !stored.fp) {
|
|
234
|
+
this._healingStats.failed++;
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const healed = this._heal(stored.fp);
|
|
239
|
+
if (healed) {
|
|
240
|
+
this._healingStats.healed++;
|
|
241
|
+
return healed;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
this._healingStats.failed++;
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
_heal(fp) {
|
|
249
|
+
// Strategy 1: data-wab-id (most stable)
|
|
250
|
+
if (fp.dataWabId) {
|
|
251
|
+
const el = safeQuerySelector(`[data-wab-id="${fp.dataWabId}"]`);
|
|
252
|
+
if (el) return { element: el, selector: `[data-wab-id="${fp.dataWabId}"]`, healed: true, strategy: 'data-wab-id' };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Strategy 2: data-testid
|
|
256
|
+
if (fp.dataTestId) {
|
|
257
|
+
const el = safeQuerySelector(`[data-testid="${fp.dataTestId}"]`);
|
|
258
|
+
if (el) return { element: el, selector: `[data-testid="${fp.dataTestId}"]`, healed: true, strategy: 'data-testid' };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Strategy 3: id (may have changed)
|
|
262
|
+
if (fp.id) {
|
|
263
|
+
const el = safeQuerySelector(`#${CSS.escape(fp.id)}`);
|
|
264
|
+
if (el) return { element: el, selector: `#${CSS.escape(fp.id)}`, healed: true, strategy: 'id' };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Strategy 4: aria-label (semantic, usually stable)
|
|
268
|
+
if (fp.ariaLabel) {
|
|
269
|
+
const sel = `${fp.tag}[aria-label="${CSS.escape(fp.ariaLabel)}"]`;
|
|
270
|
+
const el = safeQuerySelector(sel);
|
|
271
|
+
if (el) return { element: el, selector: sel, healed: true, strategy: 'aria-label' };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Strategy 5: name attribute
|
|
275
|
+
if (fp.name) {
|
|
276
|
+
const sel = `${fp.tag}[name="${CSS.escape(fp.name)}"]`;
|
|
277
|
+
const el = safeQuerySelector(sel);
|
|
278
|
+
if (el) return { element: el, selector: sel, healed: true, strategy: 'name' };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Strategy 6: text content matching (fuzzy)
|
|
282
|
+
if (fp.text && fp.text.length > 0) {
|
|
283
|
+
const candidates = safeQuerySelectorAll(fp.tag);
|
|
284
|
+
const target = fp.text.toLowerCase();
|
|
285
|
+
let bestMatch = null;
|
|
286
|
+
let bestScore = 0;
|
|
287
|
+
|
|
288
|
+
for (const candidate of candidates) {
|
|
289
|
+
const candidateText = (candidate.textContent || '').trim().toLowerCase();
|
|
290
|
+
if (!candidateText) continue;
|
|
291
|
+
|
|
292
|
+
const score = this._textSimilarity(target, candidateText);
|
|
293
|
+
if (score > 0.7 && score > bestScore) {
|
|
294
|
+
bestScore = score;
|
|
295
|
+
bestMatch = candidate;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (bestMatch) {
|
|
300
|
+
const healedSel = this._buildSelectorFor(bestMatch);
|
|
301
|
+
return { element: bestMatch, selector: healedSel, healed: true, strategy: 'text-fuzzy', confidence: bestScore };
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Strategy 7: role + position heuristic
|
|
306
|
+
if (fp.role && fp.parentTag) {
|
|
307
|
+
const candidates = safeQuerySelectorAll(`${fp.parentTag} > ${fp.tag}[role="${fp.role}"]`);
|
|
308
|
+
if (candidates.length > 0 && fp.index >= 0 && fp.index < candidates.length) {
|
|
309
|
+
const el = candidates[fp.index];
|
|
310
|
+
const sel = this._buildSelectorFor(el);
|
|
311
|
+
return { element: el, selector: sel, healed: true, strategy: 'role-position' };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
_textSimilarity(a, b) {
|
|
319
|
+
if (a === b) return 1;
|
|
320
|
+
const longer = a.length > b.length ? a : b;
|
|
321
|
+
const shorter = a.length > b.length ? b : a;
|
|
322
|
+
if (longer.length === 0) return 1;
|
|
323
|
+
if (longer.includes(shorter) || shorter.includes(longer)) {
|
|
324
|
+
return shorter.length / longer.length;
|
|
325
|
+
}
|
|
326
|
+
// Simple bigram similarity
|
|
327
|
+
const bigramsA = new Set();
|
|
328
|
+
for (let i = 0; i < a.length - 1; i++) bigramsA.add(a.slice(i, i + 2));
|
|
329
|
+
let matches = 0;
|
|
330
|
+
for (let i = 0; i < b.length - 1; i++) {
|
|
331
|
+
if (bigramsA.has(b.slice(i, i + 2))) matches++;
|
|
332
|
+
}
|
|
333
|
+
const total = Math.max(bigramsA.size, b.length - 1);
|
|
334
|
+
return total > 0 ? matches / total : 0;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
_buildSelectorFor(el) {
|
|
338
|
+
if (el.id) return `#${CSS.escape(el.id)}`;
|
|
339
|
+
if (el.dataset.wabId) return `[data-wab-id="${el.dataset.wabId}"]`;
|
|
340
|
+
const tag = el.tagName.toLowerCase();
|
|
341
|
+
const attrs = ['data-testid', 'aria-label', 'name'];
|
|
342
|
+
for (const attr of attrs) {
|
|
343
|
+
const val = el.getAttribute(attr);
|
|
344
|
+
if (val && document.querySelectorAll(`${tag}[${attr}="${CSS.escape(val)}"]`).length === 1) {
|
|
345
|
+
return `${tag}[${attr}="${CSS.escape(val)}"]`;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
getStats() {
|
|
352
|
+
return { ...this._healingStats, tracked: this._fingerprints.size };
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ─── Stealth / Human-like Interaction ─────────────────────────────────
|
|
357
|
+
// ⚠️ ETHICAL USE POLICY:
|
|
358
|
+
// Stealth mode simulates human-like interaction patterns for LEGITIMATE uses:
|
|
359
|
+
// - Accessibility testing - QA automation on YOUR OWN sites
|
|
360
|
+
// - UX research with consent - Authorized penetration testing
|
|
361
|
+
// DO NOT use to bypass anti-bot protections on sites you do not own or control.
|
|
362
|
+
// Misuse violates the MIT license terms and is the user's legal responsibility.
|
|
363
|
+
const Stealth = {
|
|
364
|
+
_enabled: false,
|
|
365
|
+
_consentGiven: false,
|
|
366
|
+
|
|
367
|
+
enable(consent) {
|
|
368
|
+
if (consent !== true) {
|
|
369
|
+
console.warn('[WAB] Stealth mode requires explicit consent: stealth.enable(true)');
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
this._consentGiven = true;
|
|
373
|
+
this._enabled = true;
|
|
374
|
+
},
|
|
375
|
+
disable() { this._enabled = false; },
|
|
376
|
+
get isEnabled() { return this._enabled; },
|
|
377
|
+
|
|
378
|
+
// Random delay between min and max ms
|
|
379
|
+
delay(min = 50, max = 300) {
|
|
380
|
+
if (!this._enabled) return Promise.resolve();
|
|
381
|
+
const duration = min + Math.floor(Math.random() * (max - min));
|
|
382
|
+
return new Promise(resolve => setTimeout(resolve, duration));
|
|
383
|
+
},
|
|
384
|
+
|
|
385
|
+
// Simulate a full mouse event chain: mouseover → mouseenter → mousemove → mousedown → mouseup → click
|
|
386
|
+
async simulateClick(el) {
|
|
387
|
+
if (!el) return;
|
|
388
|
+
const rect = el.getBoundingClientRect();
|
|
389
|
+
const x = rect.left + rect.width * (0.3 + Math.random() * 0.4);
|
|
390
|
+
const y = rect.top + rect.height * (0.3 + Math.random() * 0.4);
|
|
391
|
+
const eventOpts = { bubbles: true, cancelable: true, clientX: x, clientY: y, view: window };
|
|
392
|
+
|
|
393
|
+
el.dispatchEvent(new MouseEvent('mouseover', eventOpts));
|
|
394
|
+
el.dispatchEvent(new MouseEvent('mouseenter', { ...eventOpts, bubbles: false }));
|
|
395
|
+
await this.delay(30, 80);
|
|
396
|
+
el.dispatchEvent(new MouseEvent('mousemove', eventOpts));
|
|
397
|
+
await this.delay(40, 120);
|
|
398
|
+
el.dispatchEvent(new MouseEvent('mousedown', eventOpts));
|
|
399
|
+
await this.delay(50, 150);
|
|
400
|
+
el.dispatchEvent(new MouseEvent('mouseup', eventOpts));
|
|
401
|
+
el.dispatchEvent(new MouseEvent('click', eventOpts));
|
|
402
|
+
},
|
|
403
|
+
|
|
404
|
+
// Simulate human-like typing with variable delays
|
|
405
|
+
async simulateTyping(el, text) {
|
|
406
|
+
if (!el) return;
|
|
407
|
+
el.focus();
|
|
408
|
+
el.dispatchEvent(new Event('focus', { bubbles: true }));
|
|
409
|
+
el.value = '';
|
|
410
|
+
|
|
411
|
+
for (let i = 0; i < text.length; i++) {
|
|
412
|
+
const char = text[i];
|
|
413
|
+
el.dispatchEvent(new KeyboardEvent('keydown', { key: char, bubbles: true }));
|
|
414
|
+
el.value += char;
|
|
415
|
+
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
416
|
+
el.dispatchEvent(new KeyboardEvent('keyup', { key: char, bubbles: true }));
|
|
417
|
+
await this.delay(30, 120); // Human typing speed: 30-120ms per key
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
421
|
+
},
|
|
422
|
+
|
|
423
|
+
// Simulate natural scrolling (variable speed, easing)
|
|
424
|
+
async simulateScroll(el, direction = 'down') {
|
|
425
|
+
const target = el || document.documentElement;
|
|
426
|
+
const distance = 300 + Math.floor(Math.random() * 400);
|
|
427
|
+
const steps = 5 + Math.floor(Math.random() * 5);
|
|
428
|
+
const stepSize = distance / steps;
|
|
429
|
+
|
|
430
|
+
for (let i = 0; i < steps; i++) {
|
|
431
|
+
const delta = direction === 'down' ? stepSize : -stepSize;
|
|
432
|
+
if (el && el !== document.documentElement) {
|
|
433
|
+
el.scrollTop += delta;
|
|
434
|
+
} else {
|
|
435
|
+
window.scrollBy(0, delta);
|
|
436
|
+
}
|
|
437
|
+
await this.delay(15, 50);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
// ─── Element Utilities ────────────────────────────────────────────────
|
|
443
|
+
function isElementAllowed(selector, config) {
|
|
444
|
+
const { allowedSelectors, blockedSelectors } = config.restrictions;
|
|
445
|
+
|
|
446
|
+
if (blockedSelectors.length > 0) {
|
|
447
|
+
const el = document.querySelector(selector);
|
|
448
|
+
if (el) {
|
|
449
|
+
for (const blocked of blockedSelectors) {
|
|
450
|
+
if (el.matches(blocked) || el.closest(blocked)) return false;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (allowedSelectors.length > 0) {
|
|
456
|
+
const el = document.querySelector(selector);
|
|
457
|
+
if (el) {
|
|
458
|
+
for (const allowed of allowedSelectors) {
|
|
459
|
+
if (el.matches(allowed) || el.closest(allowed)) return true;
|
|
460
|
+
}
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function safeQuerySelector(selector) {
|
|
469
|
+
try {
|
|
470
|
+
return document.querySelector(selector);
|
|
471
|
+
} catch (e) {
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function safeQuerySelectorAll(selector) {
|
|
477
|
+
try {
|
|
478
|
+
return Array.from(document.querySelectorAll(selector));
|
|
479
|
+
} catch (e) {
|
|
480
|
+
return [];
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// ─── Action Registry ──────────────────────────────────────────────────
|
|
485
|
+
class ActionRegistry {
|
|
486
|
+
constructor() {
|
|
487
|
+
this.actions = new Map();
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
register(action) {
|
|
491
|
+
if (!action.name || !action.description) {
|
|
492
|
+
throw new Error('Action must have a name and description');
|
|
493
|
+
}
|
|
494
|
+
this.actions.set(action.name, {
|
|
495
|
+
name: action.name,
|
|
496
|
+
description: action.description,
|
|
497
|
+
trigger: action.trigger || 'click',
|
|
498
|
+
selector: action.selector || null,
|
|
499
|
+
fields: action.fields || null,
|
|
500
|
+
submitSelector: action.submitSelector || null,
|
|
501
|
+
endpoint: action.endpoint || null,
|
|
502
|
+
method: action.method || 'GET',
|
|
503
|
+
requiresAuth: action.requiresAuth || false,
|
|
504
|
+
category: action.category || 'general',
|
|
505
|
+
params: action.params || null,
|
|
506
|
+
handler: action.handler || null,
|
|
507
|
+
metadata: action.metadata || {}
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
unregister(name) {
|
|
512
|
+
return this.actions.delete(name);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
get(name) {
|
|
516
|
+
return this.actions.get(name) || null;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
list() {
|
|
520
|
+
return Array.from(this.actions.values()).map(a => ({
|
|
521
|
+
name: a.name,
|
|
522
|
+
description: a.description,
|
|
523
|
+
trigger: a.trigger,
|
|
524
|
+
category: a.category,
|
|
525
|
+
requiresAuth: a.requiresAuth,
|
|
526
|
+
params: a.params,
|
|
527
|
+
fields: a.fields ? a.fields.map(f => ({ name: f.name, type: f.type, required: f.required !== false })) : null
|
|
528
|
+
}));
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
getByCategory(category) {
|
|
532
|
+
return this.list().filter(a => a.category === category);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
toJSON() {
|
|
536
|
+
return this.list();
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// ─── Event Emitter ────────────────────────────────────────────────────
|
|
541
|
+
class BridgeEventEmitter {
|
|
542
|
+
constructor() {
|
|
543
|
+
this.listeners = {};
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
on(event, callback) {
|
|
547
|
+
if (!this.listeners[event]) this.listeners[event] = [];
|
|
548
|
+
this.listeners[event].push(callback);
|
|
549
|
+
return () => this.off(event, callback);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
off(event, callback) {
|
|
553
|
+
if (!this.listeners[event]) return;
|
|
554
|
+
this.listeners[event] = this.listeners[event].filter(cb => cb !== callback);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
emit(event, data) {
|
|
558
|
+
if (!this.listeners[event]) return;
|
|
559
|
+
this.listeners[event].forEach(cb => {
|
|
560
|
+
try { cb(data); } catch (e) { console.error(`[WAB] Event handler error for "${event}":`, e); }
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// ─── Main Bridge Class ────────────────────────────────────────────────
|
|
566
|
+
class WebAgentBridge {
|
|
567
|
+
constructor(userConfig) {
|
|
568
|
+
this.config = this._mergeConfig(DEFAULT_CONFIG, userConfig || {});
|
|
569
|
+
this.registry = new ActionRegistry();
|
|
570
|
+
this.rateLimiter = new RateLimiter(this.config.restrictions.rateLimit.maxCallsPerMinute);
|
|
571
|
+
this.logger = new BridgeLogger(this.config.logging);
|
|
572
|
+
this.events = new BridgeEventEmitter();
|
|
573
|
+
this.security = new SecuritySandbox(this.config);
|
|
574
|
+
this.healer = new SelfHealingSelector();
|
|
575
|
+
this.stealth = Stealth;
|
|
576
|
+
this.authenticated = false;
|
|
577
|
+
this.agentInfo = null;
|
|
578
|
+
this._licenseVerified = null;
|
|
579
|
+
this._ready = false;
|
|
580
|
+
this._readyCallbacks = [];
|
|
581
|
+
this._mutationObserver = null;
|
|
582
|
+
|
|
583
|
+
// Enable stealth mode if configured (requires consent: true)
|
|
584
|
+
if (this.config.stealth?.enabled && this.config.stealth?.consent === true) {
|
|
585
|
+
this.stealth.enable(true);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
this._init();
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// ── Initialization ──────────────────────────────────────────────────
|
|
592
|
+
async _init() {
|
|
593
|
+
if (this.config.configEndpoint && (this.config.siteId || this.config._licenseKey || this.config.licenseKey)) {
|
|
594
|
+
await this._secureLicenseExchange();
|
|
595
|
+
} else if (this.config.licenseKey) {
|
|
596
|
+
await this._verifyLicense();
|
|
597
|
+
} else {
|
|
598
|
+
this._licenseVerified = { tier: 'free', valid: true };
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
this._autoDiscoverActions();
|
|
602
|
+
this._storeFingerprints();
|
|
603
|
+
this._setupSPAObserver();
|
|
604
|
+
this._ready = true;
|
|
605
|
+
this._readyCallbacks.forEach(cb => cb());
|
|
606
|
+
this._readyCallbacks = [];
|
|
607
|
+
this.events.emit('ready', { version: VERSION, tier: this.getEffectiveTier() });
|
|
608
|
+
this.logger.log('init', { version: VERSION, tier: this.getEffectiveTier(), security: 'sandbox-active' });
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// Secure license exchange: POST license key to server, get session token back
|
|
612
|
+
// License key is transmitted once via POST (not visible in page source)
|
|
613
|
+
async _secureLicenseExchange() {
|
|
614
|
+
try {
|
|
615
|
+
const endpoint = this.config.configEndpoint;
|
|
616
|
+
const body = this.config.siteId
|
|
617
|
+
? { siteId: this.config.siteId }
|
|
618
|
+
: { licenseKey: this.config._licenseKey || this.config.licenseKey };
|
|
619
|
+
const res = await fetch(endpoint, {
|
|
620
|
+
method: 'POST',
|
|
621
|
+
headers: { 'Content-Type': 'application/json' },
|
|
622
|
+
body: JSON.stringify(body)
|
|
623
|
+
});
|
|
624
|
+
if (res.ok) {
|
|
625
|
+
const data = await res.json();
|
|
626
|
+
this._licenseVerified = { tier: data.tier, valid: true, sessionToken: data.sessionToken };
|
|
627
|
+
delete this.config._licenseKey;
|
|
628
|
+
if (data.expiresIn) {
|
|
629
|
+
if (this._tokenRefreshTimer) clearTimeout(this._tokenRefreshTimer);
|
|
630
|
+
this._tokenRefreshTimer = setTimeout(() => {
|
|
631
|
+
this._secureLicenseExchange();
|
|
632
|
+
}, Math.floor(data.expiresIn * 800));
|
|
633
|
+
}
|
|
634
|
+
} else {
|
|
635
|
+
this._licenseVerified = { tier: 'free', valid: false, error: 'Token exchange failed' };
|
|
636
|
+
}
|
|
637
|
+
} catch (e) {
|
|
638
|
+
this._licenseVerified = { tier: this.config.subscriptionTier || 'free', valid: false, error: 'Offline' };
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// Store fingerprints for all discovered actions (self-healing)
|
|
643
|
+
_storeFingerprints() {
|
|
644
|
+
for (const action of this.registry.actions.values()) {
|
|
645
|
+
if (action.selector) {
|
|
646
|
+
this.healer.store(action.name, action.selector);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
// Watch for SPA DOM changes and re-discover actions
|
|
652
|
+
_setupSPAObserver() {
|
|
653
|
+
if (this._mutationObserver) this._mutationObserver.disconnect();
|
|
654
|
+
|
|
655
|
+
let debounceTimer = null;
|
|
656
|
+
this._mutationObserver = new MutationObserver(() => {
|
|
657
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
658
|
+
debounceTimer = setTimeout(() => {
|
|
659
|
+
this._autoDiscoverActions();
|
|
660
|
+
this._storeFingerprints();
|
|
661
|
+
this.events.emit('dom:changed', { actionsCount: this.registry.actions.size });
|
|
662
|
+
}, 500);
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
this._mutationObserver.observe(document.body, {
|
|
666
|
+
childList: true,
|
|
667
|
+
subtree: true,
|
|
668
|
+
attributes: false
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
onReady(callback) {
|
|
673
|
+
if (this._ready) {
|
|
674
|
+
callback();
|
|
675
|
+
} else {
|
|
676
|
+
this._readyCallbacks.push(callback);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
_mergeConfig(defaults, overrides) {
|
|
681
|
+
const result = {};
|
|
682
|
+
for (const key of Object.keys(defaults)) {
|
|
683
|
+
if (typeof defaults[key] === 'object' && defaults[key] !== null && !Array.isArray(defaults[key])) {
|
|
684
|
+
result[key] = this._mergeConfig(defaults[key], overrides[key] || {});
|
|
685
|
+
} else {
|
|
686
|
+
result[key] = overrides[key] !== undefined ? overrides[key] : defaults[key];
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
for (const key of Object.keys(overrides)) {
|
|
690
|
+
if (!(key in defaults)) {
|
|
691
|
+
result[key] = overrides[key];
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return result;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
_getLicenseApiBase() {
|
|
698
|
+
const c = this.config;
|
|
699
|
+
if (c.apiBaseUrl) return String(c.apiBaseUrl).replace(/\/$/, '');
|
|
700
|
+
if (typeof global.location !== 'undefined' && location && location.origin) return location.origin;
|
|
701
|
+
return LICENSING_SERVER;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* Report action execution to WAB (fills platform analytics in admin).
|
|
706
|
+
*/
|
|
707
|
+
_maybeReportUsage(actionName, triggerType, success) {
|
|
708
|
+
try {
|
|
709
|
+
const sessionToken = this._licenseVerified && this._licenseVerified.sessionToken;
|
|
710
|
+
if (!sessionToken || (this.config.features && this.config.features.reportUsage === false)) return;
|
|
711
|
+
const base = this._getLicenseApiBase();
|
|
712
|
+
const payload = JSON.stringify({
|
|
713
|
+
sessionToken,
|
|
714
|
+
actionName,
|
|
715
|
+
triggerType: triggerType || 'unknown',
|
|
716
|
+
success: success !== false
|
|
717
|
+
});
|
|
718
|
+
fetch(`${base}/api/license/track`, {
|
|
719
|
+
method: 'POST',
|
|
720
|
+
headers: { 'Content-Type': 'application/json' },
|
|
721
|
+
body: payload,
|
|
722
|
+
keepalive: true,
|
|
723
|
+
mode: 'cors',
|
|
724
|
+
credentials: 'omit'
|
|
725
|
+
}).catch(function () {});
|
|
726
|
+
} catch (e) { /* ignore */ }
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
// ── License Verification ────────────────────────────────────────────
|
|
730
|
+
async _verifyLicense() {
|
|
731
|
+
try {
|
|
732
|
+
const res = await fetch(`${this._getLicenseApiBase()}/api/license/verify`, {
|
|
733
|
+
method: 'POST',
|
|
734
|
+
headers: { 'Content-Type': 'application/json' },
|
|
735
|
+
body: JSON.stringify({
|
|
736
|
+
domain: location.hostname,
|
|
737
|
+
licenseKey: this.config.licenseKey
|
|
738
|
+
})
|
|
739
|
+
});
|
|
740
|
+
if (res.ok) {
|
|
741
|
+
this._licenseVerified = await res.json();
|
|
742
|
+
} else {
|
|
743
|
+
this._licenseVerified = { tier: 'free', valid: false, error: 'License verification failed' };
|
|
744
|
+
}
|
|
745
|
+
} catch (e) {
|
|
746
|
+
this._licenseVerified = { tier: this.config.subscriptionTier || 'free', valid: false, error: 'Offline' };
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
getEffectiveTier() {
|
|
751
|
+
if (this._licenseVerified && this._licenseVerified.valid) {
|
|
752
|
+
return this._licenseVerified.tier;
|
|
753
|
+
}
|
|
754
|
+
return 'free';
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// ── Auto-discover Page Actions ──────────────────────────────────────
|
|
758
|
+
_autoDiscoverActions() {
|
|
759
|
+
const buttons = safeQuerySelectorAll('button, [role="button"], input[type="submit"], a.btn, a.button');
|
|
760
|
+
buttons.forEach((el, i) => {
|
|
761
|
+
const text = (el.textContent || el.value || '').trim();
|
|
762
|
+
if (!text) return;
|
|
763
|
+
|
|
764
|
+
const selector = this._generateSelector(el);
|
|
765
|
+
if (!selector || !isElementAllowed(selector, this.config)) return;
|
|
766
|
+
|
|
767
|
+
const name = this._slugify(text) || `action_${i}`;
|
|
768
|
+
if (!this.registry.get(name)) {
|
|
769
|
+
this.registry.register({
|
|
770
|
+
name,
|
|
771
|
+
description: `Click: ${text}`,
|
|
772
|
+
trigger: 'click',
|
|
773
|
+
selector,
|
|
774
|
+
category: 'auto-discovered'
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
const forms = safeQuerySelectorAll('form');
|
|
780
|
+
forms.forEach((form, i) => {
|
|
781
|
+
const formSelector = this._generateSelector(form);
|
|
782
|
+
if (!formSelector || !isElementAllowed(formSelector, this.config)) return;
|
|
783
|
+
|
|
784
|
+
const fields = Array.from(form.querySelectorAll('input, textarea, select'))
|
|
785
|
+
.filter(f => f.type !== 'hidden' && f.type !== 'submit')
|
|
786
|
+
.map(f => ({
|
|
787
|
+
name: f.name || f.id || f.placeholder || `field_${Math.random().toString(36).slice(2, 6)}`,
|
|
788
|
+
selector: this._generateSelector(f),
|
|
789
|
+
type: f.type || 'text',
|
|
790
|
+
required: f.required,
|
|
791
|
+
placeholder: f.placeholder || ''
|
|
792
|
+
}));
|
|
793
|
+
|
|
794
|
+
const submitBtn = form.querySelector('[type="submit"], button:not([type])');
|
|
795
|
+
const formName = form.id || form.name || `form_${i}`;
|
|
796
|
+
|
|
797
|
+
this.registry.register({
|
|
798
|
+
name: `fill_${this._slugify(formName)}`,
|
|
799
|
+
description: `Fill and submit form: ${formName}`,
|
|
800
|
+
trigger: 'fill_and_submit',
|
|
801
|
+
selector: formSelector,
|
|
802
|
+
fields,
|
|
803
|
+
submitSelector: submitBtn ? this._generateSelector(submitBtn) : null,
|
|
804
|
+
category: 'auto-discovered'
|
|
805
|
+
});
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
const links = safeQuerySelectorAll('nav a, [role="navigation"] a');
|
|
809
|
+
links.forEach((el, i) => {
|
|
810
|
+
const text = (el.textContent || '').trim();
|
|
811
|
+
if (!text) return;
|
|
812
|
+
|
|
813
|
+
const selector = this._generateSelector(el);
|
|
814
|
+
if (!selector || !isElementAllowed(selector, this.config)) return;
|
|
815
|
+
|
|
816
|
+
const name = `nav_${this._slugify(text)}` || `nav_${i}`;
|
|
817
|
+
if (!this.registry.get(name)) {
|
|
818
|
+
this.registry.register({
|
|
819
|
+
name,
|
|
820
|
+
description: `Navigate: ${text}`,
|
|
821
|
+
trigger: 'click',
|
|
822
|
+
selector,
|
|
823
|
+
category: 'navigation'
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
this._autoDiscoverCommerceAndBookingActions();
|
|
829
|
+
this._autoDiscoverStructuredActions();
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
_autoDiscoverCommerceAndBookingActions() {
|
|
833
|
+
const clickable = safeQuerySelectorAll('button, [role="button"], a, input[type="submit"], input[type="button"]');
|
|
834
|
+
clickable.forEach((el, i) => {
|
|
835
|
+
const text = (el.textContent || el.value || el.getAttribute('aria-label') || '').trim();
|
|
836
|
+
if (!text) return;
|
|
837
|
+
|
|
838
|
+
const intent = this._inferActionIntent(text);
|
|
839
|
+
if (!intent) return;
|
|
840
|
+
|
|
841
|
+
const selector = this._generateSelector(el);
|
|
842
|
+
if (!selector || !isElementAllowed(selector, this.config)) return;
|
|
843
|
+
|
|
844
|
+
const name = `${intent}_${this._slugify(text) || i}`;
|
|
845
|
+
if (this.registry.get(name)) return;
|
|
846
|
+
|
|
847
|
+
this.registry.register({
|
|
848
|
+
name,
|
|
849
|
+
description: `${intent.replace(/_/g, ' ')}: ${text}`,
|
|
850
|
+
trigger: 'click',
|
|
851
|
+
selector,
|
|
852
|
+
category: intent.startsWith('book') ? 'booking' : 'commerce',
|
|
853
|
+
metadata: { discoveredBy: 'intent' }
|
|
854
|
+
});
|
|
855
|
+
});
|
|
856
|
+
|
|
857
|
+
const forms = safeQuerySelectorAll('form');
|
|
858
|
+
forms.forEach((form, i) => {
|
|
859
|
+
const formText = `${form.id || ''} ${form.name || ''} ${form.className || ''} ${(form.getAttribute('aria-label') || '')}`.toLowerCase();
|
|
860
|
+
const hasBookingSignals = /(book|reservation|reserve|appointment|schedule)/.test(formText)
|
|
861
|
+
|| !!form.querySelector('input[type="date"], input[type="time"], input[name*="date" i], input[name*="time" i]');
|
|
862
|
+
if (!hasBookingSignals) return;
|
|
863
|
+
|
|
864
|
+
const formSelector = this._generateSelector(form);
|
|
865
|
+
if (!formSelector || !isElementAllowed(formSelector, this.config)) return;
|
|
866
|
+
|
|
867
|
+
const fields = Array.from(form.querySelectorAll('input, textarea, select'))
|
|
868
|
+
.filter(f => f.type !== 'hidden' && f.type !== 'submit')
|
|
869
|
+
.map(f => ({
|
|
870
|
+
name: f.name || f.id || f.placeholder || `field_${Math.random().toString(36).slice(2, 6)}`,
|
|
871
|
+
selector: this._generateSelector(f),
|
|
872
|
+
type: f.type || 'text',
|
|
873
|
+
required: f.required,
|
|
874
|
+
placeholder: f.placeholder || ''
|
|
875
|
+
}));
|
|
876
|
+
|
|
877
|
+
const submitBtn = form.querySelector('[type="submit"], button:not([type])');
|
|
878
|
+
const actionName = `book_${this._slugify(form.id || form.name || `form_${i}`)}`;
|
|
879
|
+
if (this.registry.get(actionName)) return;
|
|
880
|
+
|
|
881
|
+
this.registry.register({
|
|
882
|
+
name: actionName,
|
|
883
|
+
description: `Book or reserve via form: ${form.id || form.name || `form_${i}`}`,
|
|
884
|
+
trigger: 'fill_and_submit',
|
|
885
|
+
selector: formSelector,
|
|
886
|
+
fields,
|
|
887
|
+
submitSelector: submitBtn ? this._generateSelector(submitBtn) : null,
|
|
888
|
+
category: 'booking',
|
|
889
|
+
metadata: { discoveredBy: 'booking-form-signals' }
|
|
890
|
+
});
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
_autoDiscoverStructuredActions() {
|
|
895
|
+
const products = this._scanStructuredProducts();
|
|
896
|
+
if (!products.length) return;
|
|
897
|
+
|
|
898
|
+
if (!this.registry.get('get_structured_products')) {
|
|
899
|
+
this.registry.register({
|
|
900
|
+
name: 'get_structured_products',
|
|
901
|
+
description: 'Return product entities discovered from schema.org and microdata',
|
|
902
|
+
trigger: 'read',
|
|
903
|
+
category: 'structured-data',
|
|
904
|
+
params: [
|
|
905
|
+
{ name: 'limit', type: 'number', required: false }
|
|
906
|
+
],
|
|
907
|
+
handler: (params) => {
|
|
908
|
+
const limit = Number(params?.limit || products.length);
|
|
909
|
+
const safeLimit = Number.isFinite(limit) && limit > 0 ? Math.min(limit, products.length) : products.length;
|
|
910
|
+
return { success: true, count: products.length, products: products.slice(0, safeLimit) };
|
|
911
|
+
},
|
|
912
|
+
metadata: { discoveredBy: 'schema' }
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
if (!this.registry.get('get_structured_prices')) {
|
|
917
|
+
this.registry.register({
|
|
918
|
+
name: 'get_structured_prices',
|
|
919
|
+
description: 'Return normalized offer prices discovered from structured product data',
|
|
920
|
+
trigger: 'read',
|
|
921
|
+
category: 'structured-data',
|
|
922
|
+
handler: () => {
|
|
923
|
+
const prices = products
|
|
924
|
+
.map(p => ({
|
|
925
|
+
name: p.name || null,
|
|
926
|
+
sku: p.sku || null,
|
|
927
|
+
price: p.price || null,
|
|
928
|
+
priceCurrency: p.priceCurrency || null,
|
|
929
|
+
availability: p.availability || null,
|
|
930
|
+
source: p.source
|
|
931
|
+
}))
|
|
932
|
+
.filter(p => p.price != null || p.availability != null);
|
|
933
|
+
return { success: true, count: prices.length, offers: prices };
|
|
934
|
+
},
|
|
935
|
+
metadata: { discoveredBy: 'schema' }
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
_scanStructuredProducts() {
|
|
941
|
+
const products = [];
|
|
942
|
+
|
|
943
|
+
const jsonLdScripts = safeQuerySelectorAll('script[type="application/ld+json"]');
|
|
944
|
+
jsonLdScripts.forEach((script, idx) => {
|
|
945
|
+
const text = script.textContent;
|
|
946
|
+
if (!text || !text.trim()) return;
|
|
947
|
+
try {
|
|
948
|
+
const parsed = JSON.parse(text);
|
|
949
|
+
const graph = Array.isArray(parsed) ? parsed : (Array.isArray(parsed?.['@graph']) ? parsed['@graph'] : [parsed]);
|
|
950
|
+
graph.forEach(node => {
|
|
951
|
+
if (!node || typeof node !== 'object') return;
|
|
952
|
+
const types = Array.isArray(node['@type']) ? node['@type'] : [node['@type']];
|
|
953
|
+
if (!types.includes('Product')) return;
|
|
954
|
+
const offer = Array.isArray(node.offers) ? node.offers[0] : node.offers;
|
|
955
|
+
products.push({
|
|
956
|
+
name: node.name || null,
|
|
957
|
+
sku: node.sku || null,
|
|
958
|
+
price: offer?.price || null,
|
|
959
|
+
priceCurrency: offer?.priceCurrency || null,
|
|
960
|
+
availability: offer?.availability || null,
|
|
961
|
+
source: `jsonld:${idx}`
|
|
962
|
+
});
|
|
963
|
+
});
|
|
964
|
+
} catch (e) {
|
|
965
|
+
// Ignore invalid JSON-LD blocks.
|
|
966
|
+
}
|
|
967
|
+
});
|
|
968
|
+
|
|
969
|
+
const microProducts = safeQuerySelectorAll('[itemtype*="schema.org/Product"]');
|
|
970
|
+
microProducts.forEach((el, idx) => {
|
|
971
|
+
const readProp = (prop) => {
|
|
972
|
+
const propEl = el.querySelector(`[itemprop="${prop}"]`);
|
|
973
|
+
if (!propEl) return null;
|
|
974
|
+
if (propEl.content) return propEl.content;
|
|
975
|
+
if (propEl.getAttribute('content')) return propEl.getAttribute('content');
|
|
976
|
+
if (propEl.value) return propEl.value;
|
|
977
|
+
return (propEl.textContent || '').trim() || null;
|
|
978
|
+
};
|
|
979
|
+
|
|
980
|
+
const offerRoot = el.querySelector('[itemprop="offers"]');
|
|
981
|
+
const readOfferProp = (prop) => {
|
|
982
|
+
if (!offerRoot) return null;
|
|
983
|
+
const node = offerRoot.querySelector(`[itemprop="${prop}"]`);
|
|
984
|
+
if (!node) return null;
|
|
985
|
+
return node.getAttribute('content') || node.content || (node.textContent || '').trim() || null;
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
products.push({
|
|
989
|
+
name: readProp('name'),
|
|
990
|
+
sku: readProp('sku'),
|
|
991
|
+
price: readOfferProp('price') || readProp('price'),
|
|
992
|
+
priceCurrency: readOfferProp('priceCurrency'),
|
|
993
|
+
availability: readOfferProp('availability'),
|
|
994
|
+
source: `microdata:${idx}`
|
|
995
|
+
});
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
const unique = [];
|
|
999
|
+
const seen = new Set();
|
|
1000
|
+
for (const item of products) {
|
|
1001
|
+
const key = `${item.name || ''}|${item.sku || ''}|${item.price || ''}|${item.source}`;
|
|
1002
|
+
if (seen.has(key)) continue;
|
|
1003
|
+
seen.add(key);
|
|
1004
|
+
unique.push(item);
|
|
1005
|
+
}
|
|
1006
|
+
return unique;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
_inferActionIntent(text) {
|
|
1010
|
+
const t = String(text || '').toLowerCase();
|
|
1011
|
+
if (!t) return null;
|
|
1012
|
+
if (/(add\s*to\s*cart|buy\s*now|add\s*bag|add\s*basket)/.test(t)) return 'add_to_cart';
|
|
1013
|
+
if (/(checkout|proceed\s*to\s*checkout|pay\s*now)/.test(t)) return 'checkout';
|
|
1014
|
+
if (/(book\s*now|reserve|reservation|schedule|appointment)/.test(t)) return 'book_now';
|
|
1015
|
+
if (/(get\s*price|check\s*price|view\s*price)/.test(t)) return 'get_price';
|
|
1016
|
+
return null;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
_generateSelector(el) {
|
|
1020
|
+
if (el.id) return `#${CSS.escape(el.id)}`;
|
|
1021
|
+
if (el.dataset.wabId) return `[data-wab-id="${el.dataset.wabId}"]`;
|
|
1022
|
+
|
|
1023
|
+
const classes = Array.from(el.classList).filter(c => !c.match(/^(js-|is-|has-)/));
|
|
1024
|
+
if (classes.length > 0) {
|
|
1025
|
+
const sel = `${el.tagName.toLowerCase()}.${classes.map(c => CSS.escape(c)).join('.')}`;
|
|
1026
|
+
if (document.querySelectorAll(sel).length === 1) return sel;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
const attrs = ['name', 'data-testid', 'aria-label', 'title', 'role'];
|
|
1030
|
+
for (const attr of attrs) {
|
|
1031
|
+
const val = el.getAttribute(attr);
|
|
1032
|
+
if (val) {
|
|
1033
|
+
const sel = `${el.tagName.toLowerCase()}[${attr}="${CSS.escape(val)}"]`;
|
|
1034
|
+
if (document.querySelectorAll(sel).length === 1) return sel;
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
const path = [];
|
|
1039
|
+
let current = el;
|
|
1040
|
+
while (current && current !== document.body) {
|
|
1041
|
+
let tag = current.tagName.toLowerCase();
|
|
1042
|
+
if (current.id) {
|
|
1043
|
+
path.unshift(`#${CSS.escape(current.id)}`);
|
|
1044
|
+
break;
|
|
1045
|
+
}
|
|
1046
|
+
const parent = current.parentElement;
|
|
1047
|
+
if (parent) {
|
|
1048
|
+
const siblings = Array.from(parent.children).filter(c => c.tagName === current.tagName);
|
|
1049
|
+
if (siblings.length > 1) {
|
|
1050
|
+
tag += `:nth-of-type(${siblings.indexOf(current) + 1})`;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
path.unshift(tag);
|
|
1054
|
+
current = parent;
|
|
1055
|
+
}
|
|
1056
|
+
return path.join(' > ');
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
_slugify(text) {
|
|
1060
|
+
return text
|
|
1061
|
+
.toLowerCase()
|
|
1062
|
+
.replace(/[^\w\s-]/g, '')
|
|
1063
|
+
.replace(/[\s_]+/g, '_')
|
|
1064
|
+
.replace(/^-+|-+$/g, '')
|
|
1065
|
+
.slice(0, 40);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
// ── Agent Authentication ────────────────────────────────────────────
|
|
1069
|
+
authenticate(agentKey, agentMeta = {}) {
|
|
1070
|
+
this.logger.log('authenticate', { agentKey: agentKey ? '***' : null });
|
|
1071
|
+
this.events.emit('agent:authenticate', { agentMeta });
|
|
1072
|
+
this.authenticated = true;
|
|
1073
|
+
this.agentInfo = { key: agentKey, ...agentMeta, authenticatedAt: new Date().toISOString() };
|
|
1074
|
+
return { success: true, permissions: this._getEffectivePermissions() };
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
// ── Permission Checks ───────────────────────────────────────────────
|
|
1078
|
+
_getEffectivePermissions() {
|
|
1079
|
+
const perms = { ...this.config.agentPermissions };
|
|
1080
|
+
const tier = this.getEffectiveTier();
|
|
1081
|
+
|
|
1082
|
+
if (tier === 'free') {
|
|
1083
|
+
perms.apiAccess = false;
|
|
1084
|
+
perms.automatedLogin = false;
|
|
1085
|
+
perms.extractData = false;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
return perms;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
_checkPermission(action) {
|
|
1092
|
+
const perms = this._getEffectivePermissions();
|
|
1093
|
+
|
|
1094
|
+
switch (action) {
|
|
1095
|
+
case 'click': return perms.click;
|
|
1096
|
+
case 'fill_and_submit': return perms.fillForms;
|
|
1097
|
+
case 'scroll': return perms.scroll;
|
|
1098
|
+
case 'navigate': return perms.navigate;
|
|
1099
|
+
case 'api': return perms.apiAccess;
|
|
1100
|
+
case 'read': return perms.readContent;
|
|
1101
|
+
case 'extract': return perms.extractData;
|
|
1102
|
+
default: return perms.click;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
// ── Core: Execute Action ────────────────────────────────────────────
|
|
1107
|
+
async execute(actionName, params = {}) {
|
|
1108
|
+
// Security: check if bridge is locked
|
|
1109
|
+
if (this.security.isLocked) {
|
|
1110
|
+
return { success: false, error: 'Bridge locked due to security violations' };
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
if (!this.rateLimiter.check()) {
|
|
1114
|
+
const error = { success: false, error: 'Rate limit exceeded', retryAfter: 60 };
|
|
1115
|
+
this.logger.log('rate_limit', { action: actionName });
|
|
1116
|
+
this.security.audit('rate_limit', { action: actionName }, 'blocked');
|
|
1117
|
+
this.events.emit('error', error);
|
|
1118
|
+
return error;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
const action = this.registry.get(actionName);
|
|
1122
|
+
if (!action) {
|
|
1123
|
+
return { success: false, error: `Action "${actionName}" not found`, available: this.registry.list().map(a => a.name) };
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
if (!this._checkPermission(action.trigger)) {
|
|
1127
|
+
this.security.audit('permission_denied', { action: actionName, trigger: action.trigger }, 'blocked');
|
|
1128
|
+
return { success: false, error: `Permission denied for trigger type: ${action.trigger}`, tier: this.getEffectiveTier() };
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
if (action.requiresAuth && !this.authenticated) {
|
|
1132
|
+
return { success: false, error: 'Authentication required for this action' };
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
const loginRequired = this.config.restrictions.requireLoginForActions || [];
|
|
1136
|
+
if (loginRequired.includes(action.trigger) && !this.authenticated) {
|
|
1137
|
+
return { success: false, error: 'Login required for this action type' };
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// Self-healing: resolve selector if original is broken
|
|
1141
|
+
if (action.selector) {
|
|
1142
|
+
const resolved = this.healer.resolve(actionName, action.selector);
|
|
1143
|
+
if (resolved && resolved.healed) {
|
|
1144
|
+
action.selector = resolved.selector;
|
|
1145
|
+
this.logger.log('self_heal', { action: actionName, strategy: resolved.strategy, confidence: resolved.confidence }, 'detailed');
|
|
1146
|
+
this.events.emit('selector:healed', { action: actionName, strategy: resolved.strategy });
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
this.logger.log('execute', { action: actionName, params }, 'basic');
|
|
1151
|
+
this.security.audit('execute', { action: actionName, agentId: this.agentInfo?.key });
|
|
1152
|
+
this.events.emit('action:before', { action: actionName, params });
|
|
1153
|
+
|
|
1154
|
+
try {
|
|
1155
|
+
let result;
|
|
1156
|
+
|
|
1157
|
+
if (action.handler) {
|
|
1158
|
+
result = await action.handler(params);
|
|
1159
|
+
} else {
|
|
1160
|
+
switch (action.trigger) {
|
|
1161
|
+
case 'click':
|
|
1162
|
+
result = await this._executeClick(action);
|
|
1163
|
+
break;
|
|
1164
|
+
case 'fill_and_submit':
|
|
1165
|
+
result = await this._executeFillAndSubmit(action, params);
|
|
1166
|
+
break;
|
|
1167
|
+
case 'scroll':
|
|
1168
|
+
result = await this._executeScroll(action);
|
|
1169
|
+
break;
|
|
1170
|
+
case 'api':
|
|
1171
|
+
result = await this._executeApi(action, params);
|
|
1172
|
+
break;
|
|
1173
|
+
default:
|
|
1174
|
+
result = { success: false, error: `Unknown trigger: ${action.trigger}` };
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
this.events.emit('action:after', { action: actionName, result });
|
|
1179
|
+
this.logger.log('execute_result', { action: actionName, success: result.success }, 'detailed');
|
|
1180
|
+
this._maybeReportUsage(actionName, action.trigger, !!(result && result.success !== false));
|
|
1181
|
+
return result;
|
|
1182
|
+
|
|
1183
|
+
} catch (err) {
|
|
1184
|
+
const error = { success: false, error: err.message };
|
|
1185
|
+
this.events.emit('error', { action: actionName, error: err.message });
|
|
1186
|
+
this.logger.log('execute_error', { action: actionName, error: err.message });
|
|
1187
|
+
this._maybeReportUsage(actionName, action.trigger, false);
|
|
1188
|
+
return error;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
async _executeClick(action) {
|
|
1193
|
+
if (!action.selector) return { success: false, error: 'No selector defined' };
|
|
1194
|
+
if (!isElementAllowed(action.selector, this.config)) {
|
|
1195
|
+
return { success: false, error: 'Element is blocked by restrictions' };
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// Self-healing: try to find element even if selector is stale
|
|
1199
|
+
let el = safeQuerySelector(action.selector);
|
|
1200
|
+
if (!el) {
|
|
1201
|
+
const resolved = this.healer.resolve(action.name, action.selector);
|
|
1202
|
+
if (resolved) {
|
|
1203
|
+
el = resolved.element;
|
|
1204
|
+
} else {
|
|
1205
|
+
return { success: false, error: `Element not found: ${action.selector}` };
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
// Stealth: use human-like click simulation
|
|
1210
|
+
if (this.stealth.isEnabled) {
|
|
1211
|
+
await this.stealth.delay(100, 400);
|
|
1212
|
+
await this.stealth.simulateClick(el);
|
|
1213
|
+
} else {
|
|
1214
|
+
el.click();
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
return { success: true, action: 'click', selector: action.selector };
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
async _executeFillAndSubmit(action, params) {
|
|
1221
|
+
if (!action.fields) return { success: false, error: 'No fields defined' };
|
|
1222
|
+
|
|
1223
|
+
const results = [];
|
|
1224
|
+
for (const field of action.fields) {
|
|
1225
|
+
if (!isElementAllowed(field.selector, this.config)) {
|
|
1226
|
+
results.push({ field: field.name, success: false, error: 'Element blocked' });
|
|
1227
|
+
continue;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
let el = safeQuerySelector(field.selector);
|
|
1231
|
+
if (!el) {
|
|
1232
|
+
// Self-healing: try to find field by name/placeholder
|
|
1233
|
+
const healedField = this.healer.resolve(`field_${field.name}`, field.selector);
|
|
1234
|
+
if (healedField) {
|
|
1235
|
+
el = healedField.element;
|
|
1236
|
+
} else {
|
|
1237
|
+
results.push({ field: field.name, success: false, error: 'Element not found' });
|
|
1238
|
+
continue;
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
const value = params[field.name];
|
|
1243
|
+
if (value !== undefined) {
|
|
1244
|
+
if (this.stealth.isEnabled) {
|
|
1245
|
+
await this.stealth.delay(200, 600);
|
|
1246
|
+
await this.stealth.simulateTyping(el, String(value));
|
|
1247
|
+
} else {
|
|
1248
|
+
el.focus();
|
|
1249
|
+
el.value = value;
|
|
1250
|
+
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
1251
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
1252
|
+
}
|
|
1253
|
+
results.push({ field: field.name, success: true });
|
|
1254
|
+
} else if (field.required !== false) {
|
|
1255
|
+
results.push({ field: field.name, success: false, error: 'Value required but not provided' });
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
if (action.submitSelector) {
|
|
1260
|
+
let submitEl = safeQuerySelector(action.submitSelector);
|
|
1261
|
+
if (submitEl) {
|
|
1262
|
+
if (this.stealth.isEnabled) {
|
|
1263
|
+
await this.stealth.delay(300, 800);
|
|
1264
|
+
await this.stealth.simulateClick(submitEl);
|
|
1265
|
+
} else {
|
|
1266
|
+
submitEl.click();
|
|
1267
|
+
}
|
|
1268
|
+
results.push({ field: '_submit', success: true });
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
return { success: results.every(r => r.success), results };
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
async _executeScroll(action) {
|
|
1276
|
+
if (action.selector) {
|
|
1277
|
+
const el = safeQuerySelector(action.selector);
|
|
1278
|
+
if (el) {
|
|
1279
|
+
if (this.stealth.isEnabled) {
|
|
1280
|
+
await this.stealth.simulateScroll(el);
|
|
1281
|
+
} else {
|
|
1282
|
+
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
1283
|
+
}
|
|
1284
|
+
return { success: true, action: 'scroll', selector: action.selector };
|
|
1285
|
+
}
|
|
1286
|
+
return { success: false, error: `Element not found: ${action.selector}` };
|
|
1287
|
+
}
|
|
1288
|
+
if (this.stealth.isEnabled) {
|
|
1289
|
+
await this.stealth.simulateScroll(null, 'down');
|
|
1290
|
+
} else {
|
|
1291
|
+
window.scrollBy({ top: 500, behavior: 'smooth' });
|
|
1292
|
+
}
|
|
1293
|
+
return { success: true, action: 'scroll', direction: 'down' };
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
async _executeApi(action, params) {
|
|
1297
|
+
if (this.getEffectiveTier() === 'free') {
|
|
1298
|
+
return { success: false, error: 'API access requires a premium subscription' };
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
const url = new URL(action.endpoint, location.origin);
|
|
1302
|
+
const options = { method: action.method || 'GET', headers: { 'Content-Type': 'application/json' } };
|
|
1303
|
+
|
|
1304
|
+
if (action.method !== 'GET' && params) {
|
|
1305
|
+
options.body = JSON.stringify(params);
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
const res = await fetch(url.toString(), options);
|
|
1309
|
+
const data = await res.json().catch(() => null);
|
|
1310
|
+
return { success: res.ok, status: res.status, data };
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
// ── Content Reading ─────────────────────────────────────────────────
|
|
1314
|
+
readContent(selector) {
|
|
1315
|
+
if (!this._checkPermission('read')) {
|
|
1316
|
+
return { success: false, error: 'readContent permission denied' };
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
if (!isElementAllowed(selector, this.config)) {
|
|
1320
|
+
return { success: false, error: 'Element is blocked by restrictions' };
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
const el = safeQuerySelector(selector);
|
|
1324
|
+
if (!el) return { success: false, error: 'Element not found' };
|
|
1325
|
+
|
|
1326
|
+
return {
|
|
1327
|
+
success: true,
|
|
1328
|
+
text: el.textContent.trim(),
|
|
1329
|
+
html: el.innerHTML,
|
|
1330
|
+
attributes: Object.fromEntries(
|
|
1331
|
+
Array.from(el.attributes).map(a => [a.name, a.value])
|
|
1332
|
+
)
|
|
1333
|
+
};
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
getPageInfo() {
|
|
1337
|
+
return {
|
|
1338
|
+
title: document.title,
|
|
1339
|
+
url: location.href,
|
|
1340
|
+
domain: location.hostname,
|
|
1341
|
+
lang: document.documentElement.lang || 'unknown',
|
|
1342
|
+
bridgeVersion: VERSION,
|
|
1343
|
+
tier: this.getEffectiveTier(),
|
|
1344
|
+
permissions: this._getEffectivePermissions(),
|
|
1345
|
+
actionsCount: this.registry.actions.size,
|
|
1346
|
+
rateLimitRemaining: this.rateLimiter.remaining,
|
|
1347
|
+
security: {
|
|
1348
|
+
sandboxActive: true,
|
|
1349
|
+
locked: this.security.isLocked,
|
|
1350
|
+
sessionToken: this.security.sessionToken
|
|
1351
|
+
},
|
|
1352
|
+
selfHealing: this.healer.getStats(),
|
|
1353
|
+
stealthMode: this.stealth.isEnabled
|
|
1354
|
+
};
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
// ── Custom Action Registration ──────────────────────────────────────
|
|
1358
|
+
registerAction(actionDef) {
|
|
1359
|
+
this.registry.register(actionDef);
|
|
1360
|
+
this.events.emit('action:registered', { name: actionDef.name });
|
|
1361
|
+
this.logger.log('register_action', { name: actionDef.name });
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
unregisterAction(name) {
|
|
1365
|
+
this.registry.unregister(name);
|
|
1366
|
+
this.events.emit('action:unregistered', { name });
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
// ── Discovery / Info ────────────────────────────────────────────────
|
|
1370
|
+
getActions(category) {
|
|
1371
|
+
if (category) return this.registry.getByCategory(category);
|
|
1372
|
+
return this.registry.list();
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
getAction(name) {
|
|
1376
|
+
return this.registry.get(name);
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
// ── Waiting Utilities for Agents ────────────────────────────────────
|
|
1380
|
+
waitForElement(selector, timeout = 10000) {
|
|
1381
|
+
return new Promise((resolve, reject) => {
|
|
1382
|
+
const el = safeQuerySelector(selector);
|
|
1383
|
+
if (el) return resolve(el);
|
|
1384
|
+
|
|
1385
|
+
const observer = new MutationObserver(() => {
|
|
1386
|
+
const found = safeQuerySelector(selector);
|
|
1387
|
+
if (found) {
|
|
1388
|
+
observer.disconnect();
|
|
1389
|
+
clearTimeout(timer);
|
|
1390
|
+
resolve(found);
|
|
1391
|
+
}
|
|
1392
|
+
});
|
|
1393
|
+
|
|
1394
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
1395
|
+
|
|
1396
|
+
const timer = setTimeout(() => {
|
|
1397
|
+
observer.disconnect();
|
|
1398
|
+
reject(new Error(`Timeout: element "${selector}" not found within ${timeout}ms`));
|
|
1399
|
+
}, timeout);
|
|
1400
|
+
});
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
waitForNavigation(timeout = 15000) {
|
|
1404
|
+
return new Promise((resolve) => {
|
|
1405
|
+
const start = location.href;
|
|
1406
|
+
const check = setInterval(() => {
|
|
1407
|
+
if (location.href !== start) {
|
|
1408
|
+
clearInterval(check);
|
|
1409
|
+
clearTimeout(timer);
|
|
1410
|
+
resolve({ from: start, to: location.href });
|
|
1411
|
+
}
|
|
1412
|
+
}, 200);
|
|
1413
|
+
const timer = setTimeout(() => {
|
|
1414
|
+
clearInterval(check);
|
|
1415
|
+
resolve({ from: start, to: start, timedOut: true });
|
|
1416
|
+
}, timeout);
|
|
1417
|
+
});
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
// ── Lifecycle ───────────────────────────────────────────────────────
|
|
1421
|
+
refresh() {
|
|
1422
|
+
this.registry = new ActionRegistry();
|
|
1423
|
+
this._autoDiscoverActions();
|
|
1424
|
+
this._storeFingerprints();
|
|
1425
|
+
this.events.emit('refresh');
|
|
1426
|
+
this.logger.log('refresh', {});
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
destroy() {
|
|
1430
|
+
this.events.emit('destroy');
|
|
1431
|
+
if (this._mutationObserver) {
|
|
1432
|
+
this._mutationObserver.disconnect();
|
|
1433
|
+
this._mutationObserver = null;
|
|
1434
|
+
}
|
|
1435
|
+
this.registry = new ActionRegistry();
|
|
1436
|
+
this.logger.clear();
|
|
1437
|
+
delete global.AICommands;
|
|
1438
|
+
delete global.WebAgentBridge;
|
|
1439
|
+
delete global.__wab_bidi;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
// ── Serialization ───────────────────────────────────────────────────
|
|
1443
|
+
toJSON() {
|
|
1444
|
+
return {
|
|
1445
|
+
version: VERSION,
|
|
1446
|
+
page: this.getPageInfo(),
|
|
1447
|
+
actions: this.getActions()
|
|
1448
|
+
};
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
// ── WebDriver BiDi Compatibility ────────────────────────────────────
|
|
1452
|
+
// Exposes a standardized protocol for agents using WebDriver BiDi
|
|
1453
|
+
toBiDi() {
|
|
1454
|
+
return {
|
|
1455
|
+
type: 'wab:context',
|
|
1456
|
+
version: VERSION,
|
|
1457
|
+
context: {
|
|
1458
|
+
url: location.href,
|
|
1459
|
+
title: document.title,
|
|
1460
|
+
browsingContext: typeof window !== 'undefined' ? window.name || 'default' : 'default'
|
|
1461
|
+
},
|
|
1462
|
+
capabilities: {
|
|
1463
|
+
actions: this.getActions().map(a => ({
|
|
1464
|
+
id: a.name,
|
|
1465
|
+
type: a.trigger === 'click' ? 'pointerDown' : a.trigger === 'fill_and_submit' ? 'key' : a.trigger,
|
|
1466
|
+
description: a.description,
|
|
1467
|
+
parameters: a.fields ? a.fields.map(f => ({ name: f.name, type: f.type, required: f.required })) : undefined
|
|
1468
|
+
})),
|
|
1469
|
+
permissions: this._getEffectivePermissions(),
|
|
1470
|
+
tier: this.getEffectiveTier()
|
|
1471
|
+
}
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
// Execute via BiDi-style command
|
|
1476
|
+
async executeBiDi(command) {
|
|
1477
|
+
// Security: validate command
|
|
1478
|
+
const validation = this.security.validateCommand(command || {});
|
|
1479
|
+
if (!validation.valid) {
|
|
1480
|
+
return { id: command?.id, error: { code: 'security error', message: validation.error } };
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
if (!command || !command.method) {
|
|
1484
|
+
return { id: command?.id, error: { code: 'invalid argument', message: 'Missing method' } };
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
this.security.audit('bidi_command', { method: command.method });
|
|
1488
|
+
const responseBase = { id: command.id || null, type: 'success' };
|
|
1489
|
+
|
|
1490
|
+
switch (command.method) {
|
|
1491
|
+
case 'wab.getContext':
|
|
1492
|
+
return { ...responseBase, result: this.toBiDi() };
|
|
1493
|
+
|
|
1494
|
+
case 'wab.getActions':
|
|
1495
|
+
return { ...responseBase, result: this.getActions(command.params?.category) };
|
|
1496
|
+
|
|
1497
|
+
case 'wab.executeAction':
|
|
1498
|
+
if (!command.params?.name) {
|
|
1499
|
+
return { id: command.id, error: { code: 'invalid argument', message: 'Action name required' } };
|
|
1500
|
+
}
|
|
1501
|
+
const result = await this.execute(command.params.name, command.params.data || {});
|
|
1502
|
+
return { ...responseBase, result };
|
|
1503
|
+
|
|
1504
|
+
case 'wab.readContent':
|
|
1505
|
+
if (!command.params?.selector) {
|
|
1506
|
+
return { id: command.id, error: { code: 'invalid argument', message: 'Selector required' } };
|
|
1507
|
+
}
|
|
1508
|
+
return { ...responseBase, result: this.readContent(command.params.selector) };
|
|
1509
|
+
|
|
1510
|
+
case 'wab.getPageInfo':
|
|
1511
|
+
return { ...responseBase, result: this.getPageInfo() };
|
|
1512
|
+
|
|
1513
|
+
default:
|
|
1514
|
+
return { id: command.id, error: { code: 'unknown command', message: `Unknown method: ${command.method}` } };
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
// ─── Auto-initialize ──────────────────────────────────────────────────
|
|
1520
|
+
function autoInit() {
|
|
1521
|
+
const config = global.AIBridgeConfig || {};
|
|
1522
|
+
|
|
1523
|
+
const scriptTag = document.currentScript || document.querySelector('script[data-wab-config]');
|
|
1524
|
+
if (scriptTag) {
|
|
1525
|
+
const dataConfig = scriptTag.getAttribute('data-config') || scriptTag.getAttribute('data-wab-config');
|
|
1526
|
+
if (dataConfig) {
|
|
1527
|
+
try {
|
|
1528
|
+
Object.assign(config, JSON.parse(dataConfig));
|
|
1529
|
+
} catch (e) {
|
|
1530
|
+
console.error('[WAB] Invalid data-config JSON:', e);
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
const bridge = new WebAgentBridge(config);
|
|
1536
|
+
|
|
1537
|
+
global.AICommands = bridge;
|
|
1538
|
+
global.WebAgentBridge = WebAgentBridge;
|
|
1539
|
+
|
|
1540
|
+
// WebDriver BiDi compatibility: expose via __wab_bidi channel
|
|
1541
|
+
global.__wab_bidi = {
|
|
1542
|
+
version: VERSION,
|
|
1543
|
+
send: async (command) => bridge.executeBiDi(command),
|
|
1544
|
+
getContext: () => bridge.toBiDi()
|
|
1545
|
+
};
|
|
1546
|
+
|
|
1547
|
+
if (typeof CustomEvent !== 'undefined') {
|
|
1548
|
+
document.dispatchEvent(new CustomEvent('wab:ready', { detail: { version: VERSION } }));
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
if (document.readyState === 'loading') {
|
|
1553
|
+
document.addEventListener('DOMContentLoaded', autoInit);
|
|
1554
|
+
} else {
|
|
1555
|
+
autoInit();
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
})(typeof window !== 'undefined' ? window : globalThis);
|