chatGPT 对 js 代码生成不如其他语言,还是我提问的方式不对?

2023-04-24 18:44:43 +08:00
 hanssx

js 还没入门的超新手,js 里面有一个 setTimeout ,我想试一下延时点击,比如延时跳转,之前记得也试验过好像不行,现在 chatGPT 出来了,我就去问 chatGPT ,给的答案没一个能达到效果的,咋肥事呢?(之前一直问 chatGPT Python ,给的效果还挺好的,是我问 Python 太多了吗)

另外,这块到底应该怎么写呢,

Q:js setTimeout 实现网页所有点击都延时 5 秒后,再执行原来的操作,帮我写成油猴脚本。

A:好的,以下是实现该功能的油猴脚本:

javascript
Copy code
// ==UserScript==
// @name         Delay All Clicks by 5 Seconds
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Delays all clicks on the webpage by 5 seconds before executing the original action.
// @author       Your Name
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 重写 addEventListener 函数
    const originalAddEventListener = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function(eventName, eventListener, options) {
        // 如果是点击事件,则添加延时逻辑
        if (eventName === "click") {
            const originalEventListener = eventListener;
            eventListener = function(event) {
                // 阻止默认行为
                event.preventDefault();
                // 延时 5 秒后执行原始点击事件
                setTimeout(function() {
                    originalEventListener.call(this, event);
                }, 5000);
            };
        }
        // 调用原始的 addEventListener 函数
        originalAddEventListener.call(this, eventName, eventListener, options);
    };
})();
这个脚本会在所有的网页上运行,并对所有点击事件添加延时逻辑。当点击事件发生时,它会阻止默认行为,并在 5 秒钟后执行原始点击事件。

Q:5 秒后没有执行原始点击事件
A:非常抱歉,可能是我在代码中遗漏了一些细节,导致延时逻辑没有按照预期工作。以下是更新后的代码,它应该可以正确地实现您想要的功能:

javascript
Copy code
// ==UserScript==
// @name         Delay All Clicks by 5 Seconds
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Delays all clicks on the webpage by 5 seconds before executing the original action.
// @author       Your Name
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 重写 addEventListener 函数
    const originalAddEventListener = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function(eventName, eventListener, options) {
        // 如果是点击事件,则添加延时逻辑
        if (eventName === "click") {
            const originalEventListener = eventListener;
            eventListener = function(event) {
                // 阻止默认行为
                event.preventDefault();
                // 保存 this 指向
                const thisObject = this;
                // 延时 5 秒后执行原始点击事件
                setTimeout(function() {
                    originalEventListener.call(thisObject, event);
                }, 5000);
            };
        }
        // 调用原始的 addEventListener 函数
        originalAddEventListener.call(this, eventName, eventListener, options);
    };
})();
更新后的代码会在保存 this 指向,确保在延时函数中调用 originalEventListener 时,this 指向正确的目标元素。如果您还有其他问题,请随时问我。

Q:已经测试过,依然不行。
A:非常抱歉,让您失望了。那么,我建议您尝试一下下面这个脚本,它是通过覆盖 HTMLElement.prototype.click 方法实现的,能够正确地延迟所有点击事件:

javascript
Copy code
// ==UserScript==
// @name         Delay All Clicks by 5 Seconds
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Delays all clicks on the webpage by 5 seconds before executing the original action.
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const originalClick = HTMLElement.prototype.click;

    HTMLElement.prototype.click = function() {
        const element = this;
        setTimeout(() => {
            originalClick.call(element);
        }, 5000);
    };
})();
这个脚本会覆盖所有元素的 click 方法,将原始方法保存下来并延迟 5 秒执行。请注意,这种方式可能会导致某些网页上的某些功能出现问题。如果有问题,请尝试禁用或删除此脚本。如果仍然存在问题,请让我知道,我将尽力帮助您解决。

Q:测试过了,没有延时效果。
A:非常抱歉,让您失望了。以下是另一种实现方式,它是通过监听所有点击事件并阻止默认行为来实现的。当点击事件发生时,它会阻止默认行为,并在 5 秒后模拟一个新的点击事件,以触发原始点击事件。

javascript
Copy code
// ==UserScript==
// @name         Delay All Clicks by 5 Seconds
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Delays all clicks on the webpage by 5 seconds before executing the original action.
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('click', function(event) {
        event.preventDefault();
        const element = event.target;
        setTimeout(() => {
            const newEvent = new MouseEvent('click', {
                bubbles: true,
                cancelable: true,
                view: window
            });
            element.dispatchEvent(newEvent);
        }, 5000);
    }, true);
})();
这个脚本会在所有网页上监听 click 事件,并阻止默认行为。当点击事件发生时,它会延迟 5 秒,然后模拟一个新的点击事件,以触发原始点击事件。请注意,这种方式可能会导致某些网页上的某些功能出现问题。如果有问题,请尝试禁用或删除此脚本。如果仍然存在问题,请让我知道,我将尽力帮助您解决。

Q:
您给出的如下油猴脚本,为何没有执行原来的操作,比如 URL 跳转
// ==UserScript==
// @name         Delay All Clicks by 5 Seconds
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Delays all clicks on the webpage by 5 seconds before executing the original action.
// @author       Your Name
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 重写 addEventListener 函数
    const originalAddEventListener = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function(eventName, eventListener, options) {
        // 如果是点击事件,则添加延时逻辑
        if (eventName === "click") {
            const originalEventListener = eventListener;
            eventListener = function(event) {
                // 阻止默认行为
                event.preventDefault();
                // 保存 this 指向
                const thisObject = this;
                // 延时 5 秒后执行原始点击事件
                setTimeout(function() {
                    originalEventListener.call(thisObject, event);
                }, 5000);
            };
        }
        // 调用原始的 addEventListener 函数
        originalAddEventListener.call(this, eventName, eventListener, options);
    };
})();

A:
非常抱歉,这个脚本没有执行原来的操作是因为它没有保存原来的事件处理函数并在延时后执行。以下是修复后的脚本:

javascript
Copy code
// ==UserScript==
// @name         Delay All Clicks by 5 Seconds
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Delays all clicks on the webpage by 5 seconds before executing the original action.
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const originalAddEventListener = EventTarget.prototype.addEventListener;

    EventTarget.prototype.addEventListener = function(eventName, eventListener, options) {
        if (eventName === "click") {
            const originalEventListener = eventListener;
            eventListener = function(event) {
                event.preventDefault();
                const thisObject = this;
                const originalEventCallback = function() {
                    originalEventListener.call(thisObject, event);
                };
                setTimeout(originalEventCallback, 5000);
            };
        }
        originalAddEventListener.call(this, eventName, eventListener, options);
    };
})();
这个脚本重写了 EventTarget 原型链上的 addEventListener 方法,对于点击事件,它保存了原始事件处理函数并添加了延时逻辑。在延时结束后,它会执行保存的原始事件处理函数,以完成原始操作。希望这次可以满足您的需求。如果仍然存在问题,请让我知道,我将尽力帮助您解决。


Q:依然没有执行原来的操作,比如 URL 跳转
A:非常抱歉,还是没有达到预期效果。以下是另一种实现方式,它是通过修改原始的 EventTarget 对象上的 dispatchEvent 方法来实现的。当点击事件发生时,它会重写 dispatchEvent 方法并添加延时逻辑,以延迟触发原始点击事件。

javascript
Copy code
// ==UserScript==
// @name         Delay All Clicks by 5 Seconds
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Delays all clicks on the webpage by 5 seconds before executing the original action.
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const originalDispatchEvent = EventTarget.prototype.dispatchEvent;

    EventTarget.prototype.dispatchEvent = function(event) {
        if (event.type === "click") {
            event.preventDefault();
            const thisObject = this;
            setTimeout(function() {
                originalDispatchEvent.call(thisObject, event);
            }, 5000);
        } else {
            originalDispatchEvent.call(this, event);
        }
    };
})();
这个脚本重写了 EventTarget 原型链上的 dispatchEvent 方法,在点击事件发生时添加了延时逻辑。当延时结束后,它会触发原始点击事件。请注意,这种方式可能会影响网页上其他的事件处理函数。如果有问题,请尝试禁用或删除此脚本。如果仍然存在问题,请让我知道,我将尽力帮助您解决。


Q:不限制使用方式,环境为 Chrome 浏览器,实现网页所有点击都延时 5 秒后,再执行原来的操作,帮我写成油猴脚本。
A:好的,以下是在 Chrome 浏览器上使用油猴脚本实现网页所有点击都延时 5 秒后再执行原来操作的代码:

首先,在 Chrome 浏览器上安装 Tampermonkey 插件: https://www.tampermonkey.net/

在 Tampermonkey 的用户脚本管理页面,点击“新建脚本”按钮,新建一个脚本。

将以下代码复制粘贴到脚本编辑器中,并保存。

javascript
Copy code
// ==UserScript==
// @name         Delay All Clicks by 5 Seconds
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Delays all clicks on the webpage by 5 seconds before executing the original action.
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const originalAddEventListener = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function(eventName, eventListener, options) {
        if (eventName === "click") {
            const originalEventListener = eventListener;
            eventListener = function(event) {
                event.preventDefault();
                const thisObject = this;
                setTimeout(function() {
                    originalEventListener.call(thisObject, event);
                }, 5000);
            };
        }
        originalAddEventListener.call(this, eventName, eventListener, options);
    };
})();
保存脚本并刷新页面,现在您应该可以看到所有的点击都被延迟了 5 秒钟。请注意,这个脚本可能会影响网页上其他的事件处理函数,如果出现问题,请尝试禁用或删除此脚本。如果仍然存在问题,请让我知道,我将尽力帮助您解决。
1749 次点击
所在节点    JavaScript
3 条回复
vivipure
2023-04-24 19:23:00 +08:00
延迟点击这个功能应该实现不了。无法对通过 addEventListener 绑定的事件进行拦截。 延迟跳转倒可以,可以更改 window.open 方法,或者代理下 location.href
Track13
2023-04-24 19:35:39 +08:00
跳转不能放异步里。
复制文字在 ios 也不能在异步里。
runze
2023-04-24 21:13:32 +08:00
有没有可能是你这个需求本来就难以实现?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/935129

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX