Last active
October 16, 2024 05:16
-
-
Save kiemrong08/13805647fc656d1ba60937930ef74e2c to your computer and use it in GitHub Desktop.
Script Get all image URL of x.com twitter profile chrome devtool
This file contains hidden or 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
// Array to store image URLs | |
let imageUrls = []; | |
// Function to get image URLs from the current page | |
function getImageUrls() { | |
const imgElements = document.querySelectorAll('img[src^="https://pbs.twimg.com/media/"]'); | |
let newUrls = []; | |
imgElements.forEach(img => { | |
const src = img.src; | |
if (src.includes('name=')) { | |
const baseUrl = src.split('?')[0]; | |
const largeUrl = baseUrl + '?format=jpg&name=large'; | |
if (!imageUrls.includes(largeUrl)) { | |
newUrls.push(largeUrl); | |
} | |
} | |
}); | |
return newUrls; | |
} | |
// Function to scroll the page by one viewport height | |
function scrollOnePage() { | |
const viewportHeight = window.innerHeight; | |
window.scrollBy(0, viewportHeight); | |
} | |
// Function to wait for images to load | |
function waitForImages() { | |
return new Promise(resolve => { | |
let checkInterval = setInterval(() => { | |
let images = document.querySelectorAll('img[src^="https://pbs.twimg.com/media/"]'); | |
let allLoaded = true; | |
for (let img of images) { | |
if (!img.complete) { | |
allLoaded = false; | |
break; | |
} | |
} | |
if (allLoaded) { | |
clearInterval(checkInterval); | |
resolve(); | |
} | |
}, 100); | |
}); | |
} | |
// Main function to get all image URLs | |
async function getAllImageUrls() { | |
let consecutiveEmptyScrolls = 0; | |
while (true) { | |
// Scroll down one viewport | |
scrollOnePage(); | |
// Wait for the page to load new content and images to finish loading | |
await new Promise(resolve => setTimeout(resolve, 1000)); | |
await waitForImages(); | |
// Get image URLs from the current viewport | |
const newUrls = getImageUrls(); | |
if (newUrls.length === 0) { | |
consecutiveEmptyScrolls++; | |
} else { | |
consecutiveEmptyScrolls = 0; | |
imageUrls = imageUrls.concat(newUrls); | |
console.log(`Found ${newUrls.length} new image URLs. Total: ${imageUrls.length}`); | |
} | |
// If no new URLs in 3 consecutive scrolls, end the process | |
if (consecutiveEmptyScrolls >= 3) { | |
console.log("Reached the end of the page or no more new images."); | |
break; | |
} | |
} | |
console.log("All image URLs:"); | |
console.log(imageUrls.join('\n')); | |
} | |
// Run the script | |
getAllImageUrls(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment