tiktok-dl/scraper.js

129 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2020-01-19 13:16:46 +00:00
optStrings = {
selectors: {
2020-01-19 19:15:42 +00:00
feedLoading: 'div.tiktok-loading.feed-loading',
2020-01-19 13:16:46 +00:00
modalArrowLeft: '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',
2020-01-20 17:42:34 +00:00
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',
videoPlayer: 'div.video-card-container > div > div > video',
videoShareInput: 'div.content-container.border > div.copy-link-container > input',
2020-01-20 17:42:34 +00:00
videoCaption: 'div.content-container.border > div.video-meta-info > h1',
videoSoundLink: 'div.content-container.border > div.video-meta-info > h2.music-info > a',
2020-01-19 13:16:46 +00:00
},
classes: {
2020-01-19 19:15:42 +00:00
feedVideoItem: 'video-feed-item-wrapper',
2020-01-19 13:16:46 +00:00
modalCloseDisabled: 'disabled',
},
tags: {
resultTag: 'video_urls',
resultParentTag: 'body',
},
attributes: {
src: "src",
},
};
2020-01-19 02:11:53 +00:00
createVidUrlElement = function(outputObj) {
2020-01-19 13:16:46 +00:00
var urlSetElement = document.createElement(optStrings.tags.resultTag);
urlSetElement.innerText = JSON.stringify(outputObj);
2020-01-19 13:16:46 +00:00
document.getElementsByTagName(optStrings.tags.resultParentTag)[0].appendChild(urlSetElement);
2020-01-19 02:11:53 +00:00
}
2020-01-19 13:16:46 +00:00
buldVidUrlArray = function(finishCallback) {
2020-01-19 19:15:42 +00:00
var feedItem = document.getElementsByClassName(optStrings.classes.feedVideoItem)[0];
2020-01-19 02:11:53 +00:00
feedItem.click();
2020-01-19 13:16:46 +00:00
var videoArray = [];
2020-01-19 02:11:53 +00:00
var intervalID = window.setInterval(x => {
2020-01-19 13:16:46 +00:00
videoArray.push(getCurrentModalVideo());
var arrowRight = document.querySelectorAll(optStrings.selectors.modalArrowLeft)[0];
if (arrowRight.classList.contains(optStrings.classes.modalCloseDisabled)) {
2020-01-19 02:11:53 +00:00
window.clearInterval(intervalID);
2020-01-19 13:16:46 +00:00
document.querySelector(optStrings.selectors.modalClose).click();
finishCallback(videoArray);
2020-01-19 02:11:53 +00:00
} else {
arrowRight.click();
}
2020-01-19 19:15:42 +00:00
}, 20);
2020-01-19 02:11:53 +00:00
};
2020-01-19 13:16:46 +00:00
getCurrentModalVideo = function() {
var modalPlayer = document.querySelector(optStrings.selectors.modalPlayer);
var vidUrl = modalPlayer.getAttribute(optStrings.attributes.src);
var shareLink = document.querySelector(optStrings.selectors.modalShareInput).value;
2020-01-20 17:42:34 +00:00
var caption = document.querySelector(optStrings.selectors.modalCaption).textContent;
var soundLink = document.querySelector(optStrings.selectors.modalSoundLink);
var soundHref = soundLink.getAttribute("href");
var soundText = soundLink.text;
2020-01-19 13:16:46 +00:00
return {
url: vidUrl,
2020-01-20 17:42:34 +00:00
shareLink: shareLink,
caption: caption,
sound: {
title: soundText,
link: soundHref,
},
2020-01-19 13:16:46 +00:00
};
}
getCurrentVideo = function() {
var player = document.querySelector(optStrings.selectors.videoPlayer);
var vidUrl = player.getAttribute(optStrings.attributes.src);
var shareLink = document.querySelector(optStrings.selectors.videoShareInput).value;
2020-01-20 17:42:34 +00:00
var caption = document.querySelector(optStrings.selectors.videoCaption).textContent;
var soundLink = document.querySelector(optStrings.selectors.videoSoundLink);
var soundHref = soundLink.getAttribute("href");
var soundText = soundLink.text;
return {
url: vidUrl,
2020-01-20 17:42:34 +00:00
shareLink: shareLink,
caption: caption,
sound: {
title: soundText,
link: soundHref,
},
};
}
2020-01-19 02:11:53 +00:00
scrollWhileNew = function(finishCallback) {
var state = { count: 0 };
var intervalID = window.setInterval(x => {
var oldCount = state.count;
2020-01-19 19:15:42 +00:00
state.count = document.getElementsByClassName(optStrings.classes.feedVideoItem).length;
2020-01-19 02:11:53 +00:00
if (oldCount !== state.count) {
window.scrollTo(0, document.body.scrollHeight);
} else {
2020-01-19 19:15:42 +00:00
if (document.querySelector(optStrings.selectors.feedLoading)) {
window.scrollTo(0, document.body.scrollHeight);
return;
}
2020-01-19 02:11:53 +00:00
window.clearInterval(intervalID);
2020-01-19 19:15:42 +00:00
finishCallback(createVidUrlElement);
2020-01-19 02:11:53 +00:00
}
}, 1000);
};
2020-01-19 13:16:46 +00:00
bootstrapIteratingVideos = function() {
2020-01-19 19:15:42 +00:00
scrollWhileNew(buldVidUrlArray);
return 'bootstrapIteratingVideos';
2020-01-19 13:16:46 +00:00
};
bootstrapGetCurrentVideo = function() {
var video = getCurrentVideo();
createVidUrlElement(video);
return 'bootstrapGetCurrentVideo';
}
2020-01-19 02:11:53 +00:00
init = () => {
const newProto = navigator.__proto__;
delete newProto.webdriver;
navigator.__proto__ = newProto;
2020-01-19 19:15:42 +00:00
return 'script initialized';
2020-01-19 02:11:53 +00:00
};
2020-01-19 19:15:42 +00:00
init();