Greasemonkey/Tampermonkey 重定向到双重修改的脚本 URL

Greasemonkey/Tampermonkey script to redirect to doubly modified URL

目标页面有 URL:ouo.io/tLnpEc.html

我想将 URL 更改为:ouo.press/tLnpEc

即:.io.press并删除.html

我已经有了这个,但它没有用(它重定向到 ouo.press 但仍然没有删除。html):

var url = window.location.host;

if (url.match("//ouo.io") === null) {
    url = window.location.href;
    if  (url.match("//ouo.io") !== null){
        url = url.replace("//ouo.io", "//ouo.press");
    } else if (url.match("/*.html") !== null){
        url = url.replace("/*.html", " ");
    } else {
        return;
    }
    console.log(url);
    window.location.replace(url);
}

我希望有人能帮助解决这个问题。

相关:Greasemonkey to redirect site URLs from .html to -print.html?(以及其他几个)。

要点:

  1. 检查页面位置以确保您尚未重定向;以避免无限重定向循环。
  2. 不要对 .href 进行操作。这将导致各种引用、搜索等链接和重定向的副作用和错误触发。
  3. 使用@run-at document-start减少延迟和烦人"blinks"。

这是执行那些 URL 更改和重定向的完整脚本

// ==UserScript==
// @name     _Redirecy ouo.io/...html files to ouo.press/... {plain path}
// @match    *://ouo.io/*
// @run-at   document-start
// @grant    none
// ==/UserScript==

//-- Only redirect if the *path* ends in .html...
if (/\.html$/.test (location.pathname) ) {
    var newHost     = location.host.replace (/\.io$/, ".press");
    var plainPath   = location.pathname.replace (/\.html$/, "");
    var newURL      = location.protocol + "//" +
        newHost                  +
        plainPath                +
        location.search          +
        location.hash
    ;
    location.replace (newURL);
}