Greasemonkey / Tampermonkey脚本重定向到双重修改的URL [英] Greasemonkey/Tampermonkey script to redirect to doubly modified URL

查看:646
本文介绍了Greasemonkey / Tampermonkey脚本重定向到双重修改的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标网页包含以下网址:  ouo.io/tLnpEc.html

The target page has the URL:   ouo.io/tLnpEc.html

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

即: .io .press 并删除 .html

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

I already have this but it ain't working (It redirects to ouo.press but still doesn't remove .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);
}

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

I hope someone can help on this problem.

推荐答案

相关: Greasemonkey将站点URL从.html重定向到-print.html?(以及其他几个)。

Related: Greasemonkey to redirect site URLs from .html to -print.html? (and several others).

要点:


  1. 检查页面位置以确保您尚未重定向;避免无限重定向循环。

  2. 不要在 .href 上操作。这将导致各种引用,搜索等链接和重定向的副作用和错误触发。

  3. 使用 @ run-at document-start 减少延迟和恼人的闪烁。

  1. Check the page location to make sure you haven't already redirected; to avoid an infinite redirect loop.
  2. Don't operate on .href. This will cause side effects and false triggers for various referer, search, etc. links and redirects.
  3. Use @run-at document-start to reduce delays and annoying "blinks".

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

Here's a complete script that performs those URL changes and redirects:

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

这篇关于Greasemonkey / Tampermonkey脚本重定向到双重修改的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆