优化 Twitter/X.com 图片显示,自动加载原图并转换 WebP 格式为 JPEG
- // ==UserScript==// @name Twitter强制图片最高品质// @version 1.2// @description 优化 Twitter/X.com 图片显示,自动加载原图并转换 WebP 格式为 JPEG// @author YongZhe// @match https://twitter.com/*// @match https://x.com/*// @match https://pbs.twimg.com/*// @grant GM_addStyle// @namespace http://tampermonkey.net/// ==/UserScript==GM_addStyle(`iframe#twitter_plus_setting {max-width: 300px !important;max-height: 300px !important;}`);(function () {'use strict';/*** 获取原始图片 URL* @param {string} imgUrl - 图片地址* @returns {string|false} - 原图链接或失败标识*/const getOriginUrl = (imgUrl) => {const match = imgUrl.match(/https:\/\/(pbs\.twimg\.com\/media\/[a-zA-Z0-9\-_]+)(\?format=|.)(jpg|jpeg|png|webp)/i);if (!match) return false;if (match[3].toLowerCase() === 'webp') match[3] = 'jpg';return `https://${match[1]}.${match[3]}?name=orig`;};/*** 优化单张图片* @param {HTMLImageElement} img - 图片元素*/const optimizeImage = (img) => {const originUrl = getOriginUrl(img.src);if (originUrl) {img.src = originUrl;img.dataset.original = originUrl; // 标记已处理}};// 图片页面直连原图if (window.location.href.includes('twimg.com/image')) {window.location.replace(getOriginUrl(window.location.href));}// 主站图片优化if (window.location.hostname.includes('twitter.com') || window.location.hostname.includes('x.com')) {document.addEventListener('DOMContentLoaded', () => {document.querySelectorAll('img').forEach(optimizeImage);});const observer = new MutationObserver(() => {document.querySelectorAll('img:not([data-original])').forEach(optimizeImage);});observer.observe(document.body, {childList: true,subtree: true,attributes: ['src']});}})();