Youtube toggle "Related Videos" visibility

Adds toggle text button to hide/show Related Videos. Useful in zoomed in mode in order to easily skip to the comments.

// ==UserScript==
// @name        Youtube toggle "Related Videos" visibility
// @namespace   Violentmonkey Scripts
// @match       http*://*.youtube.com/watch*
// @grant       none
// @version     1.1
// @author      Eliran Gonen
// @description Adds toggle text button to hide/show Related Videos. Useful in zoomed in mode in order to easily skip to the comments.
// ==/UserScript==

related = document.querySelector("#primary #related");

window.related_toggle_hidden = function () {
	related.hidden = !related.hidden
	related_button = document.querySelector("#RelatedToggleBtn")
	if (related_button.textContent[0] == "+") 
		related_button.textContent = related_button.textContent.replace("+", "-")
	else if (related_button.textContent[0] == "-") 
		related_button.textContent = related_button.textContent.replace("-", "+")
}

function add_related_toggle_button() {
  console.log("add_related_toggle_button got called")
  
  related = document.querySelector("#primary #related");
  
  if (related == null) {
    return
  }
  
	// <div>
	div_el = document.createElement("div")
	div_el.id = "related_toggle_button"
	
	// <button>
        button_el = document.createElement("button")
        button_el.id = "RelatedToggleBtn"
        button_el.setAttribute("onclick", "javascript:related_toggle_hidden()")
        button_el.textContent = "Toggle related"
	
        div_el.appendChild(button_el)
	related.parentNode.insertBefore(div_el, related.previousSibling);
}

function onresize_handler() {
  console.log("Resize detected")
  
  primary_related = document.querySelector("#primary #related");
  primary_btn = document.querySelector("#primary #related_toggle_button");

  secondary_related = document.querySelector("#secondary #related");

  if (primary_related != null && primary_btn == null) {
    add_related_toggle_button()
  }
  
  if (primary_btn != null && secondary_related != null ) {
    primary_btn.remove()
  }
}

add_related_toggle_button()
document.body.onresize = onresize_handler;