// ==UserScript== // @name 推特获取原图 // @namespace https://github.com/MuXia-0326/twitter-auto-original-picture // @version 1.5 // @description 推特在新标签页打开图片自动原图 // @author Mossia // @icon https://raw.githubusercontent.com/MuXia-0326/drawio/master/angri.png // @match https://pbs.twimg.com/* // @match https://twitter.com/* // @grant GM_setClipboard // @license MIT // @downloadURL none // ==/UserScript== (function () { 'use strict'; const copyUpdate = true; //载入css样式 const css = `/* From www.lingdaima.com */ .twitter-Btn { position: relative; display: inline-block; padding: 10px; text-align: center; font-size: 18px; letter-spacing: 1px; text-decoration: none; color: rgb(29, 155, 240); background: transparent; cursor: pointer; transition: ease-out 0.5s; border: 2px solid rgb(29, 155, 240); border-radius: 10px; box-shadow: inset 0 0 0 0 rgb(29, 155, 240); } .twitter-Btn:hover { color: white; box-shadow: inset 0 -100px 0 0 rgb(29, 155, 240); } .twitter-Btn:active { transform: scale(0.9); } .twitter-Btn:hover svg { fill: white; } .twitter-Btn:active svg, .twitter-Btn svg { fill: rgb(29, 155, 240); } .Btn { position: absolute; top: 2px; right: 2px; } .svgClass { display: flex; } `; let styleTag = document.createElement('style'); styleTag.innerText = css; document.head.append(styleTag); // 获取当前页面的URL if (window.location.hostname === 'pbs.twimg.com') { let newUrl = replaceImageSizeName(window.location.href); if (newUrl !== window.location.href) { window.location.href = newUrl; } } else if (window.location.hostname === 'twitter.com') { document.js_nsfw = setInterval(main, 100); } function main() { tweetAdd(); imageDetailsAdd(); copy(); } function copy() { if (copyUpdate) { // 替换复制按钮的url let firstChildDiv = document.querySelector('div[data-testid="Dropdown"] > div:first-child'); // 确保第一个子元素是一个 div if (firstChildDiv) { firstChildDiv.addEventListener('click', function (e) { navigator.clipboard .readText() .then((text) => { // console.log('剪贴板的内容:', text); if (text.indexOf('fixupx') === -1) { // 修改剪贴板的内容 GM_setClipboard(text.replace(/x/g, 'fixupx'), 'text'); } }) .catch((err) => { console.log('无法读取剪贴板的内容:', err); }); }); } } } function imageDetailsAdd() { // 图片详情页的按钮 let classDetailsName = 'div[data-testid="swipe-to-dismiss"] div[aria-label="图像"]'; let tempDetails = [...new Set(baseSelector(document, classDetailsName))]; for (let i = 0; i < tempDetails.length; i++) { setDetailsBtn([tempDetails[i]]); } } function tweetAdd() { let tweets = document.querySelectorAll('[data-testid="cellInnerDiv"]'); for (let tweet of tweets) { let className = 'div[aria-label="图像"][data-testid="tweetPhoto"]'; let imageDiv = tweet.querySelector(className); if (imageDiv === null) { continue; } let temp = [...new Set(baseSelector(tweet, className))]; let like = tweet .querySelector('div.css-175oi2r.r-1iusvr4.r-16y2uox.r-1777fci.r-kzbkwu') .children[3].querySelector('div') .querySelector('div').children[2]; for (let i = 0; i < temp.length; i++) { setBtn([temp[i]], like); } } } function baseSelector(parentEle, selector) { let items = parentEle.querySelectorAll(selector); return Array.from(items).filter((item) => { let node = getParentByNum(item, 5).querySelectorAll('div[data-nsfw]'); return !(node && node.length > 0); }); } function setBtn(node, like) { for (let container of node) { let images = container.querySelectorAll('img'); for (let image of images) { let imageUrl = image.getAttribute('src'); let classText = image.getAttribute('class') + getRandomIntExclusive(10); let buttonHtml = getBtnHtml(classText); let parentElement = getParentByNum(image, 5); let newUrl = replaceImageSizeName(imageUrl); appendBtn(parentElement, newUrl, buttonHtml, classText, like); } } } function setDetailsBtn(node) { for (let container of node) { // console.log(container); let images = container.querySelectorAll('img'); for (let image of images) { let imageUrl = image.getAttribute('src'); let classText = image.getAttribute('class') + getRandomIntExclusive(10); let buttonHtml = getBtnHtml(classText); let like = getParentByNum(container, 4) .nextElementSibling.querySelector('div') .querySelector('div') .querySelector('div').children[2]; let newUrl = replaceImageSizeName(imageUrl); appendBtn(container, newUrl, buttonHtml, classText, like); } } } function getBtnHtml(classText) { const buttonHtml = `