-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.js
69 lines (64 loc) · 2.01 KB
/
collect.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
console.log("Collection script loaded");
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
let start = new Date(request["start"]);
let stop = new Date(request["stop"]);
let profile_name = location.pathname.replace(/^\/([^\/]*).*$/, '$1');
console.log("start: " + start);
console.log("stop: " + stop);
loadPosts(start)
.then(loadAggregate)
.then(waitTime)
.then(function() {
console.log("Done scrolling");
let posts = scrapePosts(start, stop);
let toSend = {posts: serializePosts(posts),
start: request["start"],
stop: request["stop"],
profile_name: profile_name};
console.log("Sending " + posts.length + " posts to event page");
chrome.runtime.sendMessage(toSend);
});
});
async function loadAggregate() {
let show_all = document.getElementsByClassName("showAll");
while (show_all.length !== 0) {
console.log("Loading aggregated posts.");
let show_all_links = show_all[0].getElementsByTagName("a");
show_all_links[0].click();
await waitTime();
show_all = document.getElementsByClassName("showAll");
}
}
function waitTime() {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, 2000);
});
}
function earliestPostDate() {
var posts = document.getElementsByClassName("fbUserContent");
for (let i = posts.length - 1; i >=0; i--) {
try {
let lastPost = parsePost(posts[i]);
if (lastPost.message.length === 0) {
// There is no message, so not wall post or not loaded.
continue;
}
console.log(lastPost.time);
return lastPost.time;
} catch (err) {
console.log("error parsing post: " + posts[i]);
}
}
return new Date();
}
async function loadPosts(startDate) {
while (startDate < earliestPostDate()) {
console.log("Loading posts");
window.scrollTo(0,document.body.scrollHeight);
await waitTime();
}
console.log("Finished loading posts.")
}