js实现在页面上随机自动点击

    function simulateClick(x, y) {
        var event = new MouseEvent('click', {
            bubbles: true,
            cancelable: true,
            view: window,
            clientX: x,
            clientY: y
        });
        var clickedElement = document.elementFromPoint(x, y);
        if (clickedElement) {
            clickedElement.dispatchEvent(event);
        }
        // 在点击后调用特定的函数
        yourFunction(event); // 替换为你想要调用的函数
    }
    function yourFunction(event) {
		console.log(event)
    }

    function autoRandomClick(interval) {
        setInterval(function () {
            var randomPosition = getRandomPosition();
            simulateClick(randomPosition.x, randomPosition.y);
        }, interval);
    }
    function getRandomPosition() {
        var bodyRect = document.body.getBoundingClientRect();
        var randomX = Math.floor(Math.random() * (bodyRect.width - 1)) + bodyRect.left;
        var randomY = Math.floor(Math.random() * (bodyRect.height - 1)) + bodyRect.top;
        return { x: randomX, y: randomY };
    }
    // 使用示例
    autoRandomClick(1000);

你可能感兴趣的:(JavaScript,前端,javascript,html,css)