Move youtube comment

Youtubeのコメント欄を右上の方に移動するスクリプト

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        Move youtube comment
// @namespace   nazo6.github.io
// @match       https://www.youtube.com/*
// @grant       none
// @version     1.0.3
// @author      nazo6
// @license     MIT
// @description Youtubeのコメント欄を右上の方に移動するスクリプト
// ==/UserScript==

let crr_path = null

const observer = new MutationObserver((mutations) => {
  execute()
})

observer.observe(document, {
    characterData: true,
    subtree: true
});

function sleep(n){
  return new Promise(function(resolve){
    setTimeout(resolve, 100);
  });
}

async function execute() {
  if(location.pathname.startsWith("/watch") || location.pathname.startsWith("/live")) {
    if (!document.getElementById("comment-toggle-button")) {
      const buttonInsertRef = document.querySelector("#top-row #menu > ytd-menu-renderer > *:last-child")
      if (buttonInsertRef) {
        buttonInsertRef.insertAdjacentHTML("beforebegin", `
          <button id="comment-toggle-button" style="margin:0 5px;">
            move
          </button>
        `)
        document.querySelector("#comment-toggle-button").onclick = toggleComment
      } else {
        console.warn("Failed to find reference element");
      }
    }
  }
}

function toggleComment() {
  const toggleButton = document.querySelector("#comment-toggle-button")
  const toggleButtonText = toggleButton.textContent

  if (toggleButtonText.trim() === "move") {
    const sectionHeight = document.querySelector("#player").clientHeight
    const newCommentSectionParent = document.querySelector("#related")
    newCommentSectionParent.insertAdjacentHTML("beforebegin", `
      <div style="height:${sectionHeight}px; overflow:scroll" id="new-comment-section-container"></div>
    `)
    document.getElementById('new-comment-section-container').appendChild(document.getElementById('comments'))

    toggleButton.textContent = "restore"
  } else if (toggleButtonText.trim() === "restore") {
    document.getElementById('below').appendChild(document.getElementById('comments'))
    document.getElementById('new-comment-section-container').remove()

    toggleButton.textContent = "move"
  }
}