Skip to content Skip to sidebar Skip to footer

How Can I Redirect To My Youtube Subscription Page In Greasemonkey?

I don't want to see distracting youtube recommendations on my start screen, I just want to be redirected to my subscription page whenever I go to the / path of youtube. I tried to

Solution 1:

Problems:

  1. YouTube pages have multiple elements with the same id in one document, which is a violation of the HTML standard, so document.getElementById("logo") will find the wrong one. You can use document.querySelector('a#logo') instead.

  2. The page is built dynamically using JavaScript so the elements may appear long time after DOMContentLoaded event has occurred. You can use MutationObserver (may be resource-consuming) or setTimeout/setInterval (not precise).

  3. YouTube site is a SPA (single page application) which imitates navigation by using History API but userscripts are executed only on real navigation.


Answer :

There could be many solutions, arguably, but I suggest using a simple click listener in capture phase (the third parameter of addEventListener is true) so that you don't have to depend on the exact selector for the element, which may change unpredictably when the site code updates in the future.

// ==UserScript==// @name        YT redir /home -> /sub// @match       https://www.youtube.com/*// @grant       none// @run-at      document-start// ==/UserScript==if (location.pathname === '/') {
  location.pathname = '/feed/subscriptions';
}
window.addEventListener('click', e => {
  const a = e.target.closest('a');
  if (a && a.href === 'https://www.youtube.com/') {
    a.pathname = '/feed/subscriptions';
    a.className = '';
  }
}, true);

Post a Comment for "How Can I Redirect To My Youtube Subscription Page In Greasemonkey?"