Greasy Fork is available in English.

Twitter image viewing enhancement

Make Twitter photo viewing more humane

Από την 27/03/2020. Δείτε την τελευταία έκδοση.

// 注意 NOTICE
// v0.5.1 开始包含一些重大更新,如果你的推特界面语言不是 简体中文、繁体中文、英语、日语 中的一个,请访问脚本主页以了解这一变更,否则你可能无法正常使用。
// There are some significant changes in v0.5.1, please visit the homepage of this script for more information if your twitter display language is not one of English, Japanese, Simplified Chinese, Traditional Chinese.
// ==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.5.1
// @description        Make Twitter photo viewing more humane
// @description:zh-CN  让推特图片浏览更加人性化
// @description:zh-TW  讓 Twitter 照片瀏覽更人性化
// @author       Jindai Kirin
// @include      https://twitter.com/*
// @license      MIT
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @grant        GM_openInTab
// @run-at       document-end
// @require      https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js
// @require      https://cdn.jsdelivr.net/npm/jquery-mousewheel@3.1.13/jquery.mousewheel.min.js
// ==/UserScript==

(function() {
    'use strict';

    const safetyParseJSON = str => {
        try {
            return JSON.parse(str);
        } catch (error) {}
    };

    const defaultLabelsByLang = {
        zh: { close: '关闭', prev: '上一个', next: '下一步' },
        'zh-Hant': { close: '關閉', prev: '上一個', next: '下一步' },
        en: { close: 'Close', prev: 'Previous', next: 'Next' },
        ja: { close: '閉じる', prev: '前', next: '次' },
    };

    const defaultLabels = defaultLabelsByLang[$('html').attr('lang')] || defaultLabelsByLang.zh;
    const labels = (() => {
        const setting = GM_getValue('labels', '');
        return setting ? safetyParseJSON(setting) || defaultLabels : defaultLabels;
    })();
    console.log('aria-labels', labels);
    GM_registerMenuCommand('Set aria-labels', () => {
        let input, list;
        let error = false;
        do {
            const current = GM_getValue('labels', '');
            input = prompt(`Please input the aria-label of Close, Previous, Next button and join them by comma (,). Submit an empty string will reset it to default.${error ? '\n\nINPUT ERROR' : ''}`, input || current ? Object.values(safetyParseJSON(current) || {}).join(',') : '');
            if (input === null) return;
            input = input.trim();
            if (input.length === 0) list = Object.values(defaultLabels);
            else list = input.split(',').map(label => label.trim());
            error = list.length !== Object.keys(labels).length;
        } while (error);
        Object.keys(labels).forEach((key, index) => {
            labels[key] = list[index];
        });
        GM_setValue('labels', input.length ? JSON.stringify(labels) : '');
        console.log('aria-labels-setting', GM_getValue('labels'));
        console.log('aria-labels', labels);
    });

    const getBtnByLabel = label => $(`div[aria-labelledby="modal-header"] div[aria-label="${label}"]`);

    const closeImgView = () => {
        const $btn = getBtnByLabel(labels.close);
        if ($btn.length) $btn.click();
        else if (confirm("It seems that you haven't set the right aria-labels yet. Please visit the homepage of this script for more information.")) GM_openInTab('https://greasyfork.org/zh-CN/scripts/387918', false);
    };
    const prevImg = () => getBtnByLabel(labels.prev).click();
    const nextImg = () => getBtnByLabel(labels.next).click();

    $(document).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;
    $(document).mousedown(({ clientX, clientY }) => {
        x = clientX;
        y = clientY;
    });
    $(document).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();
        }
    });
})();