Last active
September 26, 2023 01:27
-
-
Save magasine/cf646aa22a7626211fcc4587cebd76dd to your computer and use it in GitHub Desktop.
Text fragment to post on twitter - Bookmarklet
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
(async () => { | |
const selectedText = document.title + '\n...\n"' + getSelection().toString() + '"\n...'; | |
const newUrl = new URL(location); | |
newUrl.hash = `:~:text=${encodeURIComponent(selectedText)}`; | |
const tweetText = encodeURIComponent(selectedText); | |
const tweetUrl = encodeURIComponent(newUrl); | |
const twitterUrl = `https://twitter.com/intent/tweet?text=${tweetText}&url=${tweetUrl}`; | |
// Encurtar a URL usando o Bitly | |
const bitlyAccessToken = 'SEU_ACCESS_TOKEN_DO_BITLY'; // Substitua pelo seu token do Bitly | |
const bitlyUrl = 'https://api-ssl.bitly.com/v4/shorten'; | |
try { | |
const response = await fetch(bitlyUrl, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${bitlyAccessToken}` | |
}, | |
body: JSON.stringify({ long_url: twitterUrl }) | |
}); | |
if (response.ok) { | |
const data = await response.json(); | |
const shortenedTwitterUrl = data.link; | |
window.open(shortenedTwitterUrl, '_blank'); | |
} else { | |
console.error('Falha ao encurtar a URL usando Bitly.'); | |
} | |
} catch (error) { | |
console.error('Erro ao encurtar a URL:', error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment