Last active
May 17, 2019 16:34
-
-
Save RafalJDev/7609d80f0021d8986e0ce157f0c64461 to your computer and use it in GitHub Desktop.
Expand titles and show playlist popup if video is on some playlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Youtube script | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://www.youtube.com/* | |
// @run-at document-end | |
// @grant none | |
// ==/UserScript== | |
(async function() { | |
expandTitles(); | |
//wait till | |
var buttonIndex = await waitTillSaveOrZapiszAppearAndReturnButtonIndex() | |
click('style-scope ytd-button-renderer', buttonIndex) | |
//wait till: | |
await waitTillPopupAppear() | |
var checkedAmount = document.getElementsByClassName('checked').length | |
console.log("Checked amount:" + checkedAmount); | |
if (checkedAmount == 0) { | |
click('style-scope ytd-add-to-playlist-renderer', 3) | |
} | |
})(); | |
function expandTitles() { | |
var elements = document.querySelectorAll('#video-title.yt-simple-endpoint.style-scope.ytd-grid-video-renderer'); | |
elements.forEach(element => { | |
element.style.maxHeight = '7em'; | |
element.style.webkitLineClamp = '7' | |
}) | |
} | |
async function waitTillSaveOrZapiszAppearAndReturnButtonIndex() { | |
var wasExceptionThrown; | |
do { | |
console.log("12346") | |
wasExceptionThrown = false; | |
try { | |
var saveButton = document.getElementsByClassName('style-scope ytd-button-renderer') | |
if (saveButton[8].innerText === 'ZAPISZ') { | |
return 8; | |
} else if (saveButton[10].innerText === 'SAVE') { | |
return 10; | |
} | |
} catch (e) { | |
wasExceptionThrown = true | |
await sleep(100); | |
} | |
} while (wasExceptionThrown); | |
} | |
function click(className, elementIndex) { | |
console.log("123") | |
var evt = document.createEvent("MouseEvents"); | |
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | |
document.getElementsByClassName(className)[elementIndex].dispatchEvent(evt); | |
console.log("234") | |
} | |
function waitTillPopupAppear() { | |
var wasExceptionThrown; | |
console.log("1234") | |
wasExceptionThrown = false; | |
try { | |
var hasPopupAppeared = document.getElementsByClassName('checkbox-height style-scope ytd-playlist-add-to-option-renderer')[0].innerText === 'Watch later' | |
} catch (e) { | |
wasExceptionThrown = true | |
setTimeout(function () { waitTillPopupAppear()} | |
,100); | |
sleep(100) | |
} | |
} | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment