Created
March 3, 2020 16:20
-
-
Save gutchom/8a7200617c8aa53b17c354ca0fd8cdcb to your computer and use it in GitHub Desktop.
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
(() => { | |
return ` | |
"abort scrolling" | |
clearInterval(${scrollToTheEnd()}); | |
"total images count" | |
document.querySelectorAll('.AdaptiveMedia img').length; | |
` | |
function scrollToTheEnd(interval = 800) { | |
const theEnd = document.querySelector('#timeline .stream-end') | |
const id = setInterval(() => { | |
if (document.defaultView.getComputedStyle(theEnd, '').display === 'none') { | |
window.scrollTo(0, document.body.scrollHeight) | |
} else { | |
clearInterval(id) | |
downloadAll() | |
} | |
}, interval) | |
return id | |
} | |
function download(url, name = url.split('/').pop()) { | |
return fetch(new Request(url)) | |
.then(res => res.blob()) | |
.then(blob => { | |
const $a = document.createElement('a') | |
$a.href = URL.createObjectURL(blob) | |
$a.download = name | |
$a.click() | |
URL.revokeObjectURL($a.href) | |
delete $a | |
}) | |
} | |
async function downloadAll(interval = 100) { | |
const images = Array.from(document.querySelectorAll('.stream-item .tweet')) | |
.map(tweet => { | |
const unixtime = parseInt(tweet.querySelector('[data-time-ms]').dataset.timeMs) | |
const date = (new Date(unixtime)).toLocaleDateString('ja-JP').replace(/\//g, '-') | |
const { dataset: { screenName, tweetId }} = tweet | |
return Array.from(tweet.querySelectorAll('.AdaptiveMedia img')).map(({src}, i, {length}) => ({ | |
url: src, | |
name: `${screenName}-${date}-${tweetId}${length ? `-${i+1}` : ''}.${src.split('.').pop()}`, | |
})) | |
}).flat() | |
function wait(msec, func, ...args) { | |
return new Promise(result => setTimeout(() => result(func(...args)), msec)) | |
} | |
for (const {url, name} of images) { | |
await wait(interval, download, url, name) | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment