@thinkingos/vsl-mcp-server 1.3.2 → 1.3.4
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/dist/index.js +638 -578
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -2587,6 +2587,14 @@ import { isVslDiff } from "@thinkingos/vsl-sdk";
|
|
|
2587
2587
|
// src/utils/injectVslIds.ts
|
|
2588
2588
|
function buildInjectionScript() {
|
|
2589
2589
|
return (objects, semanticMap) => {
|
|
2590
|
+
if (!objects || !Array.isArray(objects)) {
|
|
2591
|
+
console.warn("[VSL] injectVslIds: objects is undefined or not an array, skipping injection");
|
|
2592
|
+
return;
|
|
2593
|
+
}
|
|
2594
|
+
if (!semanticMap || typeof semanticMap !== "object") {
|
|
2595
|
+
console.warn("[VSL] injectVslIds: semanticMap is undefined or not an object, using empty map");
|
|
2596
|
+
semanticMap = {};
|
|
2597
|
+
}
|
|
2590
2598
|
function parseVslId(id) {
|
|
2591
2599
|
const match = id.match(/^([a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*)_(\d+(?:_\d+)*)$/);
|
|
2592
2600
|
if (!match) return null;
|
|
@@ -3671,7 +3679,7 @@ async function getStateAfterAction(session, browser, url, sessionId) {
|
|
|
3671
3679
|
};
|
|
3672
3680
|
session.snapshotFromElements(extraction.elements, input);
|
|
3673
3681
|
const fullDoc = session.getSnapshot();
|
|
3674
|
-
await injectVslIdsIntoDom(browser, fullDoc.objects);
|
|
3682
|
+
await injectVslIdsIntoDom(browser, fullDoc.objects, /* @__PURE__ */ new Map(), sessionId);
|
|
3675
3683
|
const scrollContext = session.getScrollContext();
|
|
3676
3684
|
const win = computeVisibleWindow(fullDoc.canvas.viewport, scrollContext);
|
|
3677
3685
|
const scrollable = computeScrollable(fullDoc.canvas.viewport, scrollContext);
|
|
@@ -3801,638 +3809,654 @@ async function handleExecuteAction(args, browser, session, sessionId) {
|
|
|
3801
3809
|
}
|
|
3802
3810
|
const snapshot = session.getSnapshot();
|
|
3803
3811
|
const snapshotUrl = snapshot.canvas.url;
|
|
3812
|
+
const urlBeforeAction = await browser.evaluate(() => window.location.href, sessionId);
|
|
3804
3813
|
if (snapshotUrl) {
|
|
3805
|
-
const currentUrl =
|
|
3814
|
+
const currentUrl = urlBeforeAction;
|
|
3806
3815
|
if (currentUrl !== snapshotUrl) {
|
|
3807
3816
|
await browser.navigate(snapshotUrl, sessionId);
|
|
3808
3817
|
}
|
|
3809
3818
|
}
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
let targetFrame = null;
|
|
3826
|
-
let localShortId = args.target_id;
|
|
3827
|
-
const frameMatch = args.target_id.match(/^iframe_(\d+):(.+)$/);
|
|
3828
|
-
if (frameMatch) {
|
|
3829
|
-
const frameIndex = parseInt(frameMatch[1], 10);
|
|
3830
|
-
localShortId = frameMatch[2];
|
|
3831
|
-
const frameUrl = iframeFrameRegistry.get(frameIndex);
|
|
3832
|
-
if (!frameUrl) {
|
|
3833
|
-
return {
|
|
3834
|
-
status: "error",
|
|
3835
|
-
error: `Frame iframe_${frameIndex} not found in registry. Call vsl_get_snapshot first.`
|
|
3836
|
-
};
|
|
3837
|
-
}
|
|
3838
|
-
const frames = await browser.getFrames(sessionId);
|
|
3839
|
-
targetFrame = frames.find((f) => {
|
|
3840
|
-
const u = f.url();
|
|
3841
|
-
return u === frameUrl || u.includes(frameUrl) || frameUrl.includes(u);
|
|
3842
|
-
}) || null;
|
|
3843
|
-
if (!targetFrame) {
|
|
3844
|
-
return {
|
|
3845
|
-
status: "error",
|
|
3846
|
-
error: `Frame with URL "${frameUrl}" not found. Available frames: ${frames.map((f) => f.url()).join(", ")}`
|
|
3819
|
+
let actionError = null;
|
|
3820
|
+
let page = null;
|
|
3821
|
+
try {
|
|
3822
|
+
const hasVslIds = await browser.evaluate(() => {
|
|
3823
|
+
return document.querySelector("[data-vsl-id]") !== null;
|
|
3824
|
+
}, sessionId);
|
|
3825
|
+
if (!hasVslIds) {
|
|
3826
|
+
const extraction = await extractDomTree(browser, sessionId);
|
|
3827
|
+
const input = {
|
|
3828
|
+
viewport: extraction.viewport,
|
|
3829
|
+
url: snapshotUrl || "",
|
|
3830
|
+
title: "",
|
|
3831
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3832
|
+
// Метаданные скролла для единого пайплайна отдачи (АС[3])
|
|
3833
|
+
scroll: extraction.scroll
|
|
3847
3834
|
};
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
let findObjectById2 = function(objects, targetId) {
|
|
3862
|
-
for (const obj of objects) {
|
|
3863
|
-
const o = obj;
|
|
3864
|
-
if (o.id === targetId) return o;
|
|
3865
|
-
if (o.ch && Array.isArray(o.ch)) {
|
|
3866
|
-
const found = findObjectById2(o.ch, targetId);
|
|
3867
|
-
if (found) return found;
|
|
3868
|
-
}
|
|
3869
|
-
}
|
|
3870
|
-
return null;
|
|
3835
|
+
session.snapshotFromElements(extraction.elements, input);
|
|
3836
|
+
}
|
|
3837
|
+
let targetFrame = null;
|
|
3838
|
+
let localShortId = args.target_id;
|
|
3839
|
+
const frameMatch = args.target_id.match(/^iframe_(\d+):(.+)$/);
|
|
3840
|
+
if (frameMatch) {
|
|
3841
|
+
const frameIndex = parseInt(frameMatch[1], 10);
|
|
3842
|
+
localShortId = frameMatch[2];
|
|
3843
|
+
const frameUrl = iframeFrameRegistry.get(frameIndex);
|
|
3844
|
+
if (!frameUrl) {
|
|
3845
|
+
return {
|
|
3846
|
+
status: "error",
|
|
3847
|
+
error: `Frame iframe_${frameIndex} not found in registry. Call vsl_get_snapshot first.`
|
|
3871
3848
|
};
|
|
3872
|
-
var findObjectById = findObjectById2;
|
|
3873
|
-
console.warn(`[VSL] Standard locator failed for ${args.target_id}, trying positional mapping fallback`);
|
|
3874
|
-
const snapshotDoc = session.getSnapshot();
|
|
3875
|
-
const snapshotObjects = snapshotDoc.objects ?? [];
|
|
3876
|
-
const snapshotObj = findObjectById2(snapshotObjects, localId);
|
|
3877
|
-
if (!snapshotObj || !snapshotObj.rect) {
|
|
3878
|
-
console.error(`[VSL] Positional mapping failed: element ${localId} not found in snapshot or has no rect`);
|
|
3879
|
-
throw locatorError;
|
|
3880
|
-
}
|
|
3881
|
-
const rect = snapshotObj.rect;
|
|
3882
|
-
const centerX = rect.x + rect.width / 2;
|
|
3883
|
-
const centerY = rect.y + rect.height / 2;
|
|
3884
|
-
const elementFound = await target.evaluate(
|
|
3885
|
-
([x, y]) => {
|
|
3886
|
-
const el = document.elementFromPoint(x, y);
|
|
3887
|
-
if (el) {
|
|
3888
|
-
el.click();
|
|
3889
|
-
return true;
|
|
3890
|
-
}
|
|
3891
|
-
return false;
|
|
3892
|
-
},
|
|
3893
|
-
[centerX, centerY]
|
|
3894
|
-
);
|
|
3895
|
-
if (!elementFound) {
|
|
3896
|
-
console.error(`[VSL] Positional mapping failed: no element found at (${centerX}, ${centerY})`);
|
|
3897
|
-
throw locatorError;
|
|
3898
|
-
}
|
|
3899
|
-
console.log(`[VSL] Positional mapping succeeded: clicked element at (${centerX}, ${centerY}) for ${args.target_id}`);
|
|
3900
3849
|
}
|
|
3901
|
-
await
|
|
3902
|
-
|
|
3903
|
-
|
|
3904
|
-
|
|
3905
|
-
|
|
3906
|
-
if (!
|
|
3850
|
+
const frames = await browser.getFrames(sessionId);
|
|
3851
|
+
targetFrame = frames.find((f) => {
|
|
3852
|
+
const u = f.url();
|
|
3853
|
+
return u === frameUrl || u.includes(frameUrl) || frameUrl.includes(u);
|
|
3854
|
+
}) || null;
|
|
3855
|
+
if (!targetFrame) {
|
|
3907
3856
|
return {
|
|
3908
3857
|
status: "error",
|
|
3909
|
-
error: "
|
|
3858
|
+
error: `Frame with URL "${frameUrl}" not found. Available frames: ${frames.map((f) => f.url()).join(", ")}`
|
|
3910
3859
|
};
|
|
3911
3860
|
}
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
3915
|
-
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
3861
|
+
}
|
|
3862
|
+
const idMap = session.getIdMap();
|
|
3863
|
+
const resolvedId = idMap.get(args.target_id) || localShortId;
|
|
3864
|
+
const localId = resolvedId;
|
|
3865
|
+
const selector = `[data-vsl-id="${localId}"], #${localId}, .${localId}`;
|
|
3866
|
+
page = await browser.getPage(sessionId);
|
|
3867
|
+
const target = targetFrame || page;
|
|
3868
|
+
switch (args.action) {
|
|
3869
|
+
case "click": {
|
|
3870
|
+
try {
|
|
3871
|
+
await target.locator(selector).click({ timeout: 5e3 });
|
|
3872
|
+
} catch (locatorError) {
|
|
3873
|
+
let findObjectById2 = function(objects, targetId) {
|
|
3874
|
+
for (const obj of objects) {
|
|
3875
|
+
const o = obj;
|
|
3876
|
+
if (o.id === targetId) return o;
|
|
3877
|
+
if (o.ch && Array.isArray(o.ch)) {
|
|
3878
|
+
const found = findObjectById2(o.ch, targetId);
|
|
3879
|
+
if (found) return found;
|
|
3880
|
+
}
|
|
3881
|
+
}
|
|
3882
|
+
return null;
|
|
3883
|
+
};
|
|
3884
|
+
var findObjectById = findObjectById2;
|
|
3885
|
+
console.warn(`[VSL] Standard locator failed for ${args.target_id}, trying positional mapping fallback`);
|
|
3886
|
+
const snapshotDoc = session.getSnapshot();
|
|
3887
|
+
const snapshotObjects = snapshotDoc.objects ?? [];
|
|
3888
|
+
const snapshotObj = findObjectById2(snapshotObjects, localId);
|
|
3889
|
+
if (!snapshotObj || !snapshotObj.rect) {
|
|
3890
|
+
console.error(`[VSL] Positional mapping failed: element ${localId} not found in snapshot or has no rect`);
|
|
3891
|
+
throw locatorError;
|
|
3892
|
+
}
|
|
3893
|
+
const rect = snapshotObj.rect;
|
|
3894
|
+
const centerX = rect.x + rect.width / 2;
|
|
3895
|
+
const centerY = rect.y + rect.height / 2;
|
|
3896
|
+
const elementFound = await target.evaluate(
|
|
3897
|
+
([x, y]) => {
|
|
3898
|
+
const el = document.elementFromPoint(x, y);
|
|
3899
|
+
if (el) {
|
|
3900
|
+
el.click();
|
|
3901
|
+
return true;
|
|
3902
|
+
}
|
|
3903
|
+
return false;
|
|
3904
|
+
},
|
|
3905
|
+
[centerX, centerY]
|
|
3906
|
+
);
|
|
3907
|
+
if (!elementFound) {
|
|
3908
|
+
console.error(`[VSL] Positional mapping failed: no element found at (${centerX}, ${centerY})`);
|
|
3909
|
+
throw locatorError;
|
|
3910
|
+
}
|
|
3911
|
+
console.log(`[VSL] Positional mapping succeeded: clicked element at (${centerX}, ${centerY}) for ${args.target_id}`);
|
|
3938
3912
|
}
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
if (!newScroll) {
|
|
3942
|
-
newScroll = serverSideScrollFallback(prevScroll, dx, dy, session.getSnapshot().canvas?.viewport);
|
|
3913
|
+
await page.waitForTimeout(100);
|
|
3914
|
+
break;
|
|
3943
3915
|
}
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3916
|
+
case "fill":
|
|
3917
|
+
case "type":
|
|
3918
|
+
if (!args.value) {
|
|
3919
|
+
return {
|
|
3920
|
+
status: "error",
|
|
3921
|
+
error: "value is required for type action"
|
|
3922
|
+
};
|
|
3951
3923
|
}
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3924
|
+
await target.locator(selector).fill(args.value, { timeout: 5e3 });
|
|
3925
|
+
break;
|
|
3926
|
+
case "scroll": {
|
|
3927
|
+
const DEFAULT_SCROLL_AMOUNT = 500;
|
|
3928
|
+
const scrollValue = args.value || "down";
|
|
3929
|
+
const scrollParts = scrollValue.trim().split(":");
|
|
3930
|
+
const scrollDir = scrollParts[0];
|
|
3931
|
+
const scrollAmount = scrollParts[1] !== void 0 ? Number(scrollParts[1]) : DEFAULT_SCROLL_AMOUNT;
|
|
3932
|
+
const dx = scrollDir === "left" ? -scrollAmount : scrollDir === "right" ? scrollAmount : 0;
|
|
3933
|
+
const dy = scrollDir === "up" ? -scrollAmount : scrollDir === "down" ? scrollAmount : 0;
|
|
3934
|
+
await browser.evaluate(({ dx: dx2, dy: dy2 }) => {
|
|
3935
|
+
window.scrollBy(dx2, dy2);
|
|
3936
|
+
}, sessionId, { dx, dy });
|
|
3937
|
+
await page.waitForTimeout(50);
|
|
3938
|
+
const prevScroll = session.getScrollContext();
|
|
3939
|
+
let newScroll = null;
|
|
3940
|
+
try {
|
|
3941
|
+
const raw = await browser.evaluate(() => ({
|
|
3942
|
+
x: window.scrollX,
|
|
3943
|
+
y: window.scrollY,
|
|
3944
|
+
width: document.documentElement.scrollWidth,
|
|
3945
|
+
height: document.documentElement.scrollHeight
|
|
3946
|
+
}), sessionId);
|
|
3947
|
+
const r = raw;
|
|
3948
|
+
if (r !== null && typeof r === "object" && typeof r.x === "number" && typeof r.y === "number" && typeof r.width === "number" && typeof r.height === "number") {
|
|
3949
|
+
newScroll = r;
|
|
3959
3950
|
}
|
|
3951
|
+
} catch {
|
|
3960
3952
|
}
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
}
|
|
3972
|
-
if (targetFrame) {
|
|
3973
|
-
await browser.evaluateInFrame(targetFrame, ({ sel, val }) => {
|
|
3974
|
-
const el = document.querySelector(sel);
|
|
3975
|
-
if (el) el.value = val;
|
|
3976
|
-
}, sessionId, { sel: selector, val: args.value });
|
|
3977
|
-
} else {
|
|
3978
|
-
await browser.evaluate(({ sel, val }) => {
|
|
3979
|
-
const el = document.querySelector(sel);
|
|
3980
|
-
if (el) el.value = val;
|
|
3981
|
-
}, sessionId, { sel: selector, val: args.value });
|
|
3982
|
-
}
|
|
3983
|
-
break;
|
|
3984
|
-
case "hover":
|
|
3985
|
-
if (targetFrame) {
|
|
3986
|
-
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
3987
|
-
const el = document.querySelector(sel);
|
|
3988
|
-
if (el) {
|
|
3989
|
-
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
3990
|
-
el.dispatchEvent(event);
|
|
3991
|
-
}
|
|
3992
|
-
}, sessionId, selector);
|
|
3993
|
-
} else {
|
|
3994
|
-
await browser.evaluate((sel) => {
|
|
3995
|
-
const el = document.querySelector(sel);
|
|
3996
|
-
if (el) {
|
|
3997
|
-
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
3998
|
-
el.dispatchEvent(event);
|
|
3953
|
+
if (!newScroll) {
|
|
3954
|
+
newScroll = serverSideScrollFallback(prevScroll, dx, dy, session.getSnapshot().canvas?.viewport);
|
|
3955
|
+
}
|
|
3956
|
+
session.setScrollContext(newScroll);
|
|
3957
|
+
const scrollResult = {
|
|
3958
|
+
status: "success",
|
|
3959
|
+
data: {
|
|
3960
|
+
action: args.action,
|
|
3961
|
+
target_id: args.target_id,
|
|
3962
|
+
success: true
|
|
3999
3963
|
}
|
|
4000
|
-
}, sessionId, selector);
|
|
4001
|
-
}
|
|
4002
|
-
break;
|
|
4003
|
-
case "blur":
|
|
4004
|
-
if (targetFrame) {
|
|
4005
|
-
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4006
|
-
const el = document.querySelector(sel);
|
|
4007
|
-
if (el) el.blur();
|
|
4008
|
-
}, sessionId, selector);
|
|
4009
|
-
} else {
|
|
4010
|
-
await browser.evaluate((sel) => {
|
|
4011
|
-
const el = document.querySelector(sel);
|
|
4012
|
-
if (el) el.blur();
|
|
4013
|
-
}, sessionId, selector);
|
|
4014
|
-
}
|
|
4015
|
-
break;
|
|
4016
|
-
case "focus":
|
|
4017
|
-
if (targetFrame) {
|
|
4018
|
-
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4019
|
-
const el = document.querySelector(sel);
|
|
4020
|
-
if (el) el.focus();
|
|
4021
|
-
}, sessionId, selector);
|
|
4022
|
-
} else {
|
|
4023
|
-
await browser.evaluate((sel) => {
|
|
4024
|
-
const el = document.querySelector(sel);
|
|
4025
|
-
if (el) el.focus();
|
|
4026
|
-
}, sessionId, selector);
|
|
4027
|
-
}
|
|
4028
|
-
break;
|
|
4029
|
-
case "press": {
|
|
4030
|
-
if (!args.value) {
|
|
4031
|
-
return {
|
|
4032
|
-
status: "error",
|
|
4033
|
-
error: 'value is required for press action (key name, e.g. "Enter", "Tab", "Escape")'
|
|
4034
3964
|
};
|
|
3965
|
+
if (args.return_state !== false) {
|
|
3966
|
+
const state = buildScrollStateFromCache(session, newScroll);
|
|
3967
|
+
if (scrollResult.data) {
|
|
3968
|
+
scrollResult.data.state = state;
|
|
3969
|
+
if (state.error) {
|
|
3970
|
+
scrollResult.warning = `State extraction failed: ${state.error}`;
|
|
3971
|
+
}
|
|
3972
|
+
}
|
|
3973
|
+
}
|
|
3974
|
+
scrollResult.metadata = computeToolMetrics(scrollResult.data, startTime);
|
|
3975
|
+
return scrollResult;
|
|
4035
3976
|
}
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
3977
|
+
case "select":
|
|
3978
|
+
if (!args.value) {
|
|
3979
|
+
return {
|
|
3980
|
+
status: "error",
|
|
3981
|
+
error: "value is required for select action"
|
|
3982
|
+
};
|
|
3983
|
+
}
|
|
4043
3984
|
if (targetFrame) {
|
|
4044
|
-
await browser.evaluateInFrame(targetFrame, (
|
|
4045
|
-
const
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
document.body.appendChild(a);
|
|
4049
|
-
a.click();
|
|
4050
|
-
document.body.removeChild(a);
|
|
4051
|
-
}, sessionId, args.value);
|
|
3985
|
+
await browser.evaluateInFrame(targetFrame, ({ sel, val }) => {
|
|
3986
|
+
const el = document.querySelector(sel);
|
|
3987
|
+
if (el) el.value = val;
|
|
3988
|
+
}, sessionId, { sel: selector, val: args.value });
|
|
4052
3989
|
} else {
|
|
4053
|
-
await browser.evaluate((
|
|
4054
|
-
const
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
document.body.appendChild(a);
|
|
4058
|
-
a.click();
|
|
4059
|
-
document.body.removeChild(a);
|
|
4060
|
-
}, sessionId, args.value);
|
|
3990
|
+
await browser.evaluate(({ sel, val }) => {
|
|
3991
|
+
const el = document.querySelector(sel);
|
|
3992
|
+
if (el) el.value = val;
|
|
3993
|
+
}, sessionId, { sel: selector, val: args.value });
|
|
4061
3994
|
}
|
|
4062
|
-
|
|
3995
|
+
break;
|
|
3996
|
+
case "hover":
|
|
4063
3997
|
if (targetFrame) {
|
|
4064
3998
|
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4065
3999
|
const el = document.querySelector(sel);
|
|
4066
|
-
if (el)
|
|
4000
|
+
if (el) {
|
|
4001
|
+
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
4002
|
+
el.dispatchEvent(event);
|
|
4003
|
+
}
|
|
4067
4004
|
}, sessionId, selector);
|
|
4068
4005
|
} else {
|
|
4069
4006
|
await browser.evaluate((sel) => {
|
|
4070
4007
|
const el = document.querySelector(sel);
|
|
4071
|
-
if (el)
|
|
4008
|
+
if (el) {
|
|
4009
|
+
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
4010
|
+
el.dispatchEvent(event);
|
|
4011
|
+
}
|
|
4072
4012
|
}, sessionId, selector);
|
|
4073
4013
|
}
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
|
|
4081
|
-
|
|
4082
|
-
await browser.evaluate(() =>
|
|
4014
|
+
break;
|
|
4015
|
+
case "blur":
|
|
4016
|
+
if (targetFrame) {
|
|
4017
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4018
|
+
const el = document.querySelector(sel);
|
|
4019
|
+
if (el) el.blur();
|
|
4020
|
+
}, sessionId, selector);
|
|
4021
|
+
} else {
|
|
4022
|
+
await browser.evaluate((sel) => {
|
|
4023
|
+
const el = document.querySelector(sel);
|
|
4024
|
+
if (el) el.blur();
|
|
4025
|
+
}, sessionId, selector);
|
|
4083
4026
|
}
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
|
|
4091
|
-
|
|
4092
|
-
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
status: "success",
|
|
4097
|
-
data: {
|
|
4098
|
-
action: args.action,
|
|
4099
|
-
target_id: args.target_id,
|
|
4100
|
-
success: downloadInfo.status === "completed",
|
|
4101
|
-
download: {
|
|
4102
|
-
downloadId: downloadInfo.downloadId,
|
|
4103
|
-
filename: downloadInfo.filename,
|
|
4104
|
-
url: downloadInfo.url,
|
|
4105
|
-
status: downloadInfo.status,
|
|
4106
|
-
path: args.save_path ?? downloadInfo.path ?? void 0
|
|
4107
|
-
}
|
|
4027
|
+
break;
|
|
4028
|
+
case "focus":
|
|
4029
|
+
if (targetFrame) {
|
|
4030
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4031
|
+
const el = document.querySelector(sel);
|
|
4032
|
+
if (el) el.focus();
|
|
4033
|
+
}, sessionId, selector);
|
|
4034
|
+
} else {
|
|
4035
|
+
await browser.evaluate((sel) => {
|
|
4036
|
+
const el = document.querySelector(sel);
|
|
4037
|
+
if (el) el.focus();
|
|
4038
|
+
}, sessionId, selector);
|
|
4108
4039
|
}
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4115
|
-
|
|
4116
|
-
result2.warning = `State extraction failed: ${state.error}`;
|
|
4117
|
-
}
|
|
4040
|
+
break;
|
|
4041
|
+
case "press": {
|
|
4042
|
+
if (!args.value) {
|
|
4043
|
+
return {
|
|
4044
|
+
status: "error",
|
|
4045
|
+
error: 'value is required for press action (key name, e.g. "Enter", "Tab", "Escape")'
|
|
4046
|
+
};
|
|
4118
4047
|
}
|
|
4048
|
+
await page.keyboard.press(args.value, { delay: 0 });
|
|
4049
|
+
await page.waitForTimeout(50);
|
|
4050
|
+
break;
|
|
4119
4051
|
}
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
action: args.action,
|
|
4142
|
-
target_id: args.target_id,
|
|
4143
|
-
success: true,
|
|
4144
|
-
upload: {
|
|
4145
|
-
selector,
|
|
4146
|
-
files: filePaths,
|
|
4147
|
-
success: true
|
|
4052
|
+
case "download": {
|
|
4053
|
+
const downloadsBefore = browser.getDownloads();
|
|
4054
|
+
if (args.value) {
|
|
4055
|
+
if (targetFrame) {
|
|
4056
|
+
await browser.evaluateInFrame(targetFrame, (url) => {
|
|
4057
|
+
const a = document.createElement("a");
|
|
4058
|
+
a.href = url;
|
|
4059
|
+
a.download = "";
|
|
4060
|
+
document.body.appendChild(a);
|
|
4061
|
+
a.click();
|
|
4062
|
+
document.body.removeChild(a);
|
|
4063
|
+
}, sessionId, args.value);
|
|
4064
|
+
} else {
|
|
4065
|
+
await browser.evaluate((url) => {
|
|
4066
|
+
const a = document.createElement("a");
|
|
4067
|
+
a.href = url;
|
|
4068
|
+
a.download = "";
|
|
4069
|
+
document.body.appendChild(a);
|
|
4070
|
+
a.click();
|
|
4071
|
+
document.body.removeChild(a);
|
|
4072
|
+
}, sessionId, args.value);
|
|
4148
4073
|
}
|
|
4149
|
-
}
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
|
|
4154
|
-
|
|
4155
|
-
|
|
4156
|
-
|
|
4157
|
-
|
|
4074
|
+
} else {
|
|
4075
|
+
if (targetFrame) {
|
|
4076
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4077
|
+
const el = document.querySelector(sel);
|
|
4078
|
+
if (el) el.click();
|
|
4079
|
+
}, sessionId, selector);
|
|
4080
|
+
} else {
|
|
4081
|
+
await browser.evaluate((sel) => {
|
|
4082
|
+
const el = document.querySelector(sel);
|
|
4083
|
+
if (el) el.click();
|
|
4084
|
+
}, sessionId, selector);
|
|
4158
4085
|
}
|
|
4159
4086
|
}
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
if (el) {
|
|
4169
|
-
el.value = "";
|
|
4170
|
-
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
4171
|
-
el.dispatchEvent(new Event("change", { bubbles: true }));
|
|
4087
|
+
const pollDeadline = Date.now() + 5e3;
|
|
4088
|
+
let newDownload;
|
|
4089
|
+
while (!newDownload && Date.now() < pollDeadline) {
|
|
4090
|
+
newDownload = browser.getDownloads().find(
|
|
4091
|
+
(d) => !downloadsBefore.some((b) => b.downloadId === d.downloadId)
|
|
4092
|
+
);
|
|
4093
|
+
if (!newDownload) {
|
|
4094
|
+
await browser.evaluate(() => new Promise((resolve) => setTimeout(resolve, 50)), sessionId);
|
|
4172
4095
|
}
|
|
4173
|
-
}
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
|
|
4177
|
-
|
|
4178
|
-
|
|
4179
|
-
|
|
4180
|
-
|
|
4096
|
+
}
|
|
4097
|
+
if (!newDownload) {
|
|
4098
|
+
return {
|
|
4099
|
+
status: "error",
|
|
4100
|
+
error: "Download not detected after click (timeout 5000ms). The page may navigate instead of downloading."
|
|
4101
|
+
};
|
|
4102
|
+
}
|
|
4103
|
+
const downloadInfo = await browser.waitForDownload(newDownload.downloadId, args.timeout);
|
|
4104
|
+
if (args.save_path) {
|
|
4105
|
+
await browser.saveDownload(downloadInfo.downloadId, args.save_path);
|
|
4106
|
+
}
|
|
4107
|
+
const result2 = {
|
|
4108
|
+
status: "success",
|
|
4109
|
+
data: {
|
|
4110
|
+
action: args.action,
|
|
4111
|
+
target_id: args.target_id,
|
|
4112
|
+
success: downloadInfo.status === "completed",
|
|
4113
|
+
download: {
|
|
4114
|
+
downloadId: downloadInfo.downloadId,
|
|
4115
|
+
filename: downloadInfo.filename,
|
|
4116
|
+
url: downloadInfo.url,
|
|
4117
|
+
status: downloadInfo.status,
|
|
4118
|
+
path: args.save_path ?? downloadInfo.path ?? void 0
|
|
4119
|
+
}
|
|
4181
4120
|
}
|
|
4182
|
-
}, sessionId, selector);
|
|
4183
|
-
}
|
|
4184
|
-
break;
|
|
4185
|
-
}
|
|
4186
|
-
case "drag": {
|
|
4187
|
-
if (!args.value) {
|
|
4188
|
-
return {
|
|
4189
|
-
status: "error",
|
|
4190
|
-
error: "value is required for drag action (destination element ID)"
|
|
4191
4121
|
};
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
if (source && destination) {
|
|
4201
|
-
const dispatchDragEvent = (el, type) => {
|
|
4202
|
-
if (typeof DragEvent === "function") {
|
|
4203
|
-
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4204
|
-
} else {
|
|
4205
|
-
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4122
|
+
if (args.return_state !== false) {
|
|
4123
|
+
try {
|
|
4124
|
+
const currentUrl = page.url();
|
|
4125
|
+
const state = await getStateAfterAction(session, browser, currentUrl, sessionId);
|
|
4126
|
+
if (result2.data) {
|
|
4127
|
+
result2.data.state = state;
|
|
4128
|
+
if (state.error) {
|
|
4129
|
+
result2.warning = `State extraction failed: ${state.error}`;
|
|
4206
4130
|
}
|
|
4207
|
-
}
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
dispatchDragEvent(destination, "dragover");
|
|
4211
|
-
dispatchDragEvent(destination, "drop");
|
|
4212
|
-
dispatchDragEvent(source, "dragend");
|
|
4213
|
-
}
|
|
4214
|
-
}, sessionId, { srcSel: selector, dstSel: destSelector });
|
|
4215
|
-
} else {
|
|
4216
|
-
await browser.evaluate(({ srcSel, dstSel }) => {
|
|
4217
|
-
const source = document.querySelector(srcSel);
|
|
4218
|
-
const destination = document.querySelector(dstSel);
|
|
4219
|
-
if (source && destination) {
|
|
4220
|
-
const dispatchDragEvent = (el, type) => {
|
|
4221
|
-
if (typeof DragEvent === "function") {
|
|
4222
|
-
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4223
|
-
} else {
|
|
4224
|
-
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4225
|
-
}
|
|
4226
|
-
};
|
|
4227
|
-
dispatchDragEvent(source, "dragstart");
|
|
4228
|
-
dispatchDragEvent(destination, "dragenter");
|
|
4229
|
-
dispatchDragEvent(destination, "dragover");
|
|
4230
|
-
dispatchDragEvent(destination, "drop");
|
|
4231
|
-
dispatchDragEvent(source, "dragend");
|
|
4131
|
+
}
|
|
4132
|
+
} catch (stateError) {
|
|
4133
|
+
result2.warning = `State extraction failed: ${stateError instanceof Error ? stateError.message : String(stateError)}`;
|
|
4232
4134
|
}
|
|
4233
|
-
}
|
|
4135
|
+
}
|
|
4136
|
+
result2.metadata = computeToolMetrics(result2.data, startTime);
|
|
4137
|
+
return result2;
|
|
4234
4138
|
}
|
|
4235
|
-
|
|
4236
|
-
|
|
4237
|
-
|
|
4238
|
-
|
|
4239
|
-
|
|
4240
|
-
|
|
4241
|
-
|
|
4242
|
-
|
|
4243
|
-
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4139
|
+
case "upload": {
|
|
4140
|
+
if (!args.value) {
|
|
4141
|
+
return {
|
|
4142
|
+
status: "error",
|
|
4143
|
+
error: "value is required for upload action (file path(s), comma-separated)"
|
|
4144
|
+
};
|
|
4145
|
+
}
|
|
4146
|
+
const filePaths = args.value.split(",").map((p) => p.trim()).filter(Boolean);
|
|
4147
|
+
if (filePaths.length === 0) {
|
|
4148
|
+
return {
|
|
4149
|
+
status: "error",
|
|
4150
|
+
error: "value must contain at least one file path"
|
|
4151
|
+
};
|
|
4152
|
+
}
|
|
4153
|
+
await target.locator(selector).setInputFiles(filePaths.length === 1 ? filePaths[0] : filePaths);
|
|
4154
|
+
const result2 = {
|
|
4155
|
+
status: "success",
|
|
4156
|
+
data: {
|
|
4157
|
+
action: args.action,
|
|
4158
|
+
target_id: args.target_id,
|
|
4159
|
+
success: true,
|
|
4160
|
+
upload: {
|
|
4161
|
+
selector,
|
|
4162
|
+
files: filePaths,
|
|
4163
|
+
success: true
|
|
4164
|
+
}
|
|
4251
4165
|
}
|
|
4252
|
-
}
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
const
|
|
4256
|
-
if (
|
|
4257
|
-
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
|
|
4261
|
-
el2.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4262
|
-
}
|
|
4263
|
-
};
|
|
4264
|
-
dispatchDragEvent(el, "dragover");
|
|
4265
|
-
dispatchDragEvent(el, "drop");
|
|
4166
|
+
};
|
|
4167
|
+
if (args.return_state !== false) {
|
|
4168
|
+
const currentUrl = await browser.evaluate(() => window.location.href, sessionId);
|
|
4169
|
+
const state = await getStateAfterAction(session, browser, currentUrl, sessionId);
|
|
4170
|
+
if (result2.data) {
|
|
4171
|
+
result2.data.state = state;
|
|
4172
|
+
if (state.error) {
|
|
4173
|
+
result2.warning = `State extraction failed: ${state.error}`;
|
|
4174
|
+
}
|
|
4266
4175
|
}
|
|
4267
|
-
}
|
|
4176
|
+
}
|
|
4177
|
+
result2.metadata = computeToolMetrics(result2.data, startTime);
|
|
4178
|
+
return result2;
|
|
4268
4179
|
}
|
|
4269
|
-
|
|
4270
|
-
|
|
4271
|
-
|
|
4272
|
-
|
|
4273
|
-
|
|
4274
|
-
|
|
4275
|
-
|
|
4276
|
-
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4180
|
+
case "clear": {
|
|
4181
|
+
if (targetFrame) {
|
|
4182
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4183
|
+
const el = document.querySelector(sel);
|
|
4184
|
+
if (el) {
|
|
4185
|
+
el.value = "";
|
|
4186
|
+
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
4187
|
+
el.dispatchEvent(new Event("change", { bubbles: true }));
|
|
4188
|
+
}
|
|
4189
|
+
}, sessionId, selector);
|
|
4190
|
+
} else {
|
|
4191
|
+
await browser.evaluate((sel) => {
|
|
4192
|
+
const el = document.querySelector(sel);
|
|
4193
|
+
if (el) {
|
|
4194
|
+
el.value = "";
|
|
4195
|
+
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
4196
|
+
el.dispatchEvent(new Event("change", { bubbles: true }));
|
|
4197
|
+
}
|
|
4198
|
+
}, sessionId, selector);
|
|
4199
|
+
}
|
|
4200
|
+
break;
|
|
4286
4201
|
}
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
},
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4202
|
+
case "drag": {
|
|
4203
|
+
if (!args.value) {
|
|
4204
|
+
return {
|
|
4205
|
+
status: "error",
|
|
4206
|
+
error: "value is required for drag action (destination element ID)"
|
|
4207
|
+
};
|
|
4208
|
+
}
|
|
4209
|
+
const destIdMap = session.getIdMap();
|
|
4210
|
+
const destResolvedId = destIdMap.get(args.value) || args.value;
|
|
4211
|
+
const destSelector = `[data-vsl-id="${destResolvedId}"], #${destResolvedId}, .${destResolvedId}`;
|
|
4212
|
+
if (targetFrame) {
|
|
4213
|
+
await browser.evaluateInFrame(targetFrame, ({ srcSel, dstSel }) => {
|
|
4214
|
+
const source = document.querySelector(srcSel);
|
|
4215
|
+
const destination = document.querySelector(dstSel);
|
|
4216
|
+
if (source && destination) {
|
|
4217
|
+
const dispatchDragEvent = (el, type) => {
|
|
4218
|
+
if (typeof DragEvent === "function") {
|
|
4219
|
+
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4220
|
+
} else {
|
|
4221
|
+
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4222
|
+
}
|
|
4223
|
+
};
|
|
4224
|
+
dispatchDragEvent(source, "dragstart");
|
|
4225
|
+
dispatchDragEvent(destination, "dragenter");
|
|
4226
|
+
dispatchDragEvent(destination, "dragover");
|
|
4227
|
+
dispatchDragEvent(destination, "drop");
|
|
4228
|
+
dispatchDragEvent(source, "dragend");
|
|
4229
|
+
}
|
|
4230
|
+
}, sessionId, { srcSel: selector, dstSel: destSelector });
|
|
4231
|
+
} else {
|
|
4232
|
+
await browser.evaluate(({ srcSel, dstSel }) => {
|
|
4233
|
+
const source = document.querySelector(srcSel);
|
|
4234
|
+
const destination = document.querySelector(dstSel);
|
|
4235
|
+
if (source && destination) {
|
|
4236
|
+
const dispatchDragEvent = (el, type) => {
|
|
4237
|
+
if (typeof DragEvent === "function") {
|
|
4238
|
+
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4239
|
+
} else {
|
|
4240
|
+
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4241
|
+
}
|
|
4242
|
+
};
|
|
4243
|
+
dispatchDragEvent(source, "dragstart");
|
|
4244
|
+
dispatchDragEvent(destination, "dragenter");
|
|
4245
|
+
dispatchDragEvent(destination, "dragover");
|
|
4246
|
+
dispatchDragEvent(destination, "drop");
|
|
4247
|
+
dispatchDragEvent(source, "dragend");
|
|
4248
|
+
}
|
|
4249
|
+
}, sessionId, { srcSel: selector, dstSel: destSelector });
|
|
4250
|
+
}
|
|
4251
|
+
break;
|
|
4304
4252
|
}
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
el
|
|
4324
|
-
|
|
4325
|
-
|
|
4253
|
+
case "drop": {
|
|
4254
|
+
if (targetFrame) {
|
|
4255
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4256
|
+
const el = document.querySelector(sel);
|
|
4257
|
+
if (el) {
|
|
4258
|
+
const dispatchDragEvent = (el2, type) => {
|
|
4259
|
+
if (typeof DragEvent === "function") {
|
|
4260
|
+
el2.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4261
|
+
} else {
|
|
4262
|
+
el2.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4263
|
+
}
|
|
4264
|
+
};
|
|
4265
|
+
dispatchDragEvent(el, "dragover");
|
|
4266
|
+
dispatchDragEvent(el, "drop");
|
|
4267
|
+
}
|
|
4268
|
+
}, sessionId, selector);
|
|
4269
|
+
} else {
|
|
4270
|
+
await browser.evaluate((sel) => {
|
|
4271
|
+
const el = document.querySelector(sel);
|
|
4272
|
+
if (el) {
|
|
4273
|
+
const dispatchDragEvent = (el2, type) => {
|
|
4274
|
+
if (typeof DragEvent === "function") {
|
|
4275
|
+
el2.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4276
|
+
} else {
|
|
4277
|
+
el2.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4278
|
+
}
|
|
4279
|
+
};
|
|
4280
|
+
dispatchDragEvent(el, "dragover");
|
|
4281
|
+
dispatchDragEvent(el, "drop");
|
|
4282
|
+
}
|
|
4283
|
+
}, sessionId, selector);
|
|
4284
|
+
}
|
|
4285
|
+
break;
|
|
4326
4286
|
}
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4287
|
+
case "submit": {
|
|
4288
|
+
if (targetFrame) {
|
|
4289
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4290
|
+
const form = document.querySelector(sel);
|
|
4291
|
+
if (form) {
|
|
4292
|
+
form.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
|
|
4293
|
+
}
|
|
4294
|
+
}, sessionId, selector);
|
|
4295
|
+
} else {
|
|
4296
|
+
await browser.evaluate((sel) => {
|
|
4297
|
+
const form = document.querySelector(sel);
|
|
4298
|
+
if (form) {
|
|
4299
|
+
form.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
|
|
4300
|
+
}
|
|
4301
|
+
}, sessionId, selector);
|
|
4302
|
+
}
|
|
4303
|
+
break;
|
|
4304
|
+
}
|
|
4305
|
+
case "reset": {
|
|
4306
|
+
if (targetFrame) {
|
|
4307
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4308
|
+
const form = document.querySelector(sel);
|
|
4309
|
+
if (form) {
|
|
4310
|
+
form.reset();
|
|
4311
|
+
}
|
|
4312
|
+
}, sessionId, selector);
|
|
4313
|
+
} else {
|
|
4314
|
+
await browser.evaluate((sel) => {
|
|
4315
|
+
const form = document.querySelector(sel);
|
|
4316
|
+
if (form) {
|
|
4317
|
+
form.reset();
|
|
4318
|
+
}
|
|
4319
|
+
}, sessionId, selector);
|
|
4320
|
+
}
|
|
4321
|
+
break;
|
|
4348
4322
|
}
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
if (el instanceof HTMLDetailsElement) {
|
|
4323
|
+
case "open": {
|
|
4324
|
+
if (targetFrame) {
|
|
4325
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4326
|
+
const el = document.querySelector(sel);
|
|
4327
|
+
if (typeof HTMLDialogElement !== "undefined" && el instanceof HTMLDialogElement) {
|
|
4328
|
+
el.showModal();
|
|
4329
|
+
} else if (el instanceof HTMLDetailsElement) {
|
|
4357
4330
|
el.open = true;
|
|
4358
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4359
|
-
el.setAttribute("aria-expanded", "true");
|
|
4360
4331
|
}
|
|
4361
|
-
}
|
|
4362
|
-
}
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
if (el instanceof HTMLDetailsElement) {
|
|
4332
|
+
}, sessionId, selector);
|
|
4333
|
+
} else {
|
|
4334
|
+
await browser.evaluate((sel) => {
|
|
4335
|
+
const el = document.querySelector(sel);
|
|
4336
|
+
if (typeof HTMLDialogElement !== "undefined" && el instanceof HTMLDialogElement) {
|
|
4337
|
+
el.showModal();
|
|
4338
|
+
} else if (el instanceof HTMLDetailsElement) {
|
|
4368
4339
|
el.open = true;
|
|
4369
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4370
|
-
el.setAttribute("aria-expanded", "true");
|
|
4371
4340
|
}
|
|
4372
|
-
}
|
|
4373
|
-
}
|
|
4341
|
+
}, sessionId, selector);
|
|
4342
|
+
}
|
|
4343
|
+
break;
|
|
4374
4344
|
}
|
|
4375
|
-
|
|
4376
|
-
|
|
4377
|
-
|
|
4378
|
-
|
|
4379
|
-
|
|
4380
|
-
|
|
4381
|
-
|
|
4382
|
-
if (el instanceof HTMLDetailsElement) {
|
|
4345
|
+
case "close": {
|
|
4346
|
+
if (targetFrame) {
|
|
4347
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4348
|
+
const el = document.querySelector(sel);
|
|
4349
|
+
if (typeof HTMLDialogElement !== "undefined" && el instanceof HTMLDialogElement) {
|
|
4350
|
+
el.close();
|
|
4351
|
+
} else if (el instanceof HTMLDetailsElement) {
|
|
4383
4352
|
el.open = false;
|
|
4384
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4385
|
-
el.setAttribute("aria-expanded", "false");
|
|
4386
4353
|
}
|
|
4387
|
-
}
|
|
4388
|
-
}
|
|
4389
|
-
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
if (el instanceof HTMLDetailsElement) {
|
|
4354
|
+
}, sessionId, selector);
|
|
4355
|
+
} else {
|
|
4356
|
+
await browser.evaluate((sel) => {
|
|
4357
|
+
const el = document.querySelector(sel);
|
|
4358
|
+
if (typeof HTMLDialogElement !== "undefined" && el instanceof HTMLDialogElement) {
|
|
4359
|
+
el.close();
|
|
4360
|
+
} else if (el instanceof HTMLDetailsElement) {
|
|
4394
4361
|
el.open = false;
|
|
4395
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4396
|
-
el.setAttribute("aria-expanded", "false");
|
|
4397
4362
|
}
|
|
4398
|
-
}
|
|
4399
|
-
}
|
|
4363
|
+
}, sessionId, selector);
|
|
4364
|
+
}
|
|
4365
|
+
break;
|
|
4400
4366
|
}
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4367
|
+
case "expand": {
|
|
4368
|
+
if (targetFrame) {
|
|
4369
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4370
|
+
const el = document.querySelector(sel);
|
|
4371
|
+
if (el) {
|
|
4372
|
+
if (el instanceof HTMLDetailsElement) {
|
|
4373
|
+
el.open = true;
|
|
4374
|
+
} else if (el.hasAttribute("aria-expanded")) {
|
|
4375
|
+
el.setAttribute("aria-expanded", "true");
|
|
4376
|
+
}
|
|
4377
|
+
}
|
|
4378
|
+
}, sessionId, selector);
|
|
4379
|
+
} else {
|
|
4380
|
+
await browser.evaluate((sel) => {
|
|
4381
|
+
const el = document.querySelector(sel);
|
|
4382
|
+
if (el) {
|
|
4383
|
+
if (el instanceof HTMLDetailsElement) {
|
|
4384
|
+
el.open = true;
|
|
4385
|
+
} else if (el.hasAttribute("aria-expanded")) {
|
|
4386
|
+
el.setAttribute("aria-expanded", "true");
|
|
4387
|
+
}
|
|
4388
|
+
}
|
|
4389
|
+
}, sessionId, selector);
|
|
4390
|
+
}
|
|
4391
|
+
break;
|
|
4392
|
+
}
|
|
4393
|
+
case "collapse": {
|
|
4394
|
+
if (targetFrame) {
|
|
4395
|
+
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4396
|
+
const el = document.querySelector(sel);
|
|
4397
|
+
if (el) {
|
|
4398
|
+
if (el instanceof HTMLDetailsElement) {
|
|
4399
|
+
el.open = false;
|
|
4400
|
+
} else if (el.hasAttribute("aria-expanded")) {
|
|
4401
|
+
el.setAttribute("aria-expanded", "false");
|
|
4402
|
+
}
|
|
4403
|
+
}
|
|
4404
|
+
}, sessionId, selector);
|
|
4405
|
+
} else {
|
|
4406
|
+
await browser.evaluate((sel) => {
|
|
4407
|
+
const el = document.querySelector(sel);
|
|
4408
|
+
if (el) {
|
|
4409
|
+
if (el instanceof HTMLDetailsElement) {
|
|
4410
|
+
el.open = false;
|
|
4411
|
+
} else if (el.hasAttribute("aria-expanded")) {
|
|
4412
|
+
el.setAttribute("aria-expanded", "false");
|
|
4413
|
+
}
|
|
4414
|
+
}
|
|
4415
|
+
}, sessionId, selector);
|
|
4416
|
+
}
|
|
4417
|
+
break;
|
|
4418
|
+
}
|
|
4419
|
+
case "wait": {
|
|
4420
|
+
if (!args.value) {
|
|
4421
|
+
return {
|
|
4422
|
+
status: "error",
|
|
4423
|
+
error: 'value is required for wait action (selector or "idle")'
|
|
4424
|
+
};
|
|
4425
|
+
}
|
|
4426
|
+
const timeout = args.timeout || 5e3;
|
|
4427
|
+
const condition = args.value;
|
|
4428
|
+
if (condition === "idle") {
|
|
4429
|
+
await page.waitForLoadState("domcontentloaded", { timeout });
|
|
4430
|
+
} else {
|
|
4431
|
+
await page.waitForSelector(condition, { timeout, state: "attached" });
|
|
4432
|
+
}
|
|
4433
|
+
break;
|
|
4434
|
+
}
|
|
4435
|
+
case "go_back": {
|
|
4436
|
+
await page.goBack({ timeout: args.timeout || 5e3 });
|
|
4437
|
+
break;
|
|
4438
|
+
}
|
|
4439
|
+
case "go_forward": {
|
|
4440
|
+
await page.goForward({ timeout: args.timeout || 5e3 });
|
|
4441
|
+
break;
|
|
4442
|
+
}
|
|
4443
|
+
case "refresh": {
|
|
4444
|
+
await page.reload({ timeout: args.timeout || 5e3 });
|
|
4445
|
+
break;
|
|
4446
|
+
}
|
|
4447
|
+
default:
|
|
4405
4448
|
return {
|
|
4406
4449
|
status: "error",
|
|
4407
|
-
error:
|
|
4450
|
+
error: `Action ${args.action} is not yet implemented`
|
|
4408
4451
|
};
|
|
4409
|
-
}
|
|
4410
|
-
const timeout = args.timeout || 5e3;
|
|
4411
|
-
const condition = args.value;
|
|
4412
|
-
if (condition === "idle") {
|
|
4413
|
-
await page.waitForLoadState("domcontentloaded", { timeout });
|
|
4414
|
-
} else {
|
|
4415
|
-
await page.waitForSelector(condition, { timeout, state: "attached" });
|
|
4416
|
-
}
|
|
4417
|
-
break;
|
|
4418
|
-
}
|
|
4419
|
-
case "go_back": {
|
|
4420
|
-
await page.goBack({ timeout: args.timeout || 5e3 });
|
|
4421
|
-
break;
|
|
4422
|
-
}
|
|
4423
|
-
case "go_forward": {
|
|
4424
|
-
await page.goForward({ timeout: args.timeout || 5e3 });
|
|
4425
|
-
break;
|
|
4426
4452
|
}
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4453
|
+
} catch (error) {
|
|
4454
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
4455
|
+
if (errorMsg.includes("Execution context was destroyed") || errorMsg.includes("navigation")) {
|
|
4456
|
+
actionError = error instanceof Error ? error : new Error(errorMsg);
|
|
4457
|
+
} else {
|
|
4458
|
+
throw error;
|
|
4430
4459
|
}
|
|
4431
|
-
default:
|
|
4432
|
-
return {
|
|
4433
|
-
status: "error",
|
|
4434
|
-
error: `Action ${args.action} is not yet implemented`
|
|
4435
|
-
};
|
|
4436
4460
|
}
|
|
4437
4461
|
const result = {
|
|
4438
4462
|
status: "success",
|
|
@@ -4443,17 +4467,53 @@ async function handleExecuteAction(args, browser, session, sessionId) {
|
|
|
4443
4467
|
}
|
|
4444
4468
|
};
|
|
4445
4469
|
if (args.return_state !== false) {
|
|
4446
|
-
|
|
4447
|
-
|
|
4448
|
-
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4470
|
+
try {
|
|
4471
|
+
const urlAfterAction = page.url();
|
|
4472
|
+
const navigationOccurred = urlAfterAction && urlAfterAction !== urlBeforeAction;
|
|
4473
|
+
const contextDestroyed = actionError && actionError.message.includes("Execution context was destroyed");
|
|
4474
|
+
if (navigationOccurred || contextDestroyed) {
|
|
4475
|
+
try {
|
|
4476
|
+
await page.waitForLoadState("domcontentloaded", { timeout: 5e3 });
|
|
4477
|
+
await page.waitForTimeout(200);
|
|
4478
|
+
} catch (waitError) {
|
|
4479
|
+
console.warn("[vsl_execute_action] Navigation detected but waitForLoadState failed:", waitError);
|
|
4480
|
+
}
|
|
4481
|
+
} else {
|
|
4482
|
+
try {
|
|
4483
|
+
await browser.evaluate(() => {
|
|
4484
|
+
return new Promise((resolve) => {
|
|
4485
|
+
setTimeout(resolve, 100);
|
|
4486
|
+
});
|
|
4487
|
+
}, sessionId);
|
|
4488
|
+
} catch (evalError) {
|
|
4489
|
+
console.warn("[vsl_execute_action] DOM stabilization evaluate failed:", evalError);
|
|
4490
|
+
}
|
|
4491
|
+
}
|
|
4492
|
+
const currentUrl = urlAfterAction || urlBeforeAction;
|
|
4493
|
+
if ((navigationOccurred || contextDestroyed) && result.data) {
|
|
4494
|
+
result.data.navigated = true;
|
|
4495
|
+
result.data.newUrl = urlAfterAction;
|
|
4496
|
+
}
|
|
4497
|
+
try {
|
|
4498
|
+
const state = await getStateAfterAction(session, browser, currentUrl, sessionId);
|
|
4499
|
+
if (result.data) {
|
|
4500
|
+
result.data.state = state;
|
|
4501
|
+
if (state.error) {
|
|
4502
|
+
result.warning = `State extraction failed: ${state.error}`;
|
|
4503
|
+
}
|
|
4504
|
+
}
|
|
4505
|
+
} catch (stateError) {
|
|
4506
|
+
result.warning = `State extraction failed: ${stateError instanceof Error ? stateError.message : String(stateError)}`;
|
|
4507
|
+
}
|
|
4508
|
+
} catch (stateError) {
|
|
4509
|
+
result.warning = `State extraction failed: ${stateError instanceof Error ? stateError.message : String(stateError)}`;
|
|
4510
|
+
try {
|
|
4511
|
+
const urlAfterAction = page.url();
|
|
4512
|
+
if (urlAfterAction && urlAfterAction !== urlBeforeAction && result.data) {
|
|
4513
|
+
result.data.navigated = true;
|
|
4514
|
+
result.data.newUrl = urlAfterAction;
|
|
4515
|
+
}
|
|
4516
|
+
} catch {
|
|
4457
4517
|
}
|
|
4458
4518
|
}
|
|
4459
4519
|
}
|
|
@@ -6428,7 +6488,7 @@ var SessionManager = class {
|
|
|
6428
6488
|
};
|
|
6429
6489
|
|
|
6430
6490
|
// src/version.ts
|
|
6431
|
-
var VSL_MCP_SERVER_VERSION = "1.3.
|
|
6491
|
+
var VSL_MCP_SERVER_VERSION = "1.3.4";
|
|
6432
6492
|
|
|
6433
6493
|
// src/index.ts
|
|
6434
6494
|
async function main() {
|