Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Last active January 31, 2021 20:48
Show Gist options
  • Select an option

  • Save dieseltravis/1eb912fe1e73f3bd041ccdef342f136f to your computer and use it in GitHub Desktop.

Select an option

Save dieseltravis/1eb912fe1e73f3bd041ccdef342f136f to your computer and use it in GitHub Desktop.
get t.co destination URL
document.addEventListener("mouseover", function tCo(ev) {
const l = ev.target.closest("a[href^='https://t.co/']");
// only run when target is a link with a t.co href
if (l) {
fetch(l.href, { method: "GET" })
.then(function fetchResponse(res) { return res.text(); }) // get response html
.then(function fetchHtml(html) {
//console.log("html:", html);
if (html) {
const u = new DOMParser() // convert response to node
.parseFromString(html, 'text/html')
.head // head node
.firstChild // noscript node
.firstChild // meta node
.content // content attribute
.replace(/^0;URL=/, ""); // strip off meta refresh prefix
//console.log("url:", u);
if (u) {
// blink the link to reset status text
const d = l.style.visibility;
l.style.visibility = "hidden";
setTimeout(function linkReset() {
// change link's href and reset display
l.href = u;
l.style.visibility = d;
}, 0);
}
}
})
.catch(function fetchError(ex) { console.log("error:", ex); });
}
});
// replace hrefs of t.co links with target on hover (WIP)
// line #5 errors:
//Content Security Policy: The page’s settings blocked the loading of a resource at https://t.co/WcHpQjsp1X?amp=1 (“connect-src”).
const tDotCo = url => {
return fetch(url, { method: "GET", redirect: "follow" })
.then(res => res.text() ) // get response html
.then(html => new DOMParser().parseFromString(html, 'text/html')
.head // head node
.firstChild // noscript node
.firstChild // meta node
.content // content attribute
.replace(/^0;URL=/, "") // strip off meta refresh prefix
);
};
// usage:
// tDotCo("https://t.co/HlHXi6KF4Y?amp=1").then(f => console.log(f) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment