-
Notifications
You must be signed in to change notification settings - Fork 48
/
scraper.js
189 lines (173 loc) · 6.33 KB
/
scraper.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
optStrings = {
selectors: {
feedLoading: '.tiktok-ui-loading-container',
modalArrowRight: 'div > div.video-card-container > img.arrow-right',
modalClose: 'div > div.video-card-container > img.control-icon.close',
modalPlayer: 'div.video-card-container > div.video-card-browse > video',
modalCaption: 'div.content-container > div.video-infos-container > h1',
modalSoundLink: 'div.content-container > div.video-infos-container > h2.music-info > a',
modalUploader: '.user-username',
videoPlayer: 'div.video-card-container > div > video',
videoCaption: 'div.content-container > div.video-infos-container > h1',
videoSoundLink: 'div.content-container > div.video-infos-container > h2 > a',
videoUploader: '.user-username',
},
classes: {
feedVideoItem: 'video-feed-item-wrapper',
modalCloseDisabled: 'disabled',
titleMessage: 'title',
},
tags: {
resultTag: 'video_urls',
resultParentTag: 'body',
},
attributes: {
src: "src",
},
tiktokMessages: [
"Couldn't find this account",
"No videos yet",
"Video currently unavailable",
],
};
currentState = {
preloadCount: 0,
finished: false,
limit: 0
};
checkForErrors = function () {
var titles = document.getElementsByClassName(optStrings.classes.titleMessage);
//debugger;
if (titles && titles.length) {
var error = Array.from(titles).find(x => optStrings.tiktokMessages.includes(x.textContent)).textContent;
if (error) {
createVidUrlElement("ERR: " + error);
return true;
}
}
return false;
};
createVidUrlElement = function (outputObj) {
var urlSetElement = document.createElement(optStrings.tags.resultTag);
urlSetElement.innerText = JSON.stringify(outputObj);
document.getElementsByTagName(optStrings.tags.resultParentTag)[0].appendChild(urlSetElement);
currentState.finished = true;
};
buldVidUrlArray = function (finishCallback) {
var feedItem = document.getElementsByClassName(optStrings.classes.feedVideoItem)[0];
feedItem.click();
var videoArray = [];
var intervalID = window.setInterval(x => {
videoArray.push(getCurrentModalVideo());
if (currentState.limit > 0) {
if (videoArray.length >= currentState.limit) {
window.clearInterval(intervalID);
document.querySelector(optStrings.selectors.modalClose).click();
finishCallback(videoArray);
}
}
var arrowRight = document.querySelectorAll(optStrings.selectors.modalArrowRight)[0];
if (!arrowRight || arrowRight.classList.contains(optStrings.classes.modalCloseDisabled)) {
window.clearInterval(intervalID);
document.querySelector(optStrings.selectors.modalClose).click();
finishCallback(videoArray);
} else {
arrowRight.click();
}
}, 20);
};
getCurrentModalVideo = function () {
var modalPlayer = document.querySelector(optStrings.selectors.modalPlayer);
var vidUrl = modalPlayer.getAttribute(optStrings.attributes.src);
var shareLink = window.location.href;
var caption = document.querySelector(optStrings.selectors.modalCaption).textContent;
var soundLink = document.querySelector(optStrings.selectors.modalSoundLink);
var uploader = document.querySelector(optStrings.selectors.modalUploader).textContent;
var soundHref = soundLink ? soundLink.getAttribute("href") : '';
var soundText = soundLink ? soundLink.text : '';
return {
url: vidUrl,
shareLink: shareLink,
caption: caption,
uploader: uploader,
sound: {
title: soundText,
link: soundHref,
},
};
};
getCurrentVideo = function () {
//debugger;
if (checkForErrors()) return;
var player = document.querySelector(optStrings.selectors.videoPlayer);
var vidUrl = player.getAttribute(optStrings.attributes.src);
var shareLink = window.location.href;
var caption = document.querySelector(optStrings.selectors.videoCaption).textContent;
var soundLink = document.querySelector(optStrings.selectors.videoSoundLink);
var uploader = document.querySelector(optStrings.selectors.videoUploader).textContent;
var soundHref = soundLink ? soundLink.getAttribute("href") : '';
var soundText = soundLink ? soundLink.text : '';
return {
url: vidUrl,
shareLink: shareLink,
caption: caption,
uploader: uploader,
sound: {
title: soundText,
link: soundHref,
},
};
};
scrollBottom = () => window.scrollTo(0, document.body.scrollHeight);
scrollWhileNew = function (finishCallback) {
var state = {
count: 0
};
var intervalID = window.setInterval(x => {
scrollBottom();
var oldCount = state.count;
state.count = document.getElementsByClassName(optStrings.classes.feedVideoItem).length;
if (currentState.limit > 0) {
if (currentState.preloadCount >= currentState.limit || state.count >= currentState.limit) {
finishCallback(createVidUrlElement);
window.clearInterval(intervalID);
}
}
if (checkForErrors()) {
window.clearInterval(intervalID);
return;
} else if (state.count == 0) {
return;
}
if (oldCount !== state.count) {
currentState.preloadCount = state.count;
} else {
if (isLoading()) {
return;
}
window.clearInterval(intervalID);
finishCallback(createVidUrlElement);
}
}, 1000);
};
isLoading = function () {
var loadingElement = document.querySelector(optStrings.selectors.feedLoading);
return loadingElement && loadingElement.getClientRects().length != 0;
}
bootstrapIteratingVideos = function (limit) {
currentState.limit = limit;
scrollWhileNew(buldVidUrlArray);
return 'bootstrapIteratingVideos';
};
bootstrapGetCurrentVideo = function () {
var video = getCurrentVideo();
createVidUrlElement(video);
return 'bootstrapGetCurrentVideo';
};
init = () => {
const newProto = navigator.__proto__;
delete newProto.webdriver;
navigator.__proto__ = newProto;
return 'script initialized';
};
init();