Updated `scraper.js`

This commit is contained in:
Pijus Kamandulis 2020-08-16 01:30:48 +03:00
parent e9715c0d40
commit 7a25f521cb
1 changed files with 39 additions and 33 deletions

View File

@ -1,17 +1,15 @@
optStrings = {
selectors: {
feedLoading: 'div.tiktok-loading.feed-loading',
modalArrowRight: 'div.video-card-modal > div > img.arrow-right',
modalClose: '.video-card-modal > div > div.close',
modalPlayer: 'div > div > main > div.video-card-modal > div > div.video-card-big > div.video-card-container > div > div > video',
modalShareInput: '.copy-link-container > input',
modalCaption: 'div.video-card-big > div.content-container > div.video-meta-info > h1',
modalSoundLink: 'div.content-container > div.video-meta-info > h2.music-info > a',
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 > div > video',
videoShareInput: 'div.content-container.border > div.copy-link-container > input',
videoCaption: 'div.content-container.border > div.video-meta-info > h1',
videoSoundLink: 'div.content-container.border > div.video-meta-info > h2.music-info > a',
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: {
@ -41,7 +39,7 @@ currentState = {
checkForErrors = function () {
var titles = document.getElementsByClassName(optStrings.classes.titleMessage);
debugger;
//debugger;
if (titles && titles.length) {
var error = Array.from(titles).find(x => optStrings.tiktokMessages.includes(x.textContent)).textContent;
if (error) {
@ -74,7 +72,7 @@ buldVidUrlArray = function(finishCallback) {
}
}
var arrowRight = document.querySelectorAll(optStrings.selectors.modalArrowRight)[0];
if (arrowRight.classList.contains(optStrings.classes.modalCloseDisabled)) {
if (!arrowRight || arrowRight.classList.contains(optStrings.classes.modalCloseDisabled)) {
window.clearInterval(intervalID);
document.querySelector(optStrings.selectors.modalClose).click();
finishCallback(videoArray);
@ -87,12 +85,12 @@ buldVidUrlArray = function(finishCallback) {
getCurrentModalVideo = function () {
var modalPlayer = document.querySelector(optStrings.selectors.modalPlayer);
var vidUrl = modalPlayer.getAttribute(optStrings.attributes.src);
var shareLink = document.querySelector(optStrings.selectors.modalShareInput).value;
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.getAttribute("href");
var soundText = soundLink.text;
var soundHref = soundLink ? soundLink.getAttribute("href") : '';
var soundText = soundLink ? soundLink.text : '';
return {
url: vidUrl,
@ -107,15 +105,16 @@ getCurrentModalVideo = function() {
};
getCurrentVideo = function () {
//debugger;
if (checkForErrors()) return;
var player = document.querySelector(optStrings.selectors.videoPlayer);
var vidUrl = player.getAttribute(optStrings.attributes.src);
var shareLink = document.querySelector(optStrings.selectors.videoShareInput).value;
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.getAttribute("href");
var soundText = soundLink.text;
var soundHref = soundLink ? soundLink.getAttribute("href") : '';
var soundText = soundLink ? soundLink.text : '';
return {
url: vidUrl,
@ -132,7 +131,9 @@ getCurrentVideo = function() {
scrollBottom = () => window.scrollTo(0, document.body.scrollHeight);
scrollWhileNew = function (finishCallback) {
var state = { count: 0 };
var state = {
count: 0
};
var intervalID = window.setInterval(x => {
scrollBottom();
var oldCount = state.count;
@ -152,7 +153,7 @@ scrollWhileNew = function(finishCallback) {
if (oldCount !== state.count) {
currentState.preloadCount = state.count;
} else {
if (document.querySelector(optStrings.selectors.feedLoading)) {
if (isLoading()) {
return;
}
window.clearInterval(intervalID);
@ -161,6 +162,11 @@ scrollWhileNew = function(finishCallback) {
}, 1000);
};
isLoading = function () {
var loadingElement = document.querySelector(optStrings.selectors.feedLoading);
return loadingElement && loadingElement.getClientRects().length != 0;
}
bootstrapIteratingVideos = function (limit) {
currentState.limit = limit;
scrollWhileNew(buldVidUrlArray);