Search code examples
greasemonkey

Greasemonkey - Browser redirect


So it's been a minute since I've used Greasemonkey scripts and I'm probably forgetting to do something basic but...

What I want to do is when ever I navigate to https://beta.crunchyroll.com to instead redirect me to https://beta.crunchyroll.com/simulcasts/seasons/spring-2021 and I think my current code should do that but isn't for some reason.

// ==UserScript==
// @name     Crunchyroll Redirect
// @version  1
// @grant    none
// @include     http://*.crunchyroll.*/*
// @include     https://*.crunchyroll.*/*
// ==/UserScript==

var current_location = content.document.location;

if(current_location == "https://beta.crunchyroll.com"){
            window.location.replace("https://beta.crunchyroll.com/simulcasts/seasons/spring-2021")
}

So where did I mess up?


Solution

  • You can simplify the logic.

    1. Since you only need to run it on https://beta.crunchyroll.com, there is no point on running it on other pages
    2. @match is more robust than @include
    3. Use @run-at to run at the earliest possible

    Here is an example:

    // ==UserScript==
    // @name          Crunchyroll Redirect
    // @match         *://beta.crunchyroll.com/*
    // @version       1
    // @grant         none
    // @run-at        document-start
    // ==/UserScript==
    
    window.stop();    // stop loading
    location.replace('https://beta.crunchyroll.com/simulcasts/seasons/spring-2021');