如何使用用户脚本将页面切换到其他域?

时间:2015-06-25 22:33:00

标签: javascript greasemonkey url-redirection userscripts tampermonkey

我需要使用Greasemonkey替换URL中的特定文本。

例如,如果页面是:

http://adf.st/?u=https%3A%2F%2Finstagram.com%2Fp%2F4XOVEaD5Ca%2F

然后脚本将使用以下URL:

http://adfa.cc/?u=https%3A%2F%2Finstagram.com%2Fp%2F4XOVEaD5Ca%2F

注意:adf.st/之后的其余文本未修复。

1 个答案:

答案 0 :(得分:3)

这是一个标准问题;使用下面的模式。

@match行设置为旧域,newDomain设置为所需域。

// ==UserScript==
// @name        _Redirect from one domain to another
// @match       *://adf.st/*
// @run-at      document-start
// @grant       none
// ==/UserScript==

var newDomain   = "adfa.cc";
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);
相关问题