Last active
January 3, 2021 05:20
-
-
Save syuilo/66d17a307cd417297190f6b776a0698b 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
// auto-fanbox-downloader.js | |
// (c) syuilo 2021 | |
// 注: 私用の際はCORSを無効にしておく必要があります | |
// e.g. https://chrome.google.com/webstore/detail/cors-unblock/lfhmikememgdcahcdlaciloancbhjino | |
// 投稿一覧ページにおける各投稿のクラス | |
const SELECTOR = '.sc-11axwx2-0.bXrzaX'; | |
const els = document.querySelectorAll(SELECTOR); | |
for (let i = 0; i < els.length; i++) { | |
const el = els[i]; | |
const link = el.children[0]; | |
const url = link.href; | |
const button = document.createElement('button'); | |
button.innerText = 'download'; | |
button.addEventListener('click', () => { | |
const w = window.open(url, url, 'height=500, width=500'); | |
w.addEventListener('load', () => { | |
downloadPage(w); | |
}); | |
button.innerText = 'downloaded'; | |
}); | |
el.appendChild(button); | |
} | |
function wait(ms) { | |
return new Promise((res, rej) => { | |
setTimeout(res, ms) | |
}); | |
} | |
async function downloadPage(w) { | |
// 投稿ページにおける各画像のクラス | |
const SELECTOR = '.xvj0xk-1.gcZCGE'; | |
function download(url, fileName) { | |
const xhr = new XMLHttpRequest(); | |
xhr.open('GET', url, true); | |
xhr.withCredentials = true; | |
xhr.responseType = 'blob'; | |
xhr.onload = function(e) { | |
if (this.status !== 200) { | |
alert('Failed to download image :('); | |
} | |
const imgUrl = URL.createObjectURL(this.response); | |
const link = document.createElement('a'); | |
link.href = imgUrl; | |
link.download = fileName; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link) | |
}; | |
xhr.send(); | |
} | |
// lazyloadを解決するためスクロール | |
await wait(1000); | |
const height = Math.max(w.document.body.scrollHeight, w.document.body.offsetHeight, w.document.documentElement.clientHeight, w.document.documentElement.scrollHeight, w.document.documentElement.offsetHeight); | |
w.scroll({ top: height, behavior: 'smooth' }); | |
await wait(1000); | |
w.scroll({ top: 0, behavior: 'smooth' }); | |
await wait(1000); | |
const els = w.document.querySelectorAll(SELECTOR); | |
for (let i = 0; i < els.length; i++) { | |
const el = els[i]; | |
const url = el.href; | |
const ext = url.split('.').pop(); | |
const fileName = `${url.split('/')[5]}-${(i + 1).toString().padStart(4, '0')}.${ext}`; | |
download(url, fileName); | |
await wait(300); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment