Greasemonkey / Tampermonkey UserScript进行URL路径更改

问题描述 投票:0回答:1

我需要一个脚本来更改URL和重定向页面中的某些参数。例如:

更改此内容:

http://cloud.somesite.fr/somepath

到那个:

http://account.somesite.com/somepath

很容易将“ .fr”更改为“ .com”。但是问题是,我不明白如何选择该“云”参数。它既不是协议,也不是域。通常它是URL的“ www”部分,但是在某些站点中,在“ http://”之后有“ www”的特殊地址,我需要更改它。

[我以此为基础并进行了实验,但没有按照我想要的方式工作:

// ==UserScript==
// @name        my redirect (based on _Redirect from one domain to another)
// @match       *://somesite.ft/*
// @include       https://*.somesite.fr/*
// @run-at      document-start
// @grant       none
// ==/UserScript==
var oldUrlPath  = location.pathname;
if ( ! oldUrlPath.includes ("somesite.fr") ) 
{

var newDomain   = "somesite.com";
var newURL      = location.protocol + "//"
                + newDomain                 //-- location.host
                + location.pathname
                + location.search
                + location.hash
                ;
/*-- replace() puts the good page in the history instead of the
    bad page.
*/
location.replace (newURL);
}

[这里有很多类似的脚本(我已经进行过搜索),但是我无法为我的特殊情况找到一个:当有其他字符串而不是“ www”时,选择“ www”部分。

url redirect greasemonkey userscripts
1个回答
0
投票

基于到目前为止提供的信息:

// ==UserScript==
// @name        my redirect (based on _Redirect from one domain to another)
// @match       *://*.somesite.fr/*
// @run-at      document-start
// @grant       none
// ==/UserScript==


// example http://cloud.somesite.fr/somepath
// location.hostname is cloud.somesite.fr

if (location.hostname.startsWith('cloud.')) {

  window.stop(); // stop loading the window any more
  location.hostname = 'account.somesite.com'; // change location.hostname to the desired one, path & hash stays the same
} 
© www.soinside.com 2019 - 2024. All rights reserved.