Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

// ==UserScript== // @name Mouse Click Behavior // @namespace http://tampermonkey.net/…

  1. User

    // ==UserScript== // @name Mouse Click Behavior // @namespace http://tampermonkey.net/ // @version 0.1 // @description Single click to open in the current page, double click to open in a new page. // @author You // @match :///* // @grant none // ==/UserScript==

    (function() { let clickCount = 0; let singleClickTimer; let ctrlDown = false;

    // 监听键盘按下事件
    document.addEventListener("keydown", function(event) {
        if (event.key === "Control") {
            ctrlDown = true;
        }
    });
    
    // 监听键盘松开事件
    document.addEventListener("keyup", function(event) {
        if (event.key === "Control") {
            ctrlDown = false;
        }
    });
    
    document.addEventListener("click", function(event) {
        // 如果ctrl键被按下,不执行任何操作
        if (ctrlDown) {
            return;
        }
    
        // 只有当点击的是链接时,才执行下面的逻辑
        if (event.target.tagName.toLowerCase() === 'a' && event.target.href) {
            event.preventDefault();
            clickCount++;
    
            if (clickCount === 1) {
                singleClickTimer = setTimeout(function() {
                    clickCount = 0;
                    window.location.href = event.target.href;
                }, 213);
            } else if (clickCount === 2) {
                clearTimeout(singleClickTimer);
                clickCount = 0;
                window.open(event.target.href, "_blank");
            }
        }
    }, false);
    

    })();

    为什么以上油猴代码在哔哩哔哩网站上不能点击取消连播的按钮,请逐步分析并改正代码

  2. Assistant

    该油猴脚本主要功能是单击链接在当前页面打开,双击链接在新标签页打开。但是哔哩哔哩(Bilibili)网站的"取消连播"按钮不是一个普通的链接(<a>标签),它可能是一个按钮 (<button>) 或者其他可点击的元素(例如 <div>)。

    脚本中对事件是这样进行处理的:

    1. 当用户在没有按下Control键的情况下点击了一个 <a> 标签时,脚本会阻止默认行为(即跳转链接),并设置一个单击计数器。
    2. 在第一次点击后,设置一个延时,在213毫秒后将当前页面跳转到链接地址,除非用户在这段时间内再次点击链接,触发第二次点击。
    3. 如果发生第二次点击,脚本会取消之前的延时,并在新标签页中打开链接。

    而在哔哩哔哩上,"取消连播"按钮很可能不是 <a> 标签且没有 href 属性,因此脚本对这个按钮的点击没有任何反应。

    要解决这个问题,我们需要改写脚本,让它支持非 <a> 标签的点击事件,同时避免对非链接的正常点击作出反应。

    以下是修改后的脚本:

    // ==UserScript==
    // @name         Mouse Click Behavior Enhanced
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  Single click to open in the current page, double click to open in a new page, support for non-anchor elements.
    // @author       You
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        let clickCount = 0;
        let singleClickTimer;
    
        document.addEventListener("click", function(event) {
            
            if (event.target.tagName.toLowerCase() === 'a' && event.target.href) {
                event.preventDefault(); // 阻止默认行为,即链接跳转
                handleClick(event.target.href, event); // 处理点击事件
            } else if(event.target.closest('a') && event.target.closest('a').href) {
                // 如果点击的元素包含在一个a标签之内,使用closest找到最近的a标签
                event.preventDefault(); // 同样防止默认的跳转行为
                handleClick(event.target.closest('a').href, event);
            } else {
                // 非链接点击,不做处理
            }
    
        }, false);
    
        function handleClick(href, event) {
            clickCount++;
            if (clickCount === 1) {
                singleClickTimer = setTimeout(function() {
                    clickCount = 0;
                    window.location.href = href;
                }, 213);
            } else if (clickCount === 2) {
                clearTimeout(singleClickTimer);
                clickCount = 0;
                window.open(href, "_blank");
            }
        }
    
    })();
    

    这个修改版的脚本会检测点击事件发生时事件对象的tagName,如果不是<a>标签但是它的父级元素中包含<a>标签(通过closest函数实现),则会取closest函数返回的最近的<a>标签的href属性并进行处理。

    请注意,不同网站的结构和使用到的JavaScript可能会影响到脚本的工作情况,因此在实际部署时可能需要根据具体网站进一步调整。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.