Skip to main content

Fixing youtube.com/tv not closing

·1 min

If you are like me and you want to use https://youtube.com/tv on your HTPC (computer connected to your TV) with something like the following flags:

--kiosk --user-agent="Mozilla/5.0 (X11; Linux x86_64; Xbox; Xbox One; Valve Steam Gamepad)" "https://youtube.com/tv#/"

You might have encountered an issue where, after watching a video, attempting to exit YouTube using the controller’s back/escape button proves ineffective.

Upon inspecting developer tools, you’ll discover that the browser refuses to execute window.close, claiming the window wasn’t opened by the script. Ironically the exit functionality works fine before watching videos and much javascript has had the chance to run.

Scripts may close only the windows that were opened by it.

So what do we do to work around this? Well lets just make sure the window is opened by a script in the first place.

Create a open-youtube.html file containing the following:

<!DOCTYPE html>
<html>
  <head>
    <title>Open YouTube</title>
  </head>
  <body onload="window.open('https://www.youtube.com/tv#/', '_blank');window.close()">
  </body>
</html>

Visit it with your browser of choice (either by double clicking or typing the path in the browser) and authorize the popup.

After this configure your shortuct open the new file instead:

--kiosk --user-agent="Mozilla/5.0 (X11; Linux x86_64; Xbox; Xbox One; Valve Steam Gamepad)" "C:\Users\John\Documents\open-youtube.html"

Enjoy a https://youtube.com/tv experience which actually closes!