javascript 如何使用js点击电报内联按钮?

q9yhzks0  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(135)

我已经尝试过这样的解决方案:

const element = // button address
 
const event = new MouseEvent("click", {
    bubbles: true,
    cancelable: true,
    view: window
});

//Or this
const event = new MouseEvent("click");

//Or this
const event = new Event("click", { bubbles: true });

element.dispatchEvent(event);

另一个是这样的:

elem.click()

两者似乎都不能触发点击内联按钮,感谢您的帮助,谢谢!
我尝试了以上的解决方案,我试图触发一个点击内联按钮编程自动化一些事情,并记住这不是我自己的机器人

6rqinv9w

6rqinv9w1#

好吧,伙计们,我有答案了。只需要模拟长时间按键。下面是工作解决方案,谢谢我以后呵呵:

function simulateLongPress() {
  const element = document.getElementById("myElement"); // Replace "myElement" with the actual ID of your target element

  // Create and dispatch a "mousedown" event
  const mousedownEvent = new MouseEvent("mousedown", {
    bubbles: true,
    cancelable: true,
    view: window
  });
  element.dispatchEvent(mousedownEvent);

  // Wait for a specific duration to simulate the long press
  const longPressDuration = 1000; // 1 second
  setTimeout(() => {
    // Create and dispatch a "mouseup" event
    const mouseupEvent = new MouseEvent("mouseup", {
      bubbles: true,
      cancelable: true,
      view: window
    });
    element.dispatchEvent(mouseupEvent);

    // Create and dispatch a "click" event
    const clickEvent = new MouseEvent("click", {
      bubbles: true,
      cancelable: true,
      view: window
    });
    element.dispatchEvent(clickEvent);
  }, longPressDuration);
}

// Call the function to simulate a long press
simulateLongPress();

实际上不需要计时,只需要mousedown事件就可以了!

相关问题