shellway 0.8.8 → 0.8.9
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/package.json +1 -1
- package/shellway.js +36 -13
package/package.json
CHANGED
package/shellway.js
CHANGED
|
@@ -10,7 +10,7 @@ import { readFile, writeFile, appendFile, readdir, stat, lstat, mkdir, rename, c
|
|
|
10
10
|
import { createServer as netServer, connect as netConnect } from 'node:net';
|
|
11
11
|
import { randomBytes, timingSafeEqual, createHash } from 'node:crypto';
|
|
12
12
|
|
|
13
|
-
const AGENT_VERSION = '0.8.
|
|
13
|
+
const AGENT_VERSION = '0.8.9'; // keep equal to agent/package.json
|
|
14
14
|
if (Number(process.versions.node.split('.')[0]) < 22) {
|
|
15
15
|
console.error(`Shellway needs Node.js 22 or newer (you have ${process.versions.node}). Get it at https://nodejs.org`);
|
|
16
16
|
process.exit(1);
|
|
@@ -1448,17 +1448,36 @@ function proofText(b, a, what) {
|
|
|
1448
1448
|
const MINIMISED = 'The browser window is minimised or hidden, so the page has no size and cannot be captured (Shellway tried to restore it). Restore the browser window (focus_window with its title, or click it on the taskbar) and take the screenshot again.';
|
|
1449
1449
|
// 0.8.8 (Ruslan 28 Nov): "Cannot take screenshot with 0 width" = a minimised browser window. On Windows, restore the
|
|
1450
1450
|
// minimised window of that browser (the one whose title starts with the tab's title, or the only minimised one).
|
|
1451
|
-
async function restoreBrowserWindow(tabTitle) {
|
|
1451
|
+
async function restoreBrowserWindow(tabTitle, focus = false) {
|
|
1452
1452
|
if (platform() !== 'win32') return false;
|
|
1453
1453
|
const procs = { edge: 'msedge', chrome: 'chrome', brave: 'brave', opera: 'opera' };
|
|
1454
1454
|
const names = extMode() && ext.cur && procs[ext.cur.browser] ? [procs[ext.cur.browser]] : ['msedge', 'chrome', 'brave'];
|
|
1455
1455
|
try {
|
|
1456
1456
|
const out = await runPs(`${WIN_API}\n$t=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${b64(String(tabTitle || '').slice(0, 60))}'))\n`
|
|
1457
|
-
+ `$w=@(Get-Process -Name ${names.map((x) => `'${x}'`).join(',')} -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 -and [SwWin.W]::IsIconic($_.MainWindowHandle) })\n`
|
|
1458
|
-
+ "$p=@($w | Where-Object { $t -and $_.MainWindowTitle.StartsWith($t) })[0]\nif (-not $p -and $w.Count -eq 1) { $p=$w[0] }\
|
|
1457
|
+
+ `$w=@(Get-Process -Name ${names.map((x) => `'${x}'`).join(',')} -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 -and (${focus ? '$true' : '$false'} -or [SwWin.W]::IsIconic($_.MainWindowHandle)) })\n`
|
|
1458
|
+
+ "$p=@($w | Where-Object { $t -and $_.MainWindowTitle.StartsWith($t) })[0]\nif (-not $p -and $w.Count -eq 1) { $p=$w[0] }\n"
|
|
1459
|
+
+ "if ($p) { $h=$p.MainWindowHandle; if ([SwWin.W]::IsIconic($h)) { [void][SwWin.W]::ShowWindowAsync($h,9); Start-Sleep -Milliseconds 250 }; "
|
|
1460
|
+
+ (focus ? "[void](New-Object -ComObject WScript.Shell).AppActivate($p.Id); [void][SwWin.W]::SetForegroundWindow($h); " : '')
|
|
1461
|
+
+ "'RESTORED' } else { 'NONE' }");
|
|
1459
1462
|
return /RESTORED/.test(out);
|
|
1460
1463
|
} catch { return false; }
|
|
1461
1464
|
}
|
|
1465
|
+
// 0.8.9 (owner's live test 29 Nov): a screenshot of a page in a minimised browser window waited until it timed out.
|
|
1466
|
+
// Before a screenshot Shellway brings the tab forward and, on Windows, restores the window and brings it to the front.
|
|
1467
|
+
// Clicks, typing and keys still work in a minimised window (tested), so for them only the tab is brought forward inside
|
|
1468
|
+
// its window (full = false): Shellway does not pull the browser in front of what the user is doing.
|
|
1469
|
+
async function ensureVisible(s, t, full = true) {
|
|
1470
|
+
const vis = async () => { try { return await evalJs(s, 'document.visibilityState', 5000); } catch { return 'unknown'; } };
|
|
1471
|
+
if ((await vis()) !== 'hidden') return 'visible';
|
|
1472
|
+
try { await cdpJson(`/json/activate/${t.id}`); } catch {}
|
|
1473
|
+
try { await s.send('Page.bringToFront', {}, 5000); } catch {}
|
|
1474
|
+
await sleep(300);
|
|
1475
|
+
if ((await vis()) !== 'hidden' || !full) return 'shown';
|
|
1476
|
+
if (await restoreBrowserWindow(t.title, true)) { await sleep(700); if ((await vis()) !== 'hidden') return 'restored'; }
|
|
1477
|
+
return 'hidden';
|
|
1478
|
+
}
|
|
1479
|
+
const visNote = (v) => (v === 'hidden' ? '\nNote: the browser window stayed hidden (minimised or covered by another window), so the page may not react. Bring it to the front with focus_window and try again.'
|
|
1480
|
+
: v === 'restored' ? '\n(The browser window was minimised or covered; Shellway brought it to the front first.)' : '');
|
|
1462
1481
|
// 0.8.8 (Ruslan 28 Nov): tabs keep their own id (stable while the browser runs, unlike the list number) and an optional
|
|
1463
1482
|
// label the AI gives them (e.g. the name of its run), so a long run can find and keep using its own tabs.
|
|
1464
1483
|
const tabLabels = new Map();
|
|
@@ -1484,7 +1503,8 @@ const browserHandlers = {
|
|
|
1484
1503
|
async browser_click({ ref, text: t, double, x, y }) {
|
|
1485
1504
|
// 0.7.4: click a point of the latest browser_screenshot (for things that have no number, e.g. inside a canvas).
|
|
1486
1505
|
if ((ref === undefined || ref === null || ref === '') && !t && Number.isFinite(Number(x)) && Number.isFinite(Number(y)) && x !== null && y !== null) {
|
|
1487
|
-
return withPage(async (s) => {
|
|
1506
|
+
return withPage(async (s, tab) => {
|
|
1507
|
+
const shown = await ensureVisible(s, tab, false);
|
|
1488
1508
|
const vp = await evalJs(s, '({ sy: scrollY, sx: scrollX, h: innerHeight, w: innerWidth })');
|
|
1489
1509
|
const px = Number(x) / lastShot.scale, pyPage = Number(y) / lastShot.scale + lastShot.top;
|
|
1490
1510
|
let vy = pyPage - vp.sy;
|
|
@@ -1493,17 +1513,18 @@ const browserHandlers = {
|
|
|
1493
1513
|
const before = await proofBefore(s, null);
|
|
1494
1514
|
await mouseClick(s, px, vy, double ? 2 : 1);
|
|
1495
1515
|
const page = await afterAction(s);
|
|
1496
|
-
return `Clicked at x=${x}, y=${y} of the screenshot.\n${page}\n${proofText(before, await pageState(s, null), 'click')}`;
|
|
1516
|
+
return `Clicked at x=${x}, y=${y} of the screenshot.\n${page}\n${proofText(before, await pageState(s, null), 'click')}${visNote(shown)}`;
|
|
1497
1517
|
});
|
|
1498
1518
|
}
|
|
1499
1519
|
if ((ref === undefined || ref === null || ref === '') && !t) throw new Error('ref (the [number] from browser_read), text, or x and y (pixels in the latest browser_screenshot) is required');
|
|
1500
|
-
return withPage(async (s) => {
|
|
1520
|
+
return withPage(async (s, tab) => {
|
|
1521
|
+
const shown = await ensureVisible(s, tab, false);
|
|
1501
1522
|
const p = await refPoint(s, ref, t);
|
|
1502
1523
|
const n = ref !== undefined && ref !== null && ref !== '' ? Number(ref) | 0 : null;
|
|
1503
1524
|
const before = await proofBefore(s, n);
|
|
1504
1525
|
await mouseClick(s, p.x, p.y, double ? 2 : 1);
|
|
1505
1526
|
const page = await afterAction(s);
|
|
1506
|
-
return `Clicked ${n !== null ? `[${ref}]` : `"${t}"`}.\n${page}\n${proofText(before, await pageState(s, n), 'click')}`;
|
|
1527
|
+
return `Clicked ${n !== null ? `[${ref}]` : `"${t}"`}.\n${page}\n${proofText(before, await pageState(s, n), 'click')}${visNote(shown)}`;
|
|
1507
1528
|
});
|
|
1508
1529
|
},
|
|
1509
1530
|
async browser_type({ ref, text: t, clear, submit }) {
|
|
@@ -1511,7 +1532,8 @@ const browserHandlers = {
|
|
|
1511
1532
|
if (t.length > 10_000) throw new Error('text is too long (max 10,000 characters)');
|
|
1512
1533
|
if (ref === undefined || ref === null || ref === '') throw new Error('ref is required: the [number] of the field from browser_read');
|
|
1513
1534
|
const n = Number(ref) | 0;
|
|
1514
|
-
return withPage(async (s) => {
|
|
1535
|
+
return withPage(async (s, tab) => {
|
|
1536
|
+
const shown = await ensureVisible(s, tab, false);
|
|
1515
1537
|
const p = await refPoint(s, ref);
|
|
1516
1538
|
// 0.7.4: a drop-down list (<select>): choose the option whose text or value matches.
|
|
1517
1539
|
if (p.tag === 'select') {
|
|
@@ -1556,17 +1578,17 @@ const browserHandlers = {
|
|
|
1556
1578
|
}
|
|
1557
1579
|
}
|
|
1558
1580
|
}
|
|
1559
|
-
if (submit) { await pressKeys(s, 'enter'); const page = await afterAction(s); return `Typed ${t.length} characters into [${ref}] and pressed Enter.\n${page}\n${proofText(before, await pageState(s, n), 'typing and Enter')}`; }
|
|
1581
|
+
if (submit) { await pressKeys(s, 'enter'); const page = await afterAction(s); return `Typed ${t.length} characters into [${ref}] and pressed Enter.\n${page}\n${proofText(before, await pageState(s, n), 'typing and Enter')}${visNote(shown)}`; }
|
|
1560
1582
|
const after = await pageState(s, n);
|
|
1561
1583
|
// 0.8.8: say plainly whether the field now holds the text (for a password field only its length is compared)
|
|
1562
1584
|
const holds = after && after.item ? (after.item.value_tail !== null ? (t ? after.item.value_tail.endsWith(t.slice(-Math.min(t.length, 40))) : after.item.value_len === 0) : after.item.value_len !== null ? after.item.value_len >= t.length : null) : null;
|
|
1563
1585
|
const fieldLine = holds === true ? `Check: the field now holds the typed text (${after.item.value_len} characters).` : holds === false ? `Check: the field does NOT hold the typed text (it has ${after.item.value_len} characters). The page may have rejected or changed it: check with browser_read.` : proofText(before, after, 'typing');
|
|
1564
|
-
return `Typed ${t.length} characters into [${ref}]${p.type === 'password' ? ' (password field)' : ''}.\n${fieldLine}`;
|
|
1586
|
+
return `Typed ${t.length} characters into [${ref}]${p.type === 'password' ? ' (password field)' : ''}.\n${fieldLine}${visNote(shown)}`;
|
|
1565
1587
|
});
|
|
1566
1588
|
},
|
|
1567
1589
|
async browser_press({ keys }) {
|
|
1568
1590
|
cdpKeyEvents(keys); // validate first
|
|
1569
|
-
return withPage(async (s) => { const before = await proofBefore(s, null); await pressKeys(s, keys); const page = await afterAction(s, 500); return `Pressed ${keys}.\n${page}\n${proofText(before, await pageState(s, null), 'key press')}`; });
|
|
1591
|
+
return withPage(async (s, tab) => { const shown = await ensureVisible(s, tab, false); const before = await proofBefore(s, null); await pressKeys(s, keys); const page = await afterAction(s, 500); return `Pressed ${keys}.\n${page}\n${proofText(before, await pageState(s, null), 'key press')}${visNote(shown)}`; });
|
|
1570
1592
|
},
|
|
1571
1593
|
async browser_scroll({ direction, pages: n }) {
|
|
1572
1594
|
const k = Math.min(Math.max(Number(n) || 1, 1), 20) * (direction === 'up' ? -1 : 1);
|
|
@@ -1579,6 +1601,7 @@ const browserHandlers = {
|
|
|
1579
1601
|
async browser_screenshot({ full_page }) {
|
|
1580
1602
|
return withPage(async (s, t) => {
|
|
1581
1603
|
const VP = '({ w: innerWidth, h: innerHeight, H: document.documentElement.scrollHeight })';
|
|
1604
|
+
if ((await ensureVisible(s, t)) === 'hidden') throw new Error(MINIMISED);
|
|
1582
1605
|
let vp = await evalJs(s, VP);
|
|
1583
1606
|
if (!(vp.w > 0 && vp.h > 0)) {
|
|
1584
1607
|
// 0.8.8 (Ruslan 28 Nov): a minimised or hidden window has no size: bring the tab forward and restore the window once
|
|
@@ -1596,7 +1619,7 @@ const browserHandlers = {
|
|
|
1596
1619
|
// Keep the image small enough for the AI apps (about 500 KB at most): lower quality first, then size.
|
|
1597
1620
|
let sc = scale;
|
|
1598
1621
|
let r;
|
|
1599
|
-
try { r = await shot(sc, full_page ? 45 : 60); } catch (e) { if (/0 width|0 height/i.test(String(e && e.message))) throw new Error(MINIMISED); throw e; }
|
|
1622
|
+
try { r = await shot(sc, full_page ? 45 : 60); } catch (e) { if (/0 width|0 height|did not answer|timed? ?out/i.test(String(e && e.message))) throw new Error(MINIMISED); throw e; }
|
|
1600
1623
|
for (let k = 0.7; r.data.length > 700_000 && k > 0.2; k -= 0.2) { sc = scale * k; r = await shot(sc, 40); }
|
|
1601
1624
|
lastShot = { scale: sc, top: y }; // for browser_click with x, y
|
|
1602
1625
|
const info = await evalJs(s, '({ title: document.title, url: location.href })');
|