【问题标题】:Greasemonkey/Tampermonkey script to redirect to doubly modified URLGreasemonkey/Tampermonkey 脚本重定向到双重修改的 URL
【发布时间】:2018-09-14 10:48:35
【问题描述】:

目标页面的 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);
}

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

【问题讨论】:

    标签: javascript url greasemonkey userscripts tampermonkey


    【解决方案1】:

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

    关键点:

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

    这是执行这些 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-06
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 2018-05-15
      • 2013-10-23
      • 1970-01-01
      相关资源
      最近更新 更多