Last active
May 7, 2023 14:19
-
-
Save AndersMoberg/0a9f996a49d7b3a9e9c01065ce29abf4 to your computer and use it in GitHub Desktop.
Userscript, which adds the ability to clear the users Watch Later queue on Youtube. Swedish interface only for now!
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 Clear Youtube Watch Later | |
// @name:sv Rensa Youtube "Watch Later" | |
// @description Adds the ability to clear the users Watch Later queue on Youtube | |
// @description:sv Lägger till förmogan att rensa "Watch Later" listan på Youtube | |
// @version 0.1 | |
// @author Anders O.H Moberg | |
// @homepageURL https://a-moberg.com/ | |
// @grant none | |
// @match https://www.youtube.com/playlist?list=WL* | |
// @updateURL https://gist.github.com/AndersMoberg/0a9f996a49d7b3a9e9c01065ce29abf4/raw/youtubewatchlaterclear.user.js | |
// @downloadURL https://gist.github.com/AndersMoberg/0a9f996a49d7b3a9e9c01065ce29abf4/raw/youtubewatchlaterclear.user.js | |
// ==/UserScript== | |
const selectorLocale = { | |
'sv-SE': { | |
button: 'Rensa lista', | |
action: 'Åtgärdsmeny', | |
remove: 'Ta bort från', | |
userscriptConfirmation: 'Om du trycker OK, så börjar vi rensa.', | |
userscriptStartupNotification: 'Videos börja rensas!', | |
userscriptDoneNotification: 'Videos är klarrensade!', | |
} | |
} | |
function awaitUntilCanFindButtonbar() { | |
const buttonBarSelector = "ytd-menu-renderer.style-scope.ytd-playlist-header-renderer" | |
return new Promise(resolve => { | |
let intervalId; | |
const monkey = function () { | |
let buttonBar = document.querySelector(buttonBarSelector); | |
console.log(buttonBar) | |
if (buttonBar != undefined) { | |
clearInterval(intervalId) | |
resolve(buttonBar); | |
} | |
} | |
intervalId = setInterval(monkey, 1000) | |
}) | |
} | |
let localeKey; | |
function setLocale() { | |
localeKey = document.querySelector('html').getAttribute("lang") | |
} | |
function getLocaleObj() { | |
return selectorLocale[localeKey]; | |
} | |
window.addEventListener("load", async function () { | |
setLocale(); | |
if (getLocaleObj() == undefined) { | |
console.error("There is no support for " + locale); | |
return; | |
} | |
let buttonBar = await awaitUntilCanFindButtonbar(); | |
let button = document.createElement("button"); | |
button.innerText = getLocaleObj().button; | |
button.onclick = () => { | |
if (window.confirm(getLocaleObj().userscriptConfirmation)) { | |
clearVideoIntervalID = setInterval(clearVideo, msInterval); | |
GM.notification(getLocaleObj().userscriptStartupNotification, "Youtube Watch Later"); | |
} | |
} | |
buttonBar.appendChild(button); | |
}); | |
const msInterval = 750; | |
let clearVideoIntervalID; | |
function clearVideo() { | |
meny = document.querySelector(`#primary button[aria-label="${getLocaleObj().action}"]`); | |
if (meny === null) { | |
clearInterval(clearVideoIntervalID); | |
GM.notification(getLocaleObj().userscriptDoneNotification, "Youtube Watch Later"); | |
} | |
meny.click(); | |
var things = document.evaluate(`//span[contains(text(),"${getLocaleObj().remove}")]`, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |
for (var i = 0; i < things.snapshotLength; i++) { | |
things.snapshotItem(i).click(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my first userscript, and my first one public to boot.
Thanks a lot to the people of this comment thread for references: https://gist.github.com/astamicu/eb351ce10451f1a51b71a1287d36880f