browser-assistant 2.9.23 → 2.9.25
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/extension/content.js +18 -2
- package/extension/editor-bridge.js +99 -39
- package/extension/manifest.json +1 -1
- package/package.json +1 -1
- package/server/main.py +1 -1
package/extension/content.js
CHANGED
|
@@ -2110,8 +2110,13 @@ function mountJournalCountdown(signal) {
|
|
|
2110
2110
|
if (ordering) {
|
|
2111
2111
|
const active = () => automation.running && automation.runId === runId && getAutomationContext().mode === 'FULL';
|
|
2112
2112
|
const nativeDrag = (prompt, options, from, to) => safeSendMessage({ action: 'orderingDrag', prompt, options, from, to });
|
|
2113
|
-
|
|
2114
|
-
|
|
2113
|
+
const orderUrl = location.href;
|
|
2114
|
+
const applied = await currentAdapter.applyOrder(document, current, answerIndexes, active, nativeDrag);
|
|
2115
|
+
if (!automation.running || automation.runId !== runId)
|
|
2116
|
+
break;
|
|
2117
|
+
if (!applied) {
|
|
2118
|
+
const visibleQuestion = location.href === orderUrl ? getAuthorizedQuestion() : null;
|
|
2119
|
+
automation.error = orderingReviewMessage(question, answerIndexes, visibleQuestion);
|
|
2115
2120
|
stopAutomation('PAUSED');
|
|
2116
2121
|
break;
|
|
2117
2122
|
}
|
|
@@ -2849,6 +2854,17 @@ function mountJournalCountdown(signal) {
|
|
|
2849
2854
|
throw new Error('The verified answer did not match the displayed options exactly.');
|
|
2850
2855
|
}
|
|
2851
2856
|
}
|
|
2857
|
+
function orderingReviewMessage(question, order, current) {
|
|
2858
|
+
const message = 'The page did not confirm the drag.';
|
|
2859
|
+
if (current?.type !== 'ordering' || current.text !== question.text || current.options.length !== order.length) {
|
|
2860
|
+
return message + ' Check the current rows before submitting.';
|
|
2861
|
+
}
|
|
2862
|
+
const positions = order.map(index => current.options.findIndex(option => option.text === question.options[index].text) + 1);
|
|
2863
|
+
if (positions.includes(0) || new Set(positions).size !== order.length) {
|
|
2864
|
+
return message + ' Check the current rows before submitting.';
|
|
2865
|
+
}
|
|
2866
|
+
return message + ` Arrange the current rows in the order ${positions.join(' → ')}.`;
|
|
2867
|
+
}
|
|
2852
2868
|
async function prepareVisionQuestionContext() {
|
|
2853
2869
|
const context = { url: location.href, text: '', question: getAuthorizedQuestion() };
|
|
2854
2870
|
if (!context.question || context.question.type === 'code') {
|
|
@@ -286,8 +286,7 @@ function browserAssistantReadMcqSnippets() {
|
|
|
286
286
|
return { ok: false, error: error.message };
|
|
287
287
|
}
|
|
288
288
|
}
|
|
289
|
-
//
|
|
290
|
-
// native DataTransfer object only when these events originate in this world.
|
|
289
|
+
// Run in the page's main world so the practice page receives the drag data.
|
|
291
290
|
async function browserAssistantOrderingDrag(prompt, optionTexts, from, to) {
|
|
292
291
|
try {
|
|
293
292
|
const normalize = value => (value || '').replace(/\s+/g, ' ').trim();
|
|
@@ -307,7 +306,10 @@ async function browserAssistantOrderingDrag(prompt, optionTexts, from, to) {
|
|
|
307
306
|
return [];
|
|
308
307
|
const visible = [...question.querySelectorAll('.drag-and-drop-option-label')]
|
|
309
308
|
.filter(label => label.getClientRects().length)
|
|
310
|
-
.map(label => ({
|
|
309
|
+
.map(label => ({
|
|
310
|
+
label, row: label.parentElement, text: normalize(label.textContent),
|
|
311
|
+
handle: label.parentElement?.querySelector('.drag-icon-container[draggable="true"]'),
|
|
312
|
+
}));
|
|
311
313
|
return visible.length === expectedTexts.length && visible.every(row => uniqueTexts.has(row.text) && row.handle)
|
|
312
314
|
&& new Set(visible.map(row => row.text)).size === uniqueTexts.size ? visible : [];
|
|
313
315
|
};
|
|
@@ -317,45 +319,103 @@ async function browserAssistantOrderingDrag(prompt, optionTexts, from, to) {
|
|
|
317
319
|
return { ok: false };
|
|
318
320
|
if (from === to)
|
|
319
321
|
return { ok: true };
|
|
320
|
-
const
|
|
321
|
-
const
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
const
|
|
322
|
+
const moving = before[from].text;
|
|
323
|
+
const destination = before[to].text;
|
|
324
|
+
const findRow = text => rows().find(row => row.text === text);
|
|
325
|
+
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
326
|
+
const center = node => {
|
|
325
327
|
const rect = node.getBoundingClientRect();
|
|
326
|
-
node.
|
|
327
|
-
|
|
328
|
-
|
|
328
|
+
return { node, clientX: rect.left + rect.width / 2, clientY: rect.top + rect.height / 2 };
|
|
329
|
+
};
|
|
330
|
+
const unchangedRows = () => {
|
|
331
|
+
const current = rows();
|
|
332
|
+
return current.length === before.length && current.every((row, index) => row.text === expectedTexts[index]) ? current : [];
|
|
333
|
+
};
|
|
334
|
+
const pressHandle = (original, pressed) => {
|
|
335
|
+
const point = () => center(findRow(moving)?.handle || original);
|
|
336
|
+
const init = {
|
|
337
|
+
bubbles: true, cancelable: true, button: 0, buttons: pressed ? 1 : 0,
|
|
338
|
+
};
|
|
339
|
+
const pointer = point();
|
|
340
|
+
if (typeof PointerEvent === 'function' && !pointer.node.dispatchEvent(new PointerEvent(pressed ? 'pointerdown' : 'pointerup', { ...init, clientX: pointer.clientX, clientY: pointer.clientY, pointerType: 'mouse', pointerId: 1, isPrimary: true })) && pressed)
|
|
341
|
+
return;
|
|
342
|
+
const mouse = point();
|
|
343
|
+
mouse.node.dispatchEvent(new MouseEvent(pressed ? 'mousedown' : 'mouseup', {
|
|
344
|
+
...init, clientX: mouse.clientX, clientY: mouse.clientY,
|
|
329
345
|
}));
|
|
330
346
|
};
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return {
|
|
357
|
-
|
|
358
|
-
|
|
347
|
+
for (const mode of ['row', 'handle', 'press']) {
|
|
348
|
+
if (mode === 'press' && typeof MouseEvent !== 'function')
|
|
349
|
+
break;
|
|
350
|
+
const current = unchangedRows();
|
|
351
|
+
if (!current.length)
|
|
352
|
+
break;
|
|
353
|
+
const handle = current[from].handle;
|
|
354
|
+
let source = handle;
|
|
355
|
+
let started = false;
|
|
356
|
+
const dataTransfer = new DataTransfer();
|
|
357
|
+
dataTransfer.setData('text/plain', moving);
|
|
358
|
+
dataTransfer.effectAllowed = 'move';
|
|
359
|
+
const send = ({ node, clientX, clientY }, type) => node.dispatchEvent(new DragEvent(type, {
|
|
360
|
+
bubbles: true, cancelable: true, dataTransfer, clientX, clientY,
|
|
361
|
+
}));
|
|
362
|
+
const target = () => {
|
|
363
|
+
const row = findRow(destination);
|
|
364
|
+
if (!row)
|
|
365
|
+
return null;
|
|
366
|
+
if (mode === 'handle')
|
|
367
|
+
return center(row.handle);
|
|
368
|
+
const { clientX } = center(row.label);
|
|
369
|
+
const rect = row.row.getBoundingClientRect();
|
|
370
|
+
const clientY = rect.top + rect.height * (from > to ? 0.25 : 0.75);
|
|
371
|
+
const hit = document.elementFromPoint?.(clientX, clientY);
|
|
372
|
+
return { node: hit && row.row.contains(hit) ? hit : row.label, clientX, clientY };
|
|
373
|
+
};
|
|
374
|
+
try {
|
|
375
|
+
if (mode === 'press') {
|
|
376
|
+
// Some practice rows become draggable only after the handle is pressed.
|
|
377
|
+
pressHandle(handle, true);
|
|
378
|
+
await wait(100);
|
|
379
|
+
const armed = unchangedRows()[from];
|
|
380
|
+
if (!armed?.row.matches('[draggable="true"]'))
|
|
381
|
+
break;
|
|
382
|
+
source = armed.row;
|
|
383
|
+
}
|
|
384
|
+
started = send({ ...center(findRow(moving)?.handle || handle), node: source }, 'dragstart');
|
|
385
|
+
// A cancelled start can mean the row has yet to be enabled by its handle.
|
|
386
|
+
if (!started)
|
|
387
|
+
continue;
|
|
388
|
+
// A drag handler can render fresh row nodes between events.
|
|
389
|
+
await wait(220);
|
|
390
|
+
dataTransfer.dropEffect = 'move';
|
|
391
|
+
const entered = target();
|
|
392
|
+
if (!entered)
|
|
393
|
+
break;
|
|
394
|
+
send(entered, 'dragenter');
|
|
395
|
+
const hovered = target();
|
|
396
|
+
if (!hovered)
|
|
397
|
+
break;
|
|
398
|
+
send(hovered, 'dragover');
|
|
399
|
+
await wait(220);
|
|
400
|
+
const dropped = target();
|
|
401
|
+
if (!dropped)
|
|
402
|
+
break;
|
|
403
|
+
send(dropped, 'drop');
|
|
404
|
+
}
|
|
405
|
+
finally {
|
|
406
|
+
if (started) {
|
|
407
|
+
const latest = findRow(moving);
|
|
408
|
+
const endSource = latest && (source.matches('.drag-icon-container') ? latest.handle : latest.row);
|
|
409
|
+
send(center(endSource || source), 'dragend');
|
|
410
|
+
}
|
|
411
|
+
if (mode === 'press')
|
|
412
|
+
pressHandle(handle, false);
|
|
413
|
+
}
|
|
414
|
+
for (let attempt = 0; attempt < 10; attempt++) {
|
|
415
|
+
await wait(160);
|
|
416
|
+
if (rows()[to]?.text === moving)
|
|
417
|
+
return { ok: true };
|
|
418
|
+
}
|
|
359
419
|
}
|
|
360
420
|
return { ok: false };
|
|
361
421
|
}
|
package/extension/manifest.json
CHANGED
package/package.json
CHANGED
package/server/main.py
CHANGED
|
@@ -194,7 +194,7 @@ async def lifespan(_app: FastAPI):
|
|
|
194
194
|
client.close()
|
|
195
195
|
|
|
196
196
|
|
|
197
|
-
app = FastAPI(title="Browser Assistant API", version="2.9.
|
|
197
|
+
app = FastAPI(title="Browser Assistant API", version="2.9.25", lifespan=lifespan)
|
|
198
198
|
install_local_guards(app)
|
|
199
199
|
|
|
200
200
|
@app.exception_handler(HTTPException)
|