From 2b56584b1b998fc71d0eb0a936005a72ffd66ab9 Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Sun, 19 Jan 2020 15:16:46 +0200 Subject: [PATCH] Clean up JS code --- getVidLinks.js | 86 +++++++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/getVidLinks.js b/getVidLinks.js index a3cab70..9924131 100644 --- a/getVidLinks.js +++ b/getVidLinks.js @@ -1,48 +1,64 @@ -createVidUrlElement = function(videoSet) { - var videoArray = Object.entries(videoSet).map(x => { - return { - shareLink: x[1].shareLink, - url: x[0], - }; - }); +optStrings = { + selectors: { + feedVideoItem: 'video-feed-item-wrapper', + 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', + }, + classes: { + modalCloseDisabled: 'disabled', + }, + tags: { + resultTag: 'video_urls', + resultParentTag: 'body', + }, + attributes: { + src: "src", + }, +}; - var urlSetElement = document.createElement("video_urls"); +createVidUrlElement = function(videoArray) { + var urlSetElement = document.createElement(optStrings.tags.resultTag); urlSetElement.innerText = JSON.stringify(videoArray); - document.getElementsByTagName("body")[0].appendChild(urlSetElement); + document.getElementsByTagName(optStrings.tags.resultParentTag)[0].appendChild(urlSetElement); } -buldVidUrlSet = function(finishCallback) { - var feedItem = document.getElementsByClassName("video-feed-item-wrapper")[0]; +buldVidUrlArray = function(finishCallback) { + var feedItem = document.getElementsByClassName(optStrings.selectors.feedVideoItem)[0]; feedItem.click(); - var videoSet = {}; + var videoArray = []; var intervalID = window.setInterval(x => { - var players = document.getElementsByClassName("video-player"); - for (var i = 0; i < players.length; i++) { - var vidUrl = players[i].getAttribute("src"); - if(!videoSet[vidUrl]) { - var shareLink = document.querySelector(".copy-link-container > input").value; - videoSet[vidUrl] = { - shareLink: shareLink - }; - } - } - var arrowRight = document.querySelectorAll("div.video-card-modal > div > img.arrow-right")[0]; - if (arrowRight.classList.contains("disabled")) { + videoArray.push(getCurrentModalVideo()); + + var arrowRight = document.querySelectorAll(optStrings.selectors.modalArrowLeft)[0]; + if (arrowRight.classList.contains(optStrings.classes.modalCloseDisabled)) { window.clearInterval(intervalID); - document.querySelector(".video-card-modal > div > div.close").click(); - finishCallback(videoSet); + document.querySelector(optStrings.selectors.modalClose).click(); + finishCallback(videoArray); } else { arrowRight.click(); } }, 500); }; +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 + }; +} + scrollWhileNew = function(finishCallback) { var state = { count: 0 }; var intervalID = window.setInterval(x => { var oldCount = state.count; - state.count = document.getElementsByClassName("video-feed-item").length; + state.count = document.getElementsByClassName(optStrings.selectors.feedVideoItem).length; if (oldCount !== state.count) { window.scrollTo(0, document.body.scrollHeight); } else { @@ -52,15 +68,21 @@ scrollWhileNew = function(finishCallback) { }, 1000); }; +bootstrapIteratingVideos = function() { + var intervalID = window.setInterval(() => { + window.scrollTo(0, document.body.scrollHeight); + if (document.getElementsByClassName(optStrings.selectors.feedVideoItem).length > 0) { + window.setTimeout(() => buldVidUrlArray(createVidUrlElement), 100); + window.clearInterval(intervalID); + } + }, 500); +}; + init = () => { const newProto = navigator.__proto__; delete newProto.webdriver; navigator.__proto__ = newProto; - - window.setTimeout(x => { - window.scrollTo(0, document.body.scrollHeight); - window.setTimeout(x => buldVidUrlSet(createVidUrlElement), 2000); - }, 1000) + bootstrapIteratingVideos(); }; init();