Copy YouTube Channel ID

Adds a 'Copy Channel ID' button to the top of YouTube channels.

25.02.2023 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name     Copy YouTube Channel ID
// @description Adds a 'Copy Channel ID' button to the top of YouTube channels.
// @version  1.1
// @match    https://*.youtube.com/@*
// @match    https://www.youtube.com/@*
// @match    https://youtube.com/@*
// @match    https://www.youtube.com/@
// @match    https://www.youtube.com/c/*
// @grant    GM_setClipboard
// @grant    GM_registerMenuCommand
// @icon     https://www.google.com/s2/favicons?domain=youtube.com
// @namespace https://github.com/Vandekieft/MonkeyScripts/
// @license  MIT
// ==/UserScript==

// Define a function to copy the channel ID to the clipboard
function copyChannelId() {
  var justTheId = getChannelId();
  var textarea = document.createElement("textarea");
  textarea.value = justTheId;
  document.body.appendChild(textarea);
  textarea.select();
  var range = document.createRange();
  range.selectNodeContents(textarea);
  var selection = window.getSelection();
  selection.removeAllRanges();
  selection.addRange(range);
  document.execCommand("copy");
  selection.removeAllRanges(); // Clear the current selection
  document.body.removeChild(textarea); // Remove the temporary textarea element
}

// Define a function to copy the channel URL to the clipboard
function copyChannelUrl() {
  var justTheId = getChannelId();
  var channelUrl = "http://www.youtube.com/channel/" + justTheId;

  // Copy the URL to the clipboard
  GM_setClipboard(channelUrl);
}

// Register a menu command to copy the channel URL
GM_registerMenuCommand("Copy Channel URL", copyChannelUrl);

// Define a function to redirect to the channel URL
function redirectToChannel() {
  var justTheId = getChannelId();
  if (justTheId) {
    window.location.replace("http://www.youtube.com/channel/" + justTheId);
  }
}

// Register a menu command to redirect to the channel URL
GM_registerMenuCommand("Go to Channel URL", redirectToChannel);

// Register a function to get the channel ID
function getChannelId() {
  var channelUrl = window.location.href;
  var justTheId;

  if (channelUrl.indexOf("youtube.com/channel/") !== -1) {
    // Extract the channel ID from the channel URL
    justTheId = channelUrl.slice(channelUrl.indexOf("youtube.com/channel/") + 20);
  } else {
    // Parse the HTML to extract the channel ID
    var entirePage = document.getElementsByTagName("html")[0].innerHTML;
    var restOfThePage = entirePage.slice(entirePage.indexOf("},\"channelUrl\"") + 48);
    justTheId = restOfThePage.slice(0, restOfThePage.indexOf("\""));
  }

  return justTheId;
}

// Listen for changes to the URL using MutationObserver
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type === 'attributes' && mutation.attributeName === 'href') {
      // The URL has changed, so update the channel ID
      var justTheId = getChannelId();
    }
  });
});

// Observe changes to the href attribute of the <a> tag containing the channel name
var targetNode = document.querySelector('#text > a');
var config = { attributes: true, childList: false, subtree: false };
observer.observe(targetNode, config);