Created
October 7, 2023 11:46
-
-
Save thedivtagguy/a2c32e9d5d91137ee754a03019ef11c3 to your computer and use it in GitHub Desktop.
TamperMonkey script that restores text and descriptions to Twitter Links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Enhance Twitter News Links | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Extract and display aria-label over news link images on Twitter | |
// @author aman.bh, ChatGPT | |
// @match https://twitter.com/* | |
// @grant GM_addStyle | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Adding custom styles | |
GM_addStyle(` | |
/* Gradient overlay */ | |
.gradient-overlay { | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
background: linear-gradient(to top, rgba(0, 0, 0, 1) 0%, transparent 60%); | |
z-index: 5; | |
} | |
/* Style for the extracted title */ | |
.extracted-title { | |
width: 100%; | |
position: absolute; | |
bottom: 10px; | |
left: 10px; | |
font-size: 18px; | |
font-weight: bold; | |
color: #FFF; | |
display: flex; | |
align-items: start; | |
z-index: 10; | |
flex-direction: column; | |
gap:8px; | |
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; | |
} | |
/* Style for the domain name */ | |
.extracted-domain { | |
display: flex; | |
align-items: center; | |
font-size: 14px; | |
color: rgba(255, 255, 255, 0.7); | |
} | |
/* Style for the logo */ | |
.logo-svg { | |
height: 16px; | |
width: 16px; | |
margin-right: 5px; | |
} | |
`); | |
const svgLogo = ` | |
<svg class="logo-svg" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"> | |
<path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6H6a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-6m-7 1l9-9m-5 0h5v5"></path> | |
</svg> | |
`; | |
function enhanceTwitterLinks() { | |
document.querySelectorAll('div[data-testid="card.layoutLarge.media"] a[role="link"][aria-label]').forEach(link => { | |
if(link.querySelector('.gradient-overlay')) return; | |
const gradientDiv = document.createElement('div'); | |
gradientDiv.className = 'gradient-overlay'; | |
link.appendChild(gradientDiv); | |
const [domain, ...titleArr] = link.getAttribute('aria-label').split(' '); | |
const titleDiv = document.createElement('div'); | |
titleDiv.className = 'extracted-title'; | |
titleDiv.innerHTML = `<span class="extracted-domain">${svgLogo + domain}</span><span>${titleArr.join(' ')}</span>`; | |
link.appendChild(titleDiv); | |
link.querySelector('div[dir="ltr"] > span')?.remove(); | |
}); | |
} | |
const observer = new MutationObserver(mutationsList => { | |
mutationsList.forEach(mutation => { | |
if (mutation.type === 'childList') enhanceTwitterLinks(); | |
}); | |
}); | |
observer.observe(document.body, { childList: true, subtree: true }); | |
enhanceTwitterLinks(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
Install Tampermonkey by searching for it like any other addon. Chrome, Firefox, whatever your poison is.
Usage
With the script installed, go to https://twitter.com/.
The script will automatically run on Twitter, and you should notice saner links returning. Lemme know if it doesn't work.