Skip to content

Instantly share code, notes, and snippets.

@wongmjane wongmjane/bookmarklet.js
Last active Dec 4, 2019

Embed
What would you like to do?
Guesstimate the original tweet of a shared video on Twitter
javascript:(()=>{const match=document.title.match(/(\bhttps?:\/\/t\.co\/[a-zA-Z0-9\-\.as]+)/g);if(match!==null){window.location=match.slice(-1)[0]}})()
/**
* Guesstimate the original tweet of a video that’s been shared as other tweet.
* This should work on publish.twitter.com unless something breaks on Twitter’s oEmbed API
*
* @author @wongmjane
*/
{
const tweetUrlOrId = prompt('ID/URL of the Tweet?')
if (tweetUrlOrId === null) throw new Error('User bailed.')
const matches = tweetUrlOrId.match(
/^(?:https:\/\/twitter\.com\/@?(?:\w){1,15}\/status\/)?(\d+$)/,
)
if (matches === null) throw new Error('Invalid format for a tweet ID/URL.')
fetch(
`https://publish.twitter.com/oembed\?url\=https://twitter.com/i/status/${matches[1]}`,
)
.then(res => res.json())
.then(text => {
console.log(
...Array.prototype.filter
.call(
new DOMParser()
.parseFromString(text.html, 'text/html')
.querySelectorAll('.twitter-tweet > p[lang] > a[href]'),
a => /^pic\.twitter\.com\//.test(a.innerText),
)
.filter(a => /^https:\/\/t\.co\//.test(a))
.map(a => a.href),
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.