Twitter Images Origin Retriever

Retrieves the original image file from a Tweet or an image hosted by Twitter

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitter Images Origin Retriever
// @version      0.0.1
// @description  Retrieves the original image file from a Tweet or an image hosted by Twitter
// @author       Metal-Mighty
// @match        https://twitter.com/*/status/*
// @match        https://pbs.twimg.com/media/*
// @grant        none
// @namespace    https://github.com/Metal-Mighty/Twitter-Images-Origin-Retriever
// ==/UserScript==
(function () {
  "use strict";
  var queryVars = function(str) {
    return str.replace(/^\?/, '').split('&').map(x => x.split('=')).reduce((a, [k, v]) => { a[k] = v; return a; }, {});
  };
  // Check if this page contains a single image whose source is also the location.
  var image = document.getElementsByTagName('img')[0];
  var imgSet;
  var config = { attributes: true, childList: true, subtree: true };
  
  const callback = function(mutationsList, observer) {
    for (let mutation of mutationsList){
      if (mutation.target.className.includes("css-1dbjc4n")) {
        imgSet = mutation.target.getElementsByTagName('img');
        
        var i;
        for (i = 0; i < imgSet.length; i++) {
          var imgSrc = imgSet[i].src;
          if (imgSrc.includes("https://pbs.twimg.com/media/")
          && !imgSrc.match(/name=orig/)) {
            imgSet[i].src = imgSrc.replace(/name=(\w+)/g, 'name=orig');
          }
        } 
      }
    }
  }
  
  const observer = new MutationObserver(callback);
  
  // Start observing the target node for configured mutations
  observer.observe(document.body, config);
  
  if (image && image.getAttribute('src') == location.href) {
    var imgHref = location.href;
    var pathname = location.pathname;
    // Check if we already have the orig modifier
    if (!imgHref.match(/:orig$/) && !imgHref.match(/name=orig/)) {
      // Trim modifiers.
      var idx = pathname.lastIndexOf(':');
      if (idx >= 0)
      pathname = pathname.substr(0, idx);
      // Check if we need to append the file type.
      var format = queryVars(location.search).format;
      if (format && !location.pathname.endsWith(format))
      pathname += '.' + format;
      // Add the modifier.
      pathname += ':orig';
      window.location = pathname;
    }
  }
})();