Last active
September 18, 2024 01:49
-
-
Save TypeA2/7e9298641dec292a39cc83143e8a51f2 to your computer and use it in GitHub Desktop.
Copy Tweet content as DText
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 Copy Tweet text as DText | |
// @namespace Violentmonkey Scripts | |
// @match *://twitter.com/*/status/* | |
// @match *://x.com/*/status/* | |
// @grant none | |
// @version 1.1 | |
// @author TypeA2 | |
// @description 7/3/2023, 2:22:48 PM | |
// @updateURL https://gist.github.com/TypeA2/7e9298641dec292a39cc83143e8a51f2/raw/copy-dtext.user.js | |
// @downloadURL https://gist.github.com/TypeA2/7e9298641dec292a39cc83143e8a51f2/raw/copy-dtext.user.js | |
// ==/UserScript== | |
'use strict'; | |
window.addEventListener("load", () => { | |
/* Just poll in the most cursed way imaginable */ | |
let found_any = false; | |
const interval = setInterval(() => { | |
try { | |
const tweets = document.querySelectorAll("article"); | |
let updated_any = false; | |
for (const tweet of tweets) { | |
found_any = true; | |
if (tweet.dataset.hasDTextLink) { | |
updated_any = true; | |
continue; | |
} | |
const body = tweet.querySelector("[data-testid=tweetText]"); | |
const style = document.createElement("style"); | |
style.textContent = ` | |
a.copy-dtext-link { | |
display: block; | |
cursor: pointer; | |
color: rgb(29, 155, 240); | |
} | |
a.copy-dtext-link:hover { | |
text-decoration: underline; | |
} | |
`; | |
const trigger = document.createElement("a"); | |
trigger.classList.add("copy-dtext-link"); | |
trigger.textContent = "Copy as DText"; | |
trigger.addEventListener("click", _ => { | |
let text = ""; | |
let start_search = false; | |
for (const e of body.children) { | |
if (!start_search) { | |
if (e.tagName === "HR") { | |
start_search = true; | |
} | |
continue; | |
} | |
if (e.children.length === 0) { | |
/* Raw text or emoji */ | |
if (e.tagName === "IMG") { | |
text += e.alt; | |
} else { | |
text += e.textContent; | |
} | |
} else { | |
/* Hashtag, mentions or URLs */ | |
let link = null; | |
if (e.tagName === "A") { | |
link = e; | |
} else { | |
link = e.querySelector("a"); | |
} | |
let linked = link.textContent; | |
const identifier = linked[0]; | |
if (identifier === "@") { | |
/* Mention */ | |
linked = linked.substring(1); | |
text += `"@${linked}":[https://twitter.com/${linked}]`; | |
} else if (identifier === "#") { | |
/* Hashtag */ | |
linked = linked.substring(1); | |
text += `"#${linked}":[https://twitter.com/hashtag/${linked}]`; | |
} else { | |
/* Normal link (?) */ | |
if (linked.endsWith("…")) { | |
/* Filter out elipsis */ | |
linked = linked.substring(0, linked.length - 1); | |
} | |
text += `<${linked}>`; | |
} | |
} | |
} | |
navigator.clipboard.writeText(text); | |
trigger.textContent = "Copied!"; | |
}); | |
const separator = document.createElement("hr"); | |
body.prepend(style, trigger, separator); | |
tweet.dataset.hasDTextLink = true; | |
updated_any = true; | |
} | |
if (found_any && updated_any) { | |
clearInterval(interval); | |
} | |
} catch (e) { | |
/* Ignore */ | |
} | |
}, 50); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment