Last active
October 31, 2024 21:28
-
-
Save hexium310/45cad452f14d6a6bab3ddee581e6eba7 to your computer and use it in GitHub Desktop.
A bookmarklet to save an image opening on Twitter
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
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)})(); |
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 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); | |
})(); |
Author
hexium310
commented
Oct 28, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment