Bypassing Twitter's t.co domain
Lorraine Lee
Posted on July 24, 2019
I found this post by searching "twitter" on dev.to b/c Twitter. It's now 2019 and there's a "new" twitter. I thought to comment on said post but it's an old post and I'm not sure about netiquette concerning adding new wine to old bottles, so I decided on a post rather than a comment. The present post is my first post-level post on dev.to so please consider being gentle.
Link wrapper politics of twitter seems largely unchanged, though. I too am an extension developer (in my case FF but I at least try to port mine to Chromium). I too basically move the URL from the "title" attribute to the "href" one for the inline outlinks (see code in repo):
$("a[title^='http']").each(function () {
$(this).attr("href", $(this).attr("title"));
$(this).removeAttr("title");
});
But there are also tweets with embedded graphics in a box that links to some article on the web. For these there is no unwrapped link supplied anywhere in any of the nesting-doll-like Twitter elements (I checked). So I brute-forced the d-t.co-ization of these links by using the words in the title of the linked article, and the domain name of the website where it's hosted (these details are supplied by Twitter) as seen here:
$(".r-1mi0q7o").each(function () {
if ($(this).text() != "") { // kludge
let title = $(this).children().first().text();
let query = title.split(/\W+/).join(" ").trim().replace(/ /g, "+");
let tld = $(this).children().last().text();
let site = encodeURIComponent(tld);
let url = `https://duckduckgo.com/?q=${query}+site%3A${site}`;
$(this).closest("a").attr("href", url);
}
});
The element with class name r-1mi0q7o
(subject to change without notice, I'm quite sure) has three top-level children, whose textContent
s are article title, article sample text and article domain (without path), respectively. This jQuery expression is rather brute force, and makes me jump through the intermediate hoop of clicking a link on a search result, but on the plus side I find misses to be very rare, and much more often than not the article in question is the very first one in the search results.
My browser extension is called Twitter Usability Suite and is available for Firefox at https://addons.mozilla.org/en-US/firefox/addon/twitter-usability-suite/.
I have not yet succeeded at running it on Chromium. There seems to be a difference between the browsers either with the attachment of the content script or the firing of the page load event, but I'm not entirely sure what the problem is. The content script seems to be running before the elements it's looking for have arrived.
Posted on July 24, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.