Twitter 图片查看增强

让推特图片浏览更加人性化

目前为 2020-01-30 提交的版本。查看 最新版本

// ==UserScript==
// @name         Twitter image viewing enhancement
// @name:zh-CN   Twitter 图片查看增强
// @name:zh-TW   Twitter 圖像查看增強
// @icon         https://twitter.com/favicon.ico
// @namespace    https://moe.best/
// @version      0.4.3
// @description        Make Twitter photo viewing more humane
// @description:zh-CN  让推特图片浏览更加人性化
// @description:zh-TW  讓 Twitter 照片瀏覽更人性化
// @author       Jindai Kirin
// @include      https://twitter.com/*
// @license      MIT
// @grant        none
// @run-at       document-end
// @require      https://cdn.bootcss.com/jquery/3.4.1/jquery.slim.min.js
// @require      https://cdn.bootcss.com/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js
// ==/UserScript==

(function() {
    'use strict';

    const btns = () => {
        const $btns = $('div[aria-labelledby="modal-header"] div[aria-haspopup="false"][role="button"]:not([data-testid])');
        return $btns.length === 1 ? { close: $btns } : { prev: $($btns[0]), next: $($btns[1]), close: $($btns[2]) };
    };
    const closeImgView = () => btns().close.click();
    const prevImg = () => {
        const $btn = btns().prev;
        if (!$btn.attr('disabled')) $btn.click();
    };
    const nextImg = () => {
        const $btn = btns().next;
        if (!$btn.attr('disabled')) $btn.click();
    };

    $(window).mousewheel(({ deltaY, target: { tagName, baseURI } }) => {
        if (tagName == 'IMG' && /\/photo\//.test(baseURI)) {
            switch (deltaY) {
                case 1:
                    prevImg();
                    break;
                case -1:
                    nextImg();
                    break;
            }
        }
    });

    let x = 0;
    let y = 0;
    $(window).mousedown(({ clientX, clientY }) => {
        x = clientX;
        y = clientY;
    });
    $(window).mouseup(({ button, clientX, clientY, target: { tagName, baseURI } }) => {
        if (button !== 0 || !(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
        const [sx, sy] = [clientX - x, clientY - y].map(Math.abs);
        const mx = clientX - x;
        if (sx <= 10 && sy <= 10) closeImgView();
        if (sy <= sx) {
            if (mx > 0) prevImg();
            else if (mx < 0) nextImg();
        }
    });
})();