Greasy Fork

Greasy Fork is available in English.

Twitter 拖曳切换图像

使用鼠标拖曳来切换上一张或下一张图像, 点击图像即可关闭图像

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Twitter Image Switch With Drag
// @name:ja      Twitter Image Switch With Drag
// @name:zh-CN   Twitter 拖曳切换图像
// @name:zh-TW   Twitter 拖曳切換圖片
// @description         Switch between previous and next images by dragging the mouse, click on the image to close it.
// @description:ja      マウスをドラッグして前後の画像を切り替え、画像をクリックして閉じます。
// @description:zh-cn   使用鼠标拖曳来切换上一张或下一张图像, 点击图像即可关闭图像
// @description:zh-tw   使用滑鼠拖曳來切換上一張或下一張圖片, 點擊圖片即可關閉圖片
// @namespace    none
// @version      0.1.9
// @author       ShanksSU
// @match        https://x.com/*
// @match        https://mobile.x.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addStyle
// @compatible   Chrome
// @license      MIT
// ==/UserScript==
function ImageSwitchWithDrag() {
    function clickBtn(name) {
        const btn = document.querySelector(`div[aria-labelledby="modal-header"] button[aria-label*="${name}"]`);
        if (btn) {
            btn.click();
            return true;
        }
        return false;
    }

    GM_addStyle('img { -webkit-user-drag: none; }');

    window.addEventListener('wheel', function({ deltaY, target: { tagName, baseURI } }) {
        if (tagName == 'IMG' && /\/photo\//.test(baseURI)) {
            if (deltaY < 0) clickBtn('Previous slide');
            else if (deltaY > 0) clickBtn('Next slide');
        }
    });

    let initialXCoordinate = 0;
    let clickCount = 0;
    const doubleClickThreshold = 300;
    window.addEventListener('mousedown', function({ clientX }) {
        initialXCoordinate = clientX;
    });

    window.addEventListener(
        'mouseup',
        function({ button, clientX, target: { tagName, baseURI }, timeStamp }) {
            if (button !== 0 || !(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
            const distanceMovedX = clientX - initialXCoordinate;
            clickCount++;
            setTimeout(() => {
                if (clickCount === 1) {
                    if (Math.abs(distanceMovedX) === 0) {
                        if (baseURI === window.location.href) {
                            clickBtn('Close');
                        }
                    } else if (distanceMovedX > 0) {
                        clickBtn('Previous slide');
                    } else if (distanceMovedX < 0) {
                        clickBtn('Next slide');
                    }
                } else if (clickCount === 2) {
                    clickBtn('Likes. Like');
                }
                clickCount = 0;
            }, doubleClickThreshold);
        }
    );

}
ImageSwitchWithDrag();