Skip to content

Instantly share code, notes, and snippets.

@hexium310
Last active October 31, 2024 21:28
Show Gist options
  • Save hexium310/45cad452f14d6a6bab3ddee581e6eba7 to your computer and use it in GitHub Desktop.
Save hexium310/45cad452f14d6a6bab3ddee581e6eba7 to your computer and use it in GitHub Desktop.
A bookmarklet to save an image opening on Twitter
javascript:(async()=>{const url=location.href;const[,tweetId,nth]=url.match(/\/status\/(\d+)\/photo\/(\d)/);const modal=document.querySelector('div[style*="transition-duration:"][style*="background-color:"]');const ul=[...modal.querySelectorAll("ul")].find(elm=>[...elm.style].includes("transform"));const imgQuery='img[src^="https://pbs.twimg.com/media/"]';const img=ul!==undefined?[...ul.querySelectorAll(imgQuery)][nth-1]:[...modal.querySelectorAll(imgQuery)].find(elm=>elm.parentNode.getAttribute("data-testid")!=="tweetPhoto");const imgUrl=img.src;const blob=await fetch(imgUrl).then(res=>res.blob());const blobType=blob?.type;if(!/^image\/.+$/.test(blobType)){return}const objectUrl=URL.createObjectURL(blob);const anchor=document.createElement("a");anchor.href=objectUrl;anchor.download=`twitter_${tweetId}_${nth}`;anchor.click();URL.revokeObjectURL(objectUrl)})();
(async () => {
const url = location.href;
const [, tweetId, nth] = url.match(/\/status\/(\d+)\/photo\/(\d)/);
const modal = document.querySelector('div[style*="transition-duration:"][style*="background-color:"]');
const ul = [...modal.querySelectorAll('ul')].find((elm) => [...elm.style].includes('transform'));
const imgQuery = 'img[src^="https://pbs.twimg.com/media/"]';
const img = ul !== undefined
? [...ul.querySelectorAll(imgQuery)][nth - 1]
: [...modal.querySelectorAll(imgQuery)].find((elm) => elm.parentNode.getAttribute('data-testid') !== 'tweetPhoto');
const imgUrl = img.src;
const blob = await fetch(imgUrl).then((res) => res.blob());
const blobType = blob?.type;
if (!/^image\/.+$/.test(blobType)) {
return;
}
(async () => {
const url = location.href;
const [, tweetId, nth] = url.match(/\/status\/(\d+)\/photo\/(\d)/);
const modal = document.querySelector('div[style*="transition-duration:"][style*="background-color:"]');
const ul = [...modal.querySelectorAll('ul')].find((elm) => [...elm.style].includes('transform'));
const imgQuery = 'img[src^="https://pbs.twimg.com/media/"]';
const img = ul !== undefined
? [...ul.querySelectorAll(imgQuery)][nth - 1]
: [...modal.querySelectorAll(imgQuery)].find((elm) => elm.parentNode.getAttribute('data-testid') !== 'tweetPhoto');
const imgUrl = img.src;
const blob = await fetch(imgUrl).then((res) => res.blob());
const blobType = blob?.type;
if (!/^image\/.+$/.test(blobType)) {
return;
}
const objectUrl = URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = objectUrl;
anchor.download = `twitter_${tweetId}_${nth}`;
anchor.click();
URL.revokeObjectURL(objectUrl);
})();
const objectUrl = URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = objectUrl;
anchor.download = `twitter_${tweetId}_${nth}`;
anchor.click();
URL.revokeObjectURL(objectURL);
})();
@hexium310
Copy link
Author

hexium310 commented Oct 28, 2024

npx uglify-js -- save-image.js | tac <<< javascript: | paste -sd '' -

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