-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
81 lines (72 loc) · 2.09 KB
/
util.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
70
71
72
73
74
75
76
77
78
79
80
81
/*
We need to turn the date in the post into a seralizable string.
*/
function serializePosts(posts) {
let ret = [];
for (let i = 0; i < posts.length; i++) {
let post = posts[i];
let date;
let newPost = {senderName: post.senderName,
senderProfile: post.senderProfile,
time: post.time.toJSON(), message: post.message};
ret.push(newPost);
}
return ret;
}
function deserializePosts(posts) {
let ret = [];
for (let i = 0; i < posts.length; i++) {
let post = posts[i];
let newPost = {senderName: post.senderName,
senderProfile: post.senderProfile,
time: new Date(post.time), message: post.message};
ret.push(newPost);
}
return ret;
}
function parsePost(post) {
var nameElement = post.getElementsByTagName("a")[2];
var name = nameElement.innerText;
var profileLink = nameElement.href;
var timeElement = post.getElementsByTagName("abbr")[0];
var unixTime = timeElement.dataset.utime;
var date = new Date(unixTime * 1000);
var messageElement = post.getElementsByClassName("userContent")[0];
var messageHtml;
if (typeof messageElement !== "undefined") {
messageHtml = messageElement.innerHTML;
} else {
messageHtml = "";
}
return {senderName: name, senderProfile: profileLink,
time: date, message: messageHtml};
}
function filterPosts(posts) {
var ret = [];
for (let i = 0; i < posts.length; i++) {
let post = posts[i];
if (typeof post.getElementsByClassName("userContent")[0] !== "undefined" &&
typeof post.getElementsByTagName("abbr")[0] !== "undefined") {
ret.push(post);
}
}
return ret;
}
function scrapePosts(startDate, stopDate) {
let posts = filterPosts(document.getElementsByClassName("fbUserContent"));
let ret = [];
for (let i = 0; i < posts.length; i++) {
let postElement = posts[i];
let post;
try {
post = parsePost(postElement);
} catch (err) {
console.log(err);
continue;
}
if (startDate <= post.time && post.time <= stopDate) {
ret.push(post);
}
}
return ret;
}