@thinkingos/vsl-mcp-server 1.3.3 → 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 +630 -578
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -3679,7 +3679,7 @@ async function getStateAfterAction(session, browser, url, sessionId) {
|
|
|
3679
3679
|
};
|
|
3680
3680
|
session.snapshotFromElements(extraction.elements, input);
|
|
3681
3681
|
const fullDoc = session.getSnapshot();
|
|
3682
|
-
await injectVslIdsIntoDom(browser, fullDoc.objects);
|
|
3682
|
+
await injectVslIdsIntoDom(browser, fullDoc.objects, /* @__PURE__ */ new Map(), sessionId);
|
|
3683
3683
|
const scrollContext = session.getScrollContext();
|
|
3684
3684
|
const win = computeVisibleWindow(fullDoc.canvas.viewport, scrollContext);
|
|
3685
3685
|
const scrollable = computeScrollable(fullDoc.canvas.viewport, scrollContext);
|
|
@@ -3809,638 +3809,654 @@ async function handleExecuteAction(args, browser, session, sessionId) {
|
|
|
3809
3809
|
}
|
|
3810
3810
|
const snapshot = session.getSnapshot();
|
|
3811
3811
|
const snapshotUrl = snapshot.canvas.url;
|
|
3812
|
+
const urlBeforeAction = await browser.evaluate(() => window.location.href, sessionId);
|
|
3812
3813
|
if (snapshotUrl) {
|
|
3813
|
-
const currentUrl =
|
|
3814
|
+
const currentUrl = urlBeforeAction;
|
|
3814
3815
|
if (currentUrl !== snapshotUrl) {
|
|
3815
3816
|
await browser.navigate(snapshotUrl, sessionId);
|
|
3816
3817
|
}
|
|
3817
3818
|
}
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
let targetFrame = null;
|
|
3834
|
-
let localShortId = args.target_id;
|
|
3835
|
-
const frameMatch = args.target_id.match(/^iframe_(\d+):(.+)$/);
|
|
3836
|
-
if (frameMatch) {
|
|
3837
|
-
const frameIndex = parseInt(frameMatch[1], 10);
|
|
3838
|
-
localShortId = frameMatch[2];
|
|
3839
|
-
const frameUrl = iframeFrameRegistry.get(frameIndex);
|
|
3840
|
-
if (!frameUrl) {
|
|
3841
|
-
return {
|
|
3842
|
-
status: "error",
|
|
3843
|
-
error: `Frame iframe_${frameIndex} not found in registry. Call vsl_get_snapshot first.`
|
|
3844
|
-
};
|
|
3845
|
-
}
|
|
3846
|
-
const frames = await browser.getFrames(sessionId);
|
|
3847
|
-
targetFrame = frames.find((f) => {
|
|
3848
|
-
const u = f.url();
|
|
3849
|
-
return u === frameUrl || u.includes(frameUrl) || frameUrl.includes(u);
|
|
3850
|
-
}) || null;
|
|
3851
|
-
if (!targetFrame) {
|
|
3852
|
-
return {
|
|
3853
|
-
status: "error",
|
|
3854
|
-
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
|
|
3855
3834
|
};
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
let findObjectById2 = function(objects, targetId) {
|
|
3870
|
-
for (const obj of objects) {
|
|
3871
|
-
const o = obj;
|
|
3872
|
-
if (o.id === targetId) return o;
|
|
3873
|
-
if (o.ch && Array.isArray(o.ch)) {
|
|
3874
|
-
const found = findObjectById2(o.ch, targetId);
|
|
3875
|
-
if (found) return found;
|
|
3876
|
-
}
|
|
3877
|
-
}
|
|
3878
|
-
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.`
|
|
3879
3848
|
};
|
|
3880
|
-
var findObjectById = findObjectById2;
|
|
3881
|
-
console.warn(`[VSL] Standard locator failed for ${args.target_id}, trying positional mapping fallback`);
|
|
3882
|
-
const snapshotDoc = session.getSnapshot();
|
|
3883
|
-
const snapshotObjects = snapshotDoc.objects ?? [];
|
|
3884
|
-
const snapshotObj = findObjectById2(snapshotObjects, localId);
|
|
3885
|
-
if (!snapshotObj || !snapshotObj.rect) {
|
|
3886
|
-
console.error(`[VSL] Positional mapping failed: element ${localId} not found in snapshot or has no rect`);
|
|
3887
|
-
throw locatorError;
|
|
3888
|
-
}
|
|
3889
|
-
const rect = snapshotObj.rect;
|
|
3890
|
-
const centerX = rect.x + rect.width / 2;
|
|
3891
|
-
const centerY = rect.y + rect.height / 2;
|
|
3892
|
-
const elementFound = await target.evaluate(
|
|
3893
|
-
([x, y]) => {
|
|
3894
|
-
const el = document.elementFromPoint(x, y);
|
|
3895
|
-
if (el) {
|
|
3896
|
-
el.click();
|
|
3897
|
-
return true;
|
|
3898
|
-
}
|
|
3899
|
-
return false;
|
|
3900
|
-
},
|
|
3901
|
-
[centerX, centerY]
|
|
3902
|
-
);
|
|
3903
|
-
if (!elementFound) {
|
|
3904
|
-
console.error(`[VSL] Positional mapping failed: no element found at (${centerX}, ${centerY})`);
|
|
3905
|
-
throw locatorError;
|
|
3906
|
-
}
|
|
3907
|
-
console.log(`[VSL] Positional mapping succeeded: clicked element at (${centerX}, ${centerY}) for ${args.target_id}`);
|
|
3908
3849
|
}
|
|
3909
|
-
await
|
|
3910
|
-
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
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) {
|
|
3915
3856
|
return {
|
|
3916
3857
|
status: "error",
|
|
3917
|
-
error: "
|
|
3858
|
+
error: `Frame with URL "${frameUrl}" not found. Available frames: ${frames.map((f) => f.url()).join(", ")}`
|
|
3918
3859
|
};
|
|
3919
3860
|
}
|
|
3920
|
-
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
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}`);
|
|
3946
3912
|
}
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
if (!newScroll) {
|
|
3950
|
-
newScroll = serverSideScrollFallback(prevScroll, dx, dy, session.getSnapshot().canvas?.viewport);
|
|
3913
|
+
await page.waitForTimeout(100);
|
|
3914
|
+
break;
|
|
3951
3915
|
}
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3916
|
+
case "fill":
|
|
3917
|
+
case "type":
|
|
3918
|
+
if (!args.value) {
|
|
3919
|
+
return {
|
|
3920
|
+
status: "error",
|
|
3921
|
+
error: "value is required for type action"
|
|
3922
|
+
};
|
|
3959
3923
|
}
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
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;
|
|
3967
3950
|
}
|
|
3951
|
+
} catch {
|
|
3968
3952
|
}
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
}
|
|
3980
|
-
if (targetFrame) {
|
|
3981
|
-
await browser.evaluateInFrame(targetFrame, ({ sel, val }) => {
|
|
3982
|
-
const el = document.querySelector(sel);
|
|
3983
|
-
if (el) el.value = val;
|
|
3984
|
-
}, sessionId, { sel: selector, val: args.value });
|
|
3985
|
-
} else {
|
|
3986
|
-
await browser.evaluate(({ sel, val }) => {
|
|
3987
|
-
const el = document.querySelector(sel);
|
|
3988
|
-
if (el) el.value = val;
|
|
3989
|
-
}, sessionId, { sel: selector, val: args.value });
|
|
3990
|
-
}
|
|
3991
|
-
break;
|
|
3992
|
-
case "hover":
|
|
3993
|
-
if (targetFrame) {
|
|
3994
|
-
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
3995
|
-
const el = document.querySelector(sel);
|
|
3996
|
-
if (el) {
|
|
3997
|
-
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
3998
|
-
el.dispatchEvent(event);
|
|
3999
|
-
}
|
|
4000
|
-
}, sessionId, selector);
|
|
4001
|
-
} else {
|
|
4002
|
-
await browser.evaluate((sel) => {
|
|
4003
|
-
const el = document.querySelector(sel);
|
|
4004
|
-
if (el) {
|
|
4005
|
-
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
4006
|
-
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
|
|
4007
3963
|
}
|
|
4008
|
-
}, sessionId, selector);
|
|
4009
|
-
}
|
|
4010
|
-
break;
|
|
4011
|
-
case "blur":
|
|
4012
|
-
if (targetFrame) {
|
|
4013
|
-
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4014
|
-
const el = document.querySelector(sel);
|
|
4015
|
-
if (el) el.blur();
|
|
4016
|
-
}, sessionId, selector);
|
|
4017
|
-
} else {
|
|
4018
|
-
await browser.evaluate((sel) => {
|
|
4019
|
-
const el = document.querySelector(sel);
|
|
4020
|
-
if (el) el.blur();
|
|
4021
|
-
}, sessionId, selector);
|
|
4022
|
-
}
|
|
4023
|
-
break;
|
|
4024
|
-
case "focus":
|
|
4025
|
-
if (targetFrame) {
|
|
4026
|
-
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4027
|
-
const el = document.querySelector(sel);
|
|
4028
|
-
if (el) el.focus();
|
|
4029
|
-
}, sessionId, selector);
|
|
4030
|
-
} else {
|
|
4031
|
-
await browser.evaluate((sel) => {
|
|
4032
|
-
const el = document.querySelector(sel);
|
|
4033
|
-
if (el) el.focus();
|
|
4034
|
-
}, sessionId, selector);
|
|
4035
|
-
}
|
|
4036
|
-
break;
|
|
4037
|
-
case "press": {
|
|
4038
|
-
if (!args.value) {
|
|
4039
|
-
return {
|
|
4040
|
-
status: "error",
|
|
4041
|
-
error: 'value is required for press action (key name, e.g. "Enter", "Tab", "Escape")'
|
|
4042
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;
|
|
4043
3976
|
}
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
3977
|
+
case "select":
|
|
3978
|
+
if (!args.value) {
|
|
3979
|
+
return {
|
|
3980
|
+
status: "error",
|
|
3981
|
+
error: "value is required for select action"
|
|
3982
|
+
};
|
|
3983
|
+
}
|
|
4051
3984
|
if (targetFrame) {
|
|
4052
|
-
await browser.evaluateInFrame(targetFrame, (
|
|
4053
|
-
const
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
document.body.appendChild(a);
|
|
4057
|
-
a.click();
|
|
4058
|
-
document.body.removeChild(a);
|
|
4059
|
-
}, 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 });
|
|
4060
3989
|
} else {
|
|
4061
|
-
await browser.evaluate((
|
|
4062
|
-
const
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
document.body.appendChild(a);
|
|
4066
|
-
a.click();
|
|
4067
|
-
document.body.removeChild(a);
|
|
4068
|
-
}, 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 });
|
|
4069
3994
|
}
|
|
4070
|
-
|
|
3995
|
+
break;
|
|
3996
|
+
case "hover":
|
|
4071
3997
|
if (targetFrame) {
|
|
4072
3998
|
await browser.evaluateInFrame(targetFrame, (sel) => {
|
|
4073
3999
|
const el = document.querySelector(sel);
|
|
4074
|
-
if (el)
|
|
4000
|
+
if (el) {
|
|
4001
|
+
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
4002
|
+
el.dispatchEvent(event);
|
|
4003
|
+
}
|
|
4075
4004
|
}, sessionId, selector);
|
|
4076
4005
|
} else {
|
|
4077
4006
|
await browser.evaluate((sel) => {
|
|
4078
4007
|
const el = document.querySelector(sel);
|
|
4079
|
-
if (el)
|
|
4008
|
+
if (el) {
|
|
4009
|
+
const event = new MouseEvent("mouseover", { bubbles: true });
|
|
4010
|
+
el.dispatchEvent(event);
|
|
4011
|
+
}
|
|
4080
4012
|
}, sessionId, selector);
|
|
4081
4013
|
}
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
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);
|
|
4091
4026
|
}
|
|
4092
|
-
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
|
|
4097
|
-
|
|
4098
|
-
|
|
4099
|
-
|
|
4100
|
-
|
|
4101
|
-
|
|
4102
|
-
|
|
4103
|
-
|
|
4104
|
-
status: "success",
|
|
4105
|
-
data: {
|
|
4106
|
-
action: args.action,
|
|
4107
|
-
target_id: args.target_id,
|
|
4108
|
-
success: downloadInfo.status === "completed",
|
|
4109
|
-
download: {
|
|
4110
|
-
downloadId: downloadInfo.downloadId,
|
|
4111
|
-
filename: downloadInfo.filename,
|
|
4112
|
-
url: downloadInfo.url,
|
|
4113
|
-
status: downloadInfo.status,
|
|
4114
|
-
path: args.save_path ?? downloadInfo.path ?? void 0
|
|
4115
|
-
}
|
|
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);
|
|
4116
4039
|
}
|
|
4117
|
-
|
|
4118
|
-
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
result2.warning = `State extraction failed: ${state.error}`;
|
|
4125
|
-
}
|
|
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
|
+
};
|
|
4126
4047
|
}
|
|
4048
|
+
await page.keyboard.press(args.value, { delay: 0 });
|
|
4049
|
+
await page.waitForTimeout(50);
|
|
4050
|
+
break;
|
|
4127
4051
|
}
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
action: args.action,
|
|
4150
|
-
target_id: args.target_id,
|
|
4151
|
-
success: true,
|
|
4152
|
-
upload: {
|
|
4153
|
-
selector,
|
|
4154
|
-
files: filePaths,
|
|
4155
|
-
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);
|
|
4156
4073
|
}
|
|
4157
|
-
}
|
|
4158
|
-
|
|
4159
|
-
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
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);
|
|
4166
4085
|
}
|
|
4167
4086
|
}
|
|
4168
|
-
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
if (el) {
|
|
4177
|
-
el.value = "";
|
|
4178
|
-
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
4179
|
-
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);
|
|
4180
4095
|
}
|
|
4181
|
-
}
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
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
|
+
}
|
|
4189
4120
|
}
|
|
4190
|
-
}, sessionId, selector);
|
|
4191
|
-
}
|
|
4192
|
-
break;
|
|
4193
|
-
}
|
|
4194
|
-
case "drag": {
|
|
4195
|
-
if (!args.value) {
|
|
4196
|
-
return {
|
|
4197
|
-
status: "error",
|
|
4198
|
-
error: "value is required for drag action (destination element ID)"
|
|
4199
4121
|
};
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
if (source && destination) {
|
|
4209
|
-
const dispatchDragEvent = (el, type) => {
|
|
4210
|
-
if (typeof DragEvent === "function") {
|
|
4211
|
-
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4212
|
-
} else {
|
|
4213
|
-
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}`;
|
|
4214
4130
|
}
|
|
4215
|
-
}
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
dispatchDragEvent(destination, "dragover");
|
|
4219
|
-
dispatchDragEvent(destination, "drop");
|
|
4220
|
-
dispatchDragEvent(source, "dragend");
|
|
4221
|
-
}
|
|
4222
|
-
}, sessionId, { srcSel: selector, dstSel: destSelector });
|
|
4223
|
-
} else {
|
|
4224
|
-
await browser.evaluate(({ srcSel, dstSel }) => {
|
|
4225
|
-
const source = document.querySelector(srcSel);
|
|
4226
|
-
const destination = document.querySelector(dstSel);
|
|
4227
|
-
if (source && destination) {
|
|
4228
|
-
const dispatchDragEvent = (el, type) => {
|
|
4229
|
-
if (typeof DragEvent === "function") {
|
|
4230
|
-
el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true }));
|
|
4231
|
-
} else {
|
|
4232
|
-
el.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4233
|
-
}
|
|
4234
|
-
};
|
|
4235
|
-
dispatchDragEvent(source, "dragstart");
|
|
4236
|
-
dispatchDragEvent(destination, "dragenter");
|
|
4237
|
-
dispatchDragEvent(destination, "dragover");
|
|
4238
|
-
dispatchDragEvent(destination, "drop");
|
|
4239
|
-
dispatchDragEvent(source, "dragend");
|
|
4131
|
+
}
|
|
4132
|
+
} catch (stateError) {
|
|
4133
|
+
result2.warning = `State extraction failed: ${stateError instanceof Error ? stateError.message : String(stateError)}`;
|
|
4240
4134
|
}
|
|
4241
|
-
}
|
|
4135
|
+
}
|
|
4136
|
+
result2.metadata = computeToolMetrics(result2.data, startTime);
|
|
4137
|
+
return result2;
|
|
4242
4138
|
}
|
|
4243
|
-
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4258
|
-
|
|
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
|
+
}
|
|
4259
4165
|
}
|
|
4260
|
-
}
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
const
|
|
4264
|
-
if (
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
4269
|
-
el2.dispatchEvent(new Event(type, { bubbles: true, cancelable: true }));
|
|
4270
|
-
}
|
|
4271
|
-
};
|
|
4272
|
-
dispatchDragEvent(el, "dragover");
|
|
4273
|
-
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
|
+
}
|
|
4274
4175
|
}
|
|
4275
|
-
}
|
|
4176
|
+
}
|
|
4177
|
+
result2.metadata = computeToolMetrics(result2.data, startTime);
|
|
4178
|
+
return result2;
|
|
4276
4179
|
}
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
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;
|
|
4294
4201
|
}
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
},
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
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;
|
|
4312
4252
|
}
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
el
|
|
4332
|
-
|
|
4333
|
-
|
|
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;
|
|
4334
4286
|
}
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
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;
|
|
4356
4322
|
}
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
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) {
|
|
4365
4330
|
el.open = true;
|
|
4366
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4367
|
-
el.setAttribute("aria-expanded", "true");
|
|
4368
4331
|
}
|
|
4369
|
-
}
|
|
4370
|
-
}
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
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) {
|
|
4376
4339
|
el.open = true;
|
|
4377
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4378
|
-
el.setAttribute("aria-expanded", "true");
|
|
4379
4340
|
}
|
|
4380
|
-
}
|
|
4381
|
-
}
|
|
4341
|
+
}, sessionId, selector);
|
|
4342
|
+
}
|
|
4343
|
+
break;
|
|
4382
4344
|
}
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
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) {
|
|
4391
4352
|
el.open = false;
|
|
4392
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4393
|
-
el.setAttribute("aria-expanded", "false");
|
|
4394
4353
|
}
|
|
4395
|
-
}
|
|
4396
|
-
}
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
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) {
|
|
4402
4361
|
el.open = false;
|
|
4403
|
-
} else if (el.hasAttribute("aria-expanded")) {
|
|
4404
|
-
el.setAttribute("aria-expanded", "false");
|
|
4405
4362
|
}
|
|
4406
|
-
}
|
|
4407
|
-
}
|
|
4363
|
+
}, sessionId, selector);
|
|
4364
|
+
}
|
|
4365
|
+
break;
|
|
4408
4366
|
}
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
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:
|
|
4413
4448
|
return {
|
|
4414
4449
|
status: "error",
|
|
4415
|
-
error:
|
|
4450
|
+
error: `Action ${args.action} is not yet implemented`
|
|
4416
4451
|
};
|
|
4417
|
-
}
|
|
4418
|
-
const timeout = args.timeout || 5e3;
|
|
4419
|
-
const condition = args.value;
|
|
4420
|
-
if (condition === "idle") {
|
|
4421
|
-
await page.waitForLoadState("domcontentloaded", { timeout });
|
|
4422
|
-
} else {
|
|
4423
|
-
await page.waitForSelector(condition, { timeout, state: "attached" });
|
|
4424
|
-
}
|
|
4425
|
-
break;
|
|
4426
|
-
}
|
|
4427
|
-
case "go_back": {
|
|
4428
|
-
await page.goBack({ timeout: args.timeout || 5e3 });
|
|
4429
|
-
break;
|
|
4430
|
-
}
|
|
4431
|
-
case "go_forward": {
|
|
4432
|
-
await page.goForward({ timeout: args.timeout || 5e3 });
|
|
4433
|
-
break;
|
|
4434
4452
|
}
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
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;
|
|
4438
4459
|
}
|
|
4439
|
-
default:
|
|
4440
|
-
return {
|
|
4441
|
-
status: "error",
|
|
4442
|
-
error: `Action ${args.action} is not yet implemented`
|
|
4443
|
-
};
|
|
4444
4460
|
}
|
|
4445
4461
|
const result = {
|
|
4446
4462
|
status: "success",
|
|
@@ -4451,17 +4467,53 @@ async function handleExecuteAction(args, browser, session, sessionId) {
|
|
|
4451
4467
|
}
|
|
4452
4468
|
};
|
|
4453
4469
|
if (args.return_state !== false) {
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
|
|
4464
|
-
|
|
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 {
|
|
4465
4517
|
}
|
|
4466
4518
|
}
|
|
4467
4519
|
}
|
|
@@ -6436,7 +6488,7 @@ var SessionManager = class {
|
|
|
6436
6488
|
};
|
|
6437
6489
|
|
|
6438
6490
|
// src/version.ts
|
|
6439
|
-
var VSL_MCP_SERVER_VERSION = "1.3.
|
|
6491
|
+
var VSL_MCP_SERVER_VERSION = "1.3.4";
|
|
6440
6492
|
|
|
6441
6493
|
// src/index.ts
|
|
6442
6494
|
async function main() {
|