Skip to content

Instantly share code, notes, and snippets.

@magasine
Last active December 15, 2023 13:13
Show Gist options
  • Save magasine/7bf3e50ed8ef90a895b41f181856422e to your computer and use it in GitHub Desktop.
Save magasine/7bf3e50ed8ef90a895b41f181856422e to your computer and use it in GitHub Desktop.
! Quote to whatsapp - bookmarklet
(function () {
const pageTitle = document.title;
const selectedText = window.getSelection().toString();
const currentUrl = location.href;
const targetUrl = `https://www.addtoany.com/add_to/printfriendly?linkurl=${encodeURIComponent(
currentUrl
)}`;
const separator = "\n(...)\n";
const quote = `*${pageTitle}*\n${separator}${selectedText}${separator}\nSource: ${currentUrl}\nReadable: ${targetUrl}`;
async function copyToClipboard(text) {
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
alert("Conteúdo copiado!\n\n" + text);
} else {
let tempInput = document.createElement("textarea");
tempInput.value = text;
tempInput.style.position = "fixed";
tempInput.style.left = "-999999px";
tempInput.style.top = "-999999px";
document.body.appendChild(tempInput);
tempInput.focus();
tempInput.select();
if (document.execCommand("copy")) {
alert("Conteúdo copiado!\n\n" + text);
} else {
alert("Erro ao copiar");
}
tempInput.remove();
}
} catch (error) {
alert("Erro ao copiar: " + error);
}
}
copyToClipboard(quote);
})();
// bookmarklet_title: ! Quote to whatsapp
// bookmarklet_about: How to use: Select text from any web page and launch the bookmarklet in the bookmarks bar.
// The text with title and urls will be copied to clipboard and then can be pasted into whatsapp.
@magasine
Copy link
Author

magasine commented Nov 19, 2022

Encoded for bookmarks bar

javascript:(()%3D%3E%7B!function()%7Bconst%20e%3Ddocument.title%2Co%3Dwindow.getSelection().toString()%2Ct%3Dlocation.href%3B!async%20function(e)%7Btry%7Bif(navigator.clipboard%26%26window.isSecureContext)await%20navigator.clipboard.writeText(e)%2Calert(%22Conte%C3%BAdo%20copiado!%5Cn%5Cn%22%2Be)%3Belse%7Blet%20o%3Ddocument.createElement(%22textarea%22)%3Bo.value%3De%2Co.style.position%3D%22fixed%22%2Co.style.left%3D%22-999999px%22%2Co.style.top%3D%22-999999px%22%2Cdocument.body.appendChild(o)%2Co.focus()%2Co.select()%2Cdocument.execCommand(%22copy%22)%3Falert(%22Conte%C3%BAdo%20copiado!%5Cn%5Cn%22%2Be)%3Aalert(%22Erro%20ao%20copiar%22)%2Co.remove()%7D%7Dcatch(e)%7Balert(%22Erro%20ao%20copiar%3A%20%22%2Be)%7D%7D(%60*%24%7Be%7D*%5Cn%5Cn(...)%5Cn%24%7Bo%7D%5Cn(...)%5Cn%5CnSource%3A%20%24%7Bt%7D%5CnReadable%3A%20%24%7B%60https%3A%2F%2Fwww.addtoany.com%2Fadd_to%2Fprintfriendly%3Flinkurl%3D%24%7BencodeURIComponent(t)%7D%60%7D%60)%7D()%3B%7D)()%3B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment