Skip to content

Instantly share code, notes, and snippets.

@heffcodex
Created December 9, 2022 21:02
Show Gist options
  • Save heffcodex/343a48d2a38cc37cc9fb2fd18866e231 to your computer and use it in GitHub Desktop.
Save heffcodex/343a48d2a38cc37cc9fb2fd18866e231 to your computer and use it in GitHub Desktop.
Get YouTube URL containing canonical channel ID JS function suitable for bookmarklet
((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