View Twitter Origin Images

將新版推特的圖片轉為原畫質版本,並可以從右鍵取得圖片網址

2019-07-29 일자. 최신 버전을 확인하세요.

질문, 리뷰하거나, 이 스크립트를 신고하세요.
// ==UserScript==
// @name         View Twitter Origin Images
// @namespace    https://greasyfork.org
// @version      0.0.2
// @description  將新版推特的圖片轉為原畫質版本,並可以從右鍵取得圖片網址
// @author       Pixmi
// @icon         https://i.imgur.com/AmUaAWh.png
// @include      https://twitter.com/*
// @include      https://pbs.twimg.com/media/*
// @run-at       document-end
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */

(function () {
    "use strict";

    if (window.location.href.includes('pbs.twimg.com/media')) {
        const twimg_URL = /https:\/\/pbs\.twimg\.com\/media\/([a-zA-Z0-9\-]+)\.(jpg|jpeg|png)\?name\=orig/;
        let imgHref = location.href;
        if (!twimg_URL.test(imgHref)) {
            let url = imgHref.replace('?format=','.').replace(/\&name=(\w+)/g,'?name=orig');
            window.location.replace(url);
        }
    } else {
        let rootmatch = document.evaluate("//div[@id='react-root']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        let rootnode = rootmatch.singleNodeValue;

        //new Twitter UI
        if (rootnode) {
            let imgSet;
            var callback = function (mutationsList, observer) {
                for (let mutation of mutationsList) {
                    if (mutation.target.className.includes("css-1dbjc4n")) {
                        imgSet = mutation.target.getElementsByTagName('img');
                        let i;
                        for (i = 0; i < imgSet.length; i++) {
                            let imgSrc = imgSet[i].src;
                            if (imgSrc.includes("https://pbs.twimg.com/media/") && !imgSrc.match(/name=orig/)) {
                                console.log(imgSrc);
                                imgSet[i].src = imgSrc.replace('?format=','.').replace(/&name=(\w+)/g,'?name=orig');
                            }
                        }
                    }
                }
            };
            const observeConfig = {
                attributes: true,
                childList: true,
                subtree: true
            };
            const observer = new MutationObserver(callback);

            observer.observe(document.body, observeConfig);
        }
    }
});