增加會限清單分頁連結

增加YouTube會限清單分頁連結到頻道主頁上

  1. // ==UserScript==
  2. // @version 1.1.0
  3. // @author TsukiAkiba, Danny Tsai
  4. // @description 增加YouTube會限清單分頁連結到頻道主頁上
  5. // @description:en Add members-only-videos link to YouTube channel main page.
  6. // @license MIT License
  7. // @name 增加會限清單分頁連結
  8. // @name:en Add members-only-videos link
  9. // @match https://www.youtube.com/*
  10. // @namespace https://github.com/erase2004/add-members-only-videos-list-button
  11. // @grant none
  12. // @run-at document-start
  13. // ==/UserScript==
  14. (function() {
  15. 'use strict';
  16. window.onload = function() {
  17. function addLink() {
  18. const displayTextMap = {
  19. 'zh-Hant-TW': '會限清單',
  20. 'zh-Hant-HK': '會限清單',
  21. 'zh-Hans-CN': '会限清单',
  22. 'ja-JP': 'メン限リスト',
  23. 'en': 'Members-only-video List'
  24. };
  25. const anchorAttribute = 'data-anchor-attribute';
  26. const anchorElement = document.querySelector("yt-tab-shape:nth-last-of-type(2)");
  27.  
  28. if (anchorElement === null) return;
  29. if (document.querySelector(`[${anchorAttribute}]`) !== null) return;
  30.  
  31. let displayText = displayTextMap[document.documentElement.lang] || displayTextMap.en;
  32. const newNode = document.createRange().createContextualFragment(`
  33. <yt-tab-shape class="yt-tab-shape-wiz yt-tab-shape-wiz--host-clickable" role="tab" aria-selected="false" tabindex="0" tab-identifier="TAB_ID_SPONSORSHIP_PLAYLIST" tab-title="${displayText}" ${anchorAttribute}>
  34. <div class="yt-tab-shape-wiz__tab">${displayText}</div>
  35. <div class="yt-tab-shape-wiz__tab-bar">
  36. </div>
  37. </yt-tab-shape>
  38. `);
  39. anchorElement.parentNode.insertBefore(newNode, anchorElement);
  40. const target = document.querySelector("yt-tab-shape:nth-last-of-type(3)");
  41.  
  42. target.addEventListener('click', function() {
  43. const chId = document.querySelector('[itemprop="identifier"]').getAttribute("content");
  44. const targetURL = `${location.protocol}//${location.host}/playlist?list=${chId.replace(/^UC/, 'UUMO')}`;
  45. window.open(targetURL);
  46. });
  47. }
  48.  
  49. if (window.MutationObserver) {
  50. let observer = new MutationObserver(function(mutations) {
  51. mutations.forEach(mutation => {
  52. if (mutation.type == 'childList') {
  53. if (mutation.target.classList.contains("yt-tab-group-shape-wiz__tabs")) {
  54. addLink()
  55. }
  56. }
  57. });
  58. });
  59. observer.observe(document.querySelector('body'), { "childList": true, "subtree": true });
  60. }
  61. };
  62. })();