Copy YouTube Channel ID

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

От 25.02.2023. Виж последната версия.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да инсталирате разширение, като например Tampermonkey .

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==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);