Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Greasemonkey/Tampermonkey脚本重定向到双重修改的URL_Javascript_Url_Greasemonkey_Userscripts_Tampermonkey - Fatal编程技术网

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

Javascript Greasemonkey/Tampermonkey脚本重定向到双重修改的URL,javascript,url,greasemonkey,userscripts,tampermonkey,Javascript,Url,Greasemonkey,Userscripts,Tampermonkey,目标页面的URL为:ouo.io/tLnpEc.html 我想把URL改为:ouo.press/tLnpEc 即:.io至。按并删除.html 我已经有了这个,但它不起作用(它重定向到ouo.press,但仍然没有删除.html): 我希望有人能在这个问题上提供帮助。相关:(和其他几个) 要点: 检查页面位置,确保您尚未重定向;避免无限重定向循环 不要在.href上操作。这将导致各种引用、搜索等链接和重定向的副作用和错误触发 使用@run at document start减少延迟和恼人的“闪烁

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

我想把URL改为:
ouo.press/tLnpEc

即:
.io
。按
并删除
.html

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

我希望有人能在这个问题上提供帮助。

相关:(和其他几个)

要点:

  • 检查页面位置,确保您尚未重定向;避免无限重定向循环
  • 不要在
    .href
    上操作。这将导致各种引用、搜索等链接和重定向的副作用和错误触发
  • 使用
    @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);
    }
    
    // ==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);
    }