How Can I Redirect To My Youtube Subscription Page In Greasemonkey?
Solution 1:
Problems:
YouTube pages have multiple elements with the same
id
in one document, which is a violation of the HTML standard, sodocument.getElementById("logo")
will find the wrong one. You can usedocument.querySelector('a#logo')
instead.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).
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?"