Created
December 9, 2022 21:02
-
-
Save heffcodex/343a48d2a38cc37cc9fb2fd18866e231 to your computer and use it in GitHub Desktop.
Get YouTube URL containing canonical channel ID JS function suitable for bookmarklet
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
((w) => { | |
const urlPrefix = "https://www.youtube.com/"; | |
const href = w.location.href; | |
if (!href.startsWith(urlPrefix)) { | |
alert("wrong page"); | |
return; | |
} | |
fetch(href) | |
.then((res) => res.text()) | |
.then((text) => new DOMParser().parseFromString(text, "text/html")) | |
.then((doc) => doc.querySelector('meta[itemprop="channelId"]')) | |
.then((attr) => attr.getAttribute("content")) | |
.then((channelId) => | |
prompt("The URL is:", urlPrefix + "channel/" + channelId) | |
) | |
.catch((err) => console.log(err)); | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment