-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrape.js
57 lines (45 loc) · 1.42 KB
/
scrape.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
// 🔶 1. Setup XHR interception
const originalXHR = window.XMLHttpRequest
const responses = []
window.XMLHttpRequest = function () {
const xhr = new originalXHR()
const originalOpen = xhr.open
const originalSend = xhr.send
xhr.open = function (method, url) {
xhr._url = url
originalOpen.apply(this, arguments)
}
xhr.send = function () {
xhr.addEventListener('readystatechange', () => {
if (xhr.readyState === 4 && xhr._url.includes('Following')) responses.push(xhr.responseText)
})
originalSend.apply(this, arguments)
}
return xhr
}
// 🔶 2. Scroll until the end (triggers all XHR queries)
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const autoScroll = async () => {
let prevScrollHeight = document.body.scrollHeight
let totalScrolled = 0
const scrollDistance = 100
const maxScrollRetries = 10
let retries = 0
while (retries < maxScrollRetries) {
window.scrollBy(0, scrollDistance)
totalScrolled += scrollDistance
await delay(100)
if (prevScrollHeight === document.body.scrollHeight) {
retries++
} else {
prevScrollHeight = document.body.scrollHeight
retries = 0
}
}
}
await autoScroll()
// 🔶 3. Extract relevant data as need, and give a convenient way to exfiltrate data (ex: copy to clipboard)
responses.map((r) => {
const j = JSON.parse(r)
// ...
})