我有以下代码。我要做的是将鼠标悬停在消息容器(消息,在 whatsapp 网络上)上,单击出现的三角形上下文按钮,然后单击“下载”
我正在尝试自动下载语音消息(我认为唯一带有下载按钮的消息是语音消息或媒体,而且当“加星标的消息”选项卡打开时我正在执行此代码)
但由于某种原因,它只悬停在消息上,出现三角形,点击它但没有点击下载按钮。
我知道这确实很具体,但要重现这一点,您可以在 whatsapp 网站上为 2 或 3 个语音消息加注星标,转到浏览器上加星标的消息选项卡,打开控制台并粘贴代码。
var messageContainers = document.querySelectorAll('[data-testid="msg-container"]');
for (var i = 0; i < messageContainers.length; i++) {
setTimeout(function(container) {
// Simulate a mouseover event on the container element
var mouseoverEvent = new MouseEvent('mouseover', {
view: window,
bubbles: true,
cancelable: true
});
container.dispatchEvent(mouseoverEvent);
// Wait for the "down context" button to appear, then simulate a click event on it
setTimeout(function() {
var tri = container.querySelector('[data-testid="icon-down-context"]');
if (tri) {
var clickEvent = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
tri.dispatchEvent(clickEvent);
// Wait for the download button to appear, then simulate a click event on it
setTimeout(function() {
var downloadButton = container.querySelector('[data-testid="mi-msg-download"]');
if (downloadButton) {
downloadButton.dispatchEvent(clickEvent);
}
}, 1000);
}
}, 1000);
}, 1000 * i, messageContainers[i]);
}
出于某种原因,这个成功了。以防万一其他人需要它。
var messageContainers = document.querySelectorAll('[data-testid="msg-container"]');
for (var i = 0; i < messageContainers.length; i++) {
setTimeout(function(container) {
// Simulate a mouseover event on the container element
var mouseoverEvent = new MouseEvent('mouseover', {
view: window,
bubbles: true,
cancelable: true
});
container.dispatchEvent(mouseoverEvent);
setTimeout(function() {
// Find the triangle element and simulate a click event on it
var tri = container.querySelector('[data-testid="down-context"]');
if (tri) {
var clickEvent = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
tri.dispatchEvent(clickEvent);
// Wait for the context menu to appear
setTimeout(function() {
// Find the download button and simulate a click event on it
var downloadButton = document.querySelector('li[data-testid="mi-msg-download"] > div._1MZM5');
if (downloadButton) {
downloadButton.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
}));
}
}, 500);
}
},500);
}, 1000 * i, messageContainers[i]);
}