-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (62 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
async function download() {
let urls = [];
let names = [];
document.body.scrollIntoView();
// css prop : currentSrc
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// replace 360x360 with orig to have the original image
const normalizeUrl = (url) => url.replace(/\&name=[^&]+/, '&name=orig');
// always returns jpeg
const getExtension = (url) => new URL(url).searchParams.get('format');
const download = (url, fname) =>
fetch(url)
.then((resp) => resp.blob())
.then((blob) => {
let bloburl = window.URL.createObjectURL(blob);
let a = document.createElement('a');
a.style.display = 'none';
a.href = bloburl;
if (!fname) {
fname = new URL(url).pathname.split('/').pop() + '.jpg';
// fname += getExtension(url);
} else {
fname += '_' + new URL(url).pathname.split('/').pop() + '.jpg';
// '_' + new URL(url).pathname.split('/').pop() + getExtension(url);
}
a.download = fname;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(bloburl);
})
.catch(() => console.error(`${url} download failed`));
do {
// somehow without var script throw error when running in browser
var newImgs = Array.from(document.querySelectorAll('img')).filter(
(img) =>
/\.mp4|media/.test(img.src) && !urls.includes(normalizeUrl(img.src)),
);
for (const img of newImgs) {
// fname equals to author @name
let filename = '';
let url = normalizeUrl(img.src);
urls.push(url);
try {
filename = img
.closest('[data-testid="tweet"]')
.innerText.split('\n')
.slice(1, 2);
// '.' +
// getExtension(url);
} catch (e) {
// if there is an error in getting the author name, just use the img id as the filename
filename = '';
}
names.push(filename);
console.log(urls.length, url, filename);
img.scrollIntoView();
download(url, filename);
await sleep(700);
}
} while (newImgs.length);
console.log(Array.from(urls).join('\n'));
};