Greasemonkey - 浏览器重定向

问题描述

所以我已经有一段时间没有使用 Greasemonkey 脚本了,我可能忘记了做一些基本的事情,但是...

我想做的是当我导航到 here 而不是将我重定向https://beta.crunchyroll.com 时,我认为我当前的代码应该这样做,但不是出于某种原因。

// ==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")
}

那么我在哪里搞砸了?

解决方法

您可以简化逻辑。

  1. 由于您只需要在 https://beta.crunchyroll.com 上运行它,因此在其他页面上运行它是没有意义的
  2. @match@include
  3. 更健壮
  4. 使用 @run-at 尽早运行

这是一个例子:

// ==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');