I'm writing a Tampermonkey script that I want to use to redirect from youtube.com/*
to a YouTube channel address.
window.addEventListener ("load", LocalMain, false);
function LocalMain () {
location.replace("https://www.youtube.com/channel/*");
}
When the script is running it redirects to the channel URL but then keeps running and continuously redirects.
The standard, much more efficient, and much faster performing way to do this kind of redirect is to tune the script's metadata to not even run on the redirected-to page.
Also, use // @run-at document-start
for even better response.
So your script would become something like:
// ==UserScript==
// @name YouTube, Redirect to my channel
// @match https://www.youtube.com/*
// @exclude https://www.youtube.com/channel/*
// @run-at document-start
// ==/UserScript==
location.replace("https://www.youtube.com/channel/UC8VkNBOwvsTlFjoSnNSMmxw");
Also, for such "broad spectrum" redirect scripts, consider using location.assign()
so that you or your user can recover the original URL in the history in case of an overzealous redirect.