Greasy Fork

Greasy Fork is available in English.

View Full Twitter Image

Undo Twitter's insistence to down-res images when viewing on its dedicated page and add a button to download the full image without the weird file extensions which don't count as actual images.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         View Full Twitter Image
// @version      2.0.1
// @description  Undo Twitter's insistence to down-res images when viewing on its dedicated page and add a button to download the full image without the weird file extensions which don't count as actual images.
// @author       ForgottenUmbrella
// @match        https://pbs.twimg.com/media/*
// @grant        none
// @noframes
// @namespace    http://greasyfork.icu/users/83187
// ==/UserScript==

"use strict";

// Returns the URL to the original image file (as a string), given a `location` object.
function ogImageUrl(location) {
    let url = new URL(location.href);
    const isNewFormat = url.search.length > 0;
    if (isNewFormat) {
        // The old URL format is https://pbs.twimg/media/hash.jpg:orig.
        // The new URL format is https://pbs.twimg.com/media/hash?format=jpg&name=orig.
        let params = new URLSearchParams(url.search);
        params.set("name", "orig");
        url.search = params.toString();
        return url.href;
    }
    return url.href.replace(":large", "") + ":orig";
}

// Returns the filename of the original image, given a `location` to said image.
function imageName(location) {
    const filename = location.pathname.split("/").pop();
    // Remove the ":orig" tag if Twitter is using the old URL format.
    return filename.replace(":orig", "");
}

// Saves the webpage/file being viewed as `filename`.
function download(filename) {
    let element = document.createElement("a");
    // The `download` attribute only works if `href` is set.
    element.href = location.href;
    element.download = filename;
    element.style.display = "none";
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
}

// View original image file.
const isOgImage = location.href.includes("orig");
if (!isOgImage) {
    location.assign(ogImageUrl(location));
}
// Add spacing between image and download button.
const image = document.getElementsByTagName("img")[0];
const spacing = document.createElement("p");
document.body.insertBefore(spacing, image);
// Add download button.
let button = document.createElement("button");
button.innerHTML = "Download";
button.onclick = () => void download(imageName(location));
document.body.insertBefore(button, spacing);