-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.js
42 lines (34 loc) · 1.26 KB
/
parser.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
const queryDOM = element => selector => Array.from(element.querySelectorAll(selector));
const getImageData = img => ({
url: queryDOM(img)('img')[0].getAttribute('data-src')
});
const getVideoData = video => {
const sources = queryDOM(video)('source');
return {
poster: queryDOM(video)('video')[0].getAttribute('poster'),
sources: sources.map(source => ({
src: source.getAttribute('src'),
type: source.getAttribute('type'),
}))
}
};
function parseTweet(tweet) {
const $tweet = queryDOM(tweet);
const images = $tweet('.entity-image').map(getImageData);
const videos = $tweet('.entity-video').map(getVideoData);
// Clean stuff. /!\ Modify the original node.
const ignoreSelector = ['.row', '.entity-image', '.entity-video', '.tw-permalink'].join(',');
$tweet(ignoreSelector).forEach(child => child.remove());
$tweet('a.entity-url').forEach(child => {
child.removeAttribute('data-preview');
child.removeAttribute('class');
});
const tweetHTML = tweet.innerHTML;
return { tweetHTML, images, videos };
}
function getTweets(threadReaderDoc) {
return queryDOM(threadReaderDoc)('.t-main .content-tweet').map(parseTweet);
}
if (typeof exports === 'object' && typeof module !== 'undefined') {
exports.getTweets = getTweets;
}