Last active
January 3, 2023 19:08
-
-
Save jack126guy/a717e9d6efe491c1fe4621f55e59a277 to your computer and use it in GitHub Desktop.
Userscript to show YouTube channel ID next to channel name
This file contains hidden or 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 Show YouTube channel ID | |
// @description Add YouTube channel ID next to channel name for visibility | |
// @version 4 | |
// @match https://*.youtube.com/* | |
// @grant GM.setClipboard | |
// ==/UserScript== | |
(function (window, document, setClipboard) { | |
function getChannelId(event) { | |
try { | |
var detail = event.detail; | |
if (!detail.pageType === 'channel') { | |
return null; | |
} | |
return detail.response.response.header.c4TabbedHeaderRenderer.channelId; | |
} catch (e) { | |
return null; | |
} | |
} | |
function getChannelIdNote() { | |
var noteList = document.getElementsByClassName('sytcid-note'); | |
if (noteList.length > 0) { | |
return noteList[0]; | |
} | |
} | |
function createChannelIdNote() { | |
var note = document.createElement('span'); | |
note.classList.add('sytcid-note'); | |
note.style.color = 'red'; | |
note.appendChild(document.createTextNode('[')); | |
var idElement = document.createElement('span'); | |
idElement.classList.add('sytcid-id'); | |
note.appendChild(idElement); | |
note.appendChild(document.createTextNode(']')); | |
if (setClipboard) { | |
var copyLink = document.createElement('a'); | |
copyLink.textContent = '(copy ID)'; | |
copyLink.href = '#'; | |
copyLink.classList.add('sytcid-copy'); | |
copyLink.addEventListener('click', function (event) { | |
setClipboard(idElement.textContent); | |
event.preventDefault(); | |
}); | |
note.appendChild(copyLink); | |
} | |
return note; | |
} | |
function getChannelNameElement() { | |
return document.querySelector('#channel-header #channel-name'); | |
} | |
function updateChannelIdNote(event) { | |
var channelIdNote = getChannelIdNote(); | |
if (!channelIdNote) { | |
var channelNameElement = getChannelNameElement(); | |
if (!channelNameElement) { | |
return; | |
} | |
channelIdNote = createChannelIdNote(); | |
channelNameElement.appendChild(channelIdNote); | |
} | |
var channelId = getChannelId(event); | |
var channelIdElement = channelIdNote.querySelector('.sytcid-id'); | |
var copyLink = channelIdNote.querySelector('.sytcid-copy'); | |
if (channelId) { | |
channelIdElement.textContent = channelId; | |
if (copyLink) { | |
copyLink.style.display = 'inline'; | |
} | |
} else { | |
channelIdElement.textContent = 'Could not get channel ID'; | |
if (copyLink) { | |
copyLink.style.display = 'none'; | |
} | |
} | |
} | |
window.addEventListener('yt-navigate-finish', updateChannelIdNote); | |
})(window, document, GM.setClipboard); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment