tiktok-dl/scraper.js

107 lines
3.5 KiB
JavaScript
Raw 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',
videoPlayer: 'div.video-card-container > div > div > video',
videoShareInput: 'div.content-container.border > div.copy-link-container > input',
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;
return {
url: vidUrl,
shareLink: shareLink
};
}
getCurrentVideo = function() {
var player = document.querySelector(optStrings.selectors.videoPlayer);
var vidUrl = player.getAttribute(optStrings.attributes.src);
var shareLink = document.querySelector(optStrings.selectors.videoShareInput).value;
return {
url: vidUrl,
shareLink: shareLink
};
}
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();