Last active
September 30, 2023 01:59
-
-
Save awsr/e6adbfdc34a4e026e0503b35fcd25464 to your computer and use it in GitHub Desktop.
Restore Twitter Logo
This file contains 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 Make Twitter Twitter Again | |
// @namespace https://gist.github.com/awsr | |
// @version 1.0.3 | |
// @description Because nobody's willing to tell Elon Musk he's an idiot | |
// @author awsr | |
// @match https://twitter.com/* | |
// @match https://x.com/* | |
// @downloadURL https://gist.github.com/awsr/e6adbfdc34a4e026e0503b35fcd25464/raw/twitterbirb.user.js | |
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAByUlEQVQ4T2NkoBAwUqifAW7Au3fv+IWEhD6iGwgSn3/jY+KnXz8EFfg57icaKi4AiYHUgdTDDfBfdXpjgKrIepACZEMMZh65cOE3hz4DEwsDw++fDA78vw88+PxLscZWpSnFQG4e3AD77Y9PHbz1zDRBnn1Bn51cAcj080/eGxge+HKegYMbYebXjwwCXFxfDjpy+euL8O6DG7Dg7te6hPM/Gxk+vGLgf/fwfaCa+EY+Pvavk96JZ6N7a505d2WgDNsURkbGLwgXrLl+9CCDuBVY8d8/DAyf30FoYSmMcP4fLCgK1PwGJAE34MKrb2EOR7/P/fCXkQdfzBjyM98578qvClMDN+D///88dZe/TZ5890cIPkPm6LM2pajy1mMYABLY//RDeOGVPy0XPjOpYHMFyPZzLny2QOe/wGoA0BX8Z9/88m659Stjw/PftsiGgDQfcuBL4WVlPIgsDvYCKGEUHXo0Yf31p/4fmTgEGcTkGBh4heHq4uXZt08x4OrkYWE4ArT9L4YBQJuZgYKqF97+tVj34qcjTIE8J8sLZzGGcwo8bBeBYrfRNaPEAogDCkgghbCageEnkP8eqBFEYwXUy0z44h6fHAAkJ6sRJxhsYgAAAABJRU5ErkJggg== | |
// @grant window.onurlchange | |
// @run-at document-idle | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
const svgRef = () => document.querySelector("a[href='/'] svg") || document.querySelector("a[href='/home'] svg"); | |
const config = { childList: true, subtree: true, attributes: true, attributeFilter: ["style", "fill", "d"] }; | |
const watcher = new MutationObserver(replaceLogo); | |
let loaderLoop = null; | |
const tracker = new WeakSet(); | |
function replaceLogo() { | |
const cachedSvgRef = svgRef(); | |
if (tracker.has(cachedSvgRef)) { | |
return true; | |
} | |
if (cachedSvgRef) { | |
const path = cachedSvgRef.getElementsByTagName("path")[0]; | |
if (path) { | |
watcher.disconnect(); | |
cachedSvgRef.setAttribute("viewBox", "0 0 72 72"); | |
cachedSvgRef.style.fill = "#1d9bf0"; | |
path.setAttribute("d", "M67.812 16.141a26.246 26.246 0 0 1-7.519 2.06 13.134 13.134 0 0 0 5.756-7.244 26.127 26.127 0 0 1-8.313 3.176A13.075 13.075 0 0 0 48.182 10c-7.229 0-13.092 5.861-13.092 13.093 0 1.026.118 2.021.338 2.981-10.885-.548-20.528-5.757-26.987-13.679a13.048 13.048 0 0 0-1.771 6.581c0 4.542 2.312 8.551 5.824 10.898a13.048 13.048 0 0 1-5.93-1.638c-.002.055-.002.11-.002.162 0 6.345 4.513 11.638 10.504 12.84a13.177 13.177 0 0 1-3.449.457c-.846 0-1.667-.078-2.465-.231 1.667 5.2 6.499 8.986 12.23 9.09a26.276 26.276 0 0 1-16.26 5.606A26.21 26.21 0 0 1 4 55.976a37.036 37.036 0 0 0 20.067 5.882c24.083 0 37.251-19.949 37.251-37.249 0-.566-.014-1.134-.039-1.694a26.597 26.597 0 0 0 6.533-6.774z"); | |
tracker.add(cachedSvgRef); | |
watcher.observe(cachedSvgRef.parentElement, config); | |
console.log("[script] Twitter logo replaced."); | |
return true; | |
} | |
} | |
return false; | |
} | |
function loader(maxLoops = 10) { | |
// Clear any existing backup loop | |
if (loaderLoop) { | |
clearInterval(loaderLoop); | |
loaderLoop = null; | |
} | |
// Try replacing logo and run backup loop if unsuccessful | |
if (!replaceLogo()) { | |
let count = 0; | |
loaderLoop = setInterval(() => { | |
count++; | |
if (count > maxLoops || replaceLogo()) { | |
clearInterval(loaderLoop); | |
} | |
}, 500); | |
} | |
} | |
// Re-check in case of a full reload of the page content | |
if (window.onurlchange === null) { | |
window.addEventListener("urlchange", () => {loader(6)}); | |
} | |
else { | |
window.addEventListener("popstate", () => {loader(6)}); | |
} | |
// Init | |
loader(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment